Skip to content

Instantly share code, notes, and snippets.

@bitbrain
Last active August 29, 2015 13:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bitbrain/8973698 to your computer and use it in GitHub Desktop.
/* Main file
*
* Author: Miguel Gonzalez <m.gonzalez@student.fontys.nl>
* Version: 1.0
* Since: 1.0
*/
#include <string>
#include <iostream>
#include <cstdlib>
#include <bitset>
using namespace std;
// Declarations for assignments
void assignment1_1() {
cout << "Miguel Gonzalez, Einsteinstrasse 5" << endl;
}
// Declarations
int readYear(string title) {
char yearSequence[4];
cout << title;
cin >> yearSequence;
return atoi(yearSequence);
}
void assignment1_2() {
int age = readYear("Current year: ") - readYear("Year of birth: ");
cout << "Congratulations! Your current age is " << age << endl;
}
void assignment1_3() {
string line1, line2;
cout << "Line 1: " << flush;
cin >> line1;
cout << "Line 2: " << flush;
cin >> line2;
cout << line1 << ", " << line2 << endl;
}
void assignment1_4() {
char ch;
cin >> ch;
cout << ++ch << endl;
}
void assignment1_5() {
char ch;
int i;
ch = 'A';
i = 123;
}
float miliToZoll(float mili) {
return mili / 1000.0 * 39.37;
}
float zollToMili(float zoll) {
return zoll / 39.37 * 1000;
}
void assignment1_6() {
cout << zollToMili(39.37) << endl;
}
void assignment1_7() {
int number = 0;
int n = 10;
int *nPtr =&n;
cout << *nPtr << endl;
cin >> number;
if (number > 0 && number <= 1000) {
number = (number << 6) + (number << 5) + (number << 2);
cout << number << endl;
} else {
cout << "INVALID INPUT!" << endl;
}
}
void assignment2_1()
{
int value = 0;
int biggest = 0;
while (cin >> value && value >= 0)
{
biggest = (value > biggest) ? value : biggest;
}
cout << "Biggest number was " << biggest << endl;
}
void assignment2_2()
{
double value;
double average;
double sum;
int count;
while (cin >> value && value >= 0.f)
{
++count;
sum += value;
average = sum / count;
}
cout << "Average was " << average << endl;
}
void assignment2_3()
{
int number = 0;
cout << "Number: ";
cin >> number;
int lastNumber = number % 10;
number = (number - lastNumber) / 10;
int otherNumber = number % 10;
cout << "Sum of last 2 digits: " << (otherNumber + lastNumber) << endl;
}
void assignment2_4()
{
int number = 0;
int last = 0;
int count = 20;
int smallerCount = 0;
while (count-- > 0)
{
cin >> number;
if (count < 19 && last > number)
{
++smallerCount;
}
last = number;
}
cout << smallerCount << " numbers followed by smaller ones." << endl;
}
void assignment2_5()
{
int count = 10;
int smallest;
int value = 0;
while (count-- > 0)
{
cin >> value;
if (count == 9)
{
smallest = value;
}
smallest = (value < smallest) ? value : smallest;
}
cout << "Smallest number is " << smallest << endl;
}
void assignment3_1()
{
string line, other;
cout << "Word: ";
cin >> line;
int index = line.length() - 1;
do
{
other.push_back(line[index]);
} while (index-- >= 0);
cout << "Inverse: " << other;
}
void assignment3_2()
{
int number;
int biggest = 0, count = 0;
while (cin >> number)
{
if (number > biggest)
{
biggest = number;
count = 1;
} else if (number == biggest)
{
++count;
}
}
cout << "Biggest was " << biggest << " and occurred " << count << " times" << endl;
}
int digitSum(int n)
{
int result = 0;
while (n > 0)
{
result += n % 10;
n /= 10;
}
return result;
}
void assignment4_2()
{
cout << digitSum(12345) << endl;
}
unsigned long int datumcode(int jahr, int monat, int tag)
{
unsigned long result = jahr << 4;
result ^= monat;
result <<= 5;
result ^= tag;
return result;
}
void assignment5_1()
{
bitset<16> bits(datumcode(21,6,17));
cout << bits << endl;
}
unsigned char bcd(int n)
{
char result;
int lastNumber = n % 10;
int secondNumber = ((n - lastNumber) / 10) % 10;
result ^= (secondNumber << 4);
result ^= lastNumber;
return result;
}
void assignment5_2()
{
bitset<16> bits(bcd(12345));
cout << bits << endl;
}
int komtVoor(int number, int arr[], int size)
{
int count = 0;
for (int i = 0; i < size; ++i)
{
(arr[i] == number) ? ++count : count;
}
return count;
}
void assignment5_5()
{
int arr[] = {1,2,3,4,5,5,6,5,7};
cout << komtVoor(5, arr, 9);
}
int nOverK(int n, int k)
{
// TODO
return nOverK(n, k - 1) * (n - k + 1) / k;
}
void assignment5_6()
{
cout << "Value: " << nOverK(10, 20) << endl;
}
int main(int argc, char** argv) {
//assignment1_1();
//assignment1_2();
//assignment1_3();
//assignment1_4();
//assignment1_5();
//assignment1_6();
//assignment1_7();
//assignment2_1();
//assignment2_2();
//assignment2_3();
//assignment2_4();
//assignment2_5();
//assignment2_6();
//assignment2_7();
//assignment3_1();
//assignment3_2();
assignment4_2();
//assignment5_1();
//assignment5_2();
//assignment5_6();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment