Skip to main content

Hello World!

Aug 31, 2020joshwrou
Loading...

More C++ Posts

Bit arithmetic + and -

Sep 1, 2023LeifMessinger

0 likes • 2 views

#define NUM_BITS 8
#include <iostream>
struct Number{
int num : NUM_BITS;
Number(){}
Number(const int& bruh){
num = bruh;
}
operator int() const { return num; }
Number& operator=(const int& bruh){
num = bruh;
return (*this);
}
};
using namespace std;
bool isNegative(const int& num){
//This gets the bitwise and of num and 10000000000000000000000000000000
//This implicit casts to bool, which means (num & (1 << 31)) != 0
return (num & (1 << 31));
}
void printBinaryNumber(const int& num, const int numBits){
for(int i = numBits; i > 0; --i){
//8..1
int bitMask = 1 << (i-1);
if(num & bitMask){ //Test the bit
cout << '1';
}else{
cout << '0';
}
}
}
void printCarryBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if(aBit && bBit || aBit && carry || bBit && carry){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void printBorrowBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if((!(aBit ^ carry)) && bBit){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void doProblem(const int& a, const int& b, const char& sign, const int& result, const int& numBits){
if(sign == '+'){
cout << ' '; printCarryBits(a, b, numBits); cout << endl;
}else{
cout << ' '; printBorrowBits(a, b, numBits); cout << endl;
}
cout << ' '; printBinaryNumber(a, numBits); cout << endl;
cout << sign; printBinaryNumber(b, numBits); cout << endl;
cout << "----------" << endl;
cout << ""; printBinaryNumber(result, numBits + 1); cout << " = " << result;
cout << endl;
}
int main(){
Number a = 0b110;
Number b = 0b011;
cout<< a << endl << b << endl;
doProblem(a, b, '+', a + b, NUM_BITS);
doProblem(a, b, '-', a - b, NUM_BITS);
doProblem(-a, b, '+', -a + b, NUM_BITS);
doProblem(a, b, '-', -a - b, NUM_BITS);
return 0;
}

Egg Problem Template

Jul 10, 2023LeifMessinger

0 likes • 3 views

#include <iostream>
#include <vector>
#include <limits>
#define DEBUG_TRIAL false
class Trial{
public:
const size_t HEIGHT;
std::string record;
//Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
//Eggs is the eggs remaining.
//Start is the bottom floor.
//End is one above the top floor.
const size_t BREAKING_HEIGHT;
size_t eggs;
size_t start;
size_t end;
size_t floorsLeft(){
return (end-start);
}
size_t middle(){
return start + (floorsLeft()/2UL);
}
size_t drops = 0;
Trial(const size_t BREAKING_HEIGHT, size_t eggs, size_t start, size_t end): BREAKING_HEIGHT(BREAKING_HEIGHT), eggs(eggs), start(start), end(end), HEIGHT(end), record(end, '_'){
record[BREAKING_HEIGHT] = 'B'; //Marking the breaking point
}
bool foundAnswer(){
return ((record[0] == 'X') || (record.find("OX")!=std::string::npos));
}
//returns true if the egg broke.
//height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
bool drop(size_t height){
#if DEBUG_TRIAL
std::cout << "Start: " << start << ". End: " << end << ". Floors Left: " << floorsLeft() << ". Middle Index: " << middle() << std::endl;
#endif
drops++;
bool cracked = height >= BREAKING_HEIGHT;
if(cracked) --eggs;
//Update the record
record[height] = (height >= BREAKING_HEIGHT)? 'X' : 'O';
#if DEBUG_TRIAL
//Print the record
std::cout << record << std::endl;
#endif
return cracked;
}
size_t nowWhat(){
if(foundAnswer()){
return drops;
}else if(eggs <= 0){ //Ran out of eggs
throw "Algorithm failed! No more eggs!";
return 1UL;
}else if(eggs > 1){
return wrecklessSearch();
}else{
return safeSearch();
}
}
size_t safeSearch(){
if(drop(start)){
--end;
}else{
++start;
}
return nowWhat();
}
size_t wrecklessSearch(){
//If the egg breaks
if(drop(middle())){
end -= (floorsLeft()/2UL);
}else{ //egg doesn't crack
start += (floorsLeft()/2UL);
}
return nowWhat();
}
//returns the amount of drops needed to find the answer
size_t search(){
return nowWhat();
}
};
//Height is the height of the building in floors.
//Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
//Eggs is the eggs given.
//returns the amount of drops needed to find the answer
size_t search(const size_t height, const size_t BREAKING_HEIGHT, size_t eggs){
Trial trial(BREAKING_HEIGHT, eggs, 0, height);
return trial.search();
}
class TrialStats {
public:
size_t min = std::numeric_limits<size_t>::max();
size_t max = 0;
double mean = -1.0;
void printStats(){
// Print the results
std::cout << "Minimum drops: " << min << std::endl;
std::cout << "Maximum drops: " << max << std::endl;
std::cout << "Mean drops: " << mean << std::endl;
}
};
//Benchmarks all the possible breaking points of a single building height with a number of eggs.
TrialStats trial(const size_t HEIGHT, const size_t eggs){
TrialStats stats;
int totaldrops = 0;
//Test every possible breaking point
//Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
for (int breakingHeight = 0; breakingHeight < HEIGHT; ++breakingHeight) {
size_t drops = search(HEIGHT, breakingHeight, eggs);
stats.min = std::min(stats.min, drops);
stats.max = std::max(stats.max, drops);
totaldrops += drops;
}
// Calculate the mean number of drops
stats.mean = static_cast<double>(totaldrops) / HEIGHT;
return stats;
}
//Benchmarks a single building height from 1 egg to MAX_EGGS
void testTower(const size_t height, const size_t MAX_EGGS){
//Drop every amount of eggs that you'd need.
for (int eggs = 1; eggs <= MAX_EGGS; ++eggs) {
std::cout << "Building height: " << height << ". Num eggs: " << eggs << std::endl;
TrialStats stats = trial(height, eggs);
stats.printStats();
std::cout << std::endl << std::endl;
}
}
//Benchmarks all buildings from 0 to MAX_HEIGHT
void benchmark(const size_t MAX_HEIGHT){
const size_t MAX_EGGS = 2;
//Test every building
for (size_t height = 1; height <= MAX_HEIGHT; ++height) {
testTower(height, std::min(height, MAX_EGGS));
}
}
int main() {
constexpr size_t MAX_HEIGHT = 36;
benchmark(MAX_HEIGHT);
return 0;
}

GCD using Stein's Algorithm

Jun 30, 2023Iceman_71

1 like • 2 views

// Iterative C++ program to
// implement Stein's Algorithm
//#include <bits/stdc++.h>
#include <bitset>
using namespace std;
// Function to implement
// Stein's Algorithm
int gcd(int a, int b)
{
/* GCD(0, b) == b; GCD(a, 0) == a,
GCD(0, 0) == 0 */
if (a == 0)
return b;
if (b == 0)
return a;
/*Finding K, where K is the
greatest power of 2
that divides both a and b. */
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
/* Dividing a by 2 until a becomes odd */
while ((a & 1) == 0)
a >>= 1;
/* From here on, 'a' is always odd. */
do
{
/* If b is even, remove all factor of 2 in b */
while ((b & 1) == 0)
b >>= 1;
/* Now a and b are both odd.
Swap if necessary so a <= b,
then set b = b - a (which is even).*/
if (a > b)
swap(a, b); // Swap u and v.
b = (b - a);
} while (b != 0);
/* restore common factors of 2 */
return a << k;
}
// Driver code
int main()
{
int a = 12, b = 780;
printf("Gcd of given numbers is %d\n", gcd(a, b));
return 0;
}

sum function

Sep 3, 2023AustinLeath

0 likes • 9 views

#include "stdio.h"
#include <stdlib.h>
int main (int argCount, char** args) {
int a = atoi(args[1]);
int b = atoi(args[2]);
unsigned int sum = 0;
unsigned int p = 1;
for (unsigned int i = 1; i < b; i++) {
p = p * i;
}
// (b!, (1 + b)!, (2 + b)!, ..., (n + b)!)
for (unsigned int i = 0; i < a; i++) {
p = p * (i + b);
sum = sum + p;
}
printf("y: %u\n", sum);
return 0;
}

wordScore.cpp

Apr 16, 2023LeifMessinger

0 likes • 0 views

#include <iostream>
#include <string> //Should already be in iostream
#include <cstdlib>
//A word score adds up the character values. a-z gets mapped to 1-26 for the values of the characters.
//wordScore [wordValue]
//Pipe in the input into stdin, or type the words yourself.
//Lowercase words only
int characterValue(const char b){
return ((b >= 'a') && (b <= 'z'))? ((b - 'a') + 1) : 0;
}
int main(int argc, char** argv){
//The first argument specifies if you are trying to look for a certain word score
int wordValue = (argc > 1)? std::atoi(argv[1]) : 0;
std::string line;
while(std::getline(std::cin, line)){
int sum = 0;
for(const char c : line){
sum += characterValue(c);
}
if(wordValue){ //If wordValue is 0 or the sum is the correct value
if(wordValue == sum){
std::cout << line << std::endl;
}
} else {
std::cout << sum << "\t" << line << std::endl;
}
}
return 0;
}

Hello, World!

Nov 18, 2022AustinLeath

0 likes • 2 views

#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}