From cbca973f699f10d45b725e94ed3f49be9cf1f512 Mon Sep 17 00:00:00 2001 From: Michael Abed Date: Sun, 12 Feb 2012 15:08:17 -0500 Subject: rename files based on submission guidelines --- Q1.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ Q2.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Q3.cpp | 41 ++++++++++++++++++++++++++++++++++++++++ Q4.cpp | 0 4 files changed, 159 insertions(+) create mode 100644 Q1.cpp create mode 100644 Q2.cpp create mode 100644 Q3.cpp create mode 100644 Q4.cpp diff --git a/Q1.cpp b/Q1.cpp new file mode 100644 index 0000000..0e26609 --- /dev/null +++ b/Q1.cpp @@ -0,0 +1,50 @@ + +#include + +using namespace std; +int main() +{ + double t1b, t1h, t2b, t2h; // base and height of triangle 1 and 2 + cout << "Enter the information for the first triangle:" << endl; + + do { + cout << "Base: "; + cin >> t1b; + if (t1b - (int)t1b != 0 || t1b <= 0) + cout << "Please enter a positive integer" << endl; + } while (t1b <= 0 || t1b - (int)t1b != 0); + + do { + cout << "Height: "; + cin >> t1h; + if (t1h - (int)t1h != 0 || t1h <= 0) + cout << "Please enter a positive integer" << endl; + } while (t1h <= 0 || t1h - (int)t1h != 0); + + cout << "Second Triangle:" << endl; + + do { + cout << "Base: "; + cin >> t2b; + if (t2b - (int)t2b != 0 || t2b <= 0) + cout << "Please enter a positive integer" << endl; + } while (t2b <= 0 || t2b - (int)t2b != 0); + + do { + cout << "Height: "; + cin >> t2h; + if (t2h - (int)t2h != 0 || t2h <= 0) + cout << "Please enter a positive integer" << endl; + } while (t2h <= 0 || t2h - (int)t2h != 0); + + double area1 = 0.5 * t1h * t1b; + double area2 = 0.5 * t2h * t2b; + + if (area1 > area2) + cout << "Triangle 1 is bigger by " << area1-area2 << " units." << endl; + else if (area2 > area1) + cout << "Triangle 2 is bigger by " << area2-area1 << " units." << endl; + else + cout << "Triangle 1 and Triangle 2 are the same size" << endl; + return 0; +} diff --git a/Q2.cpp b/Q2.cpp new file mode 100644 index 0000000..255b7f8 --- /dev/null +++ b/Q2.cpp @@ -0,0 +1,68 @@ + +#include + +using namespace std; + +int main() +{ + double conversion[10] = {0.47317, 2, 0.5, 0.23659, 2.1134, 4.2268}; + unsigned short sel; + double amount; + + cout << "Pints to Liters (enter 0)" << endl; + cout << "Pints to Cups (enter 1)" << endl; + cout << "Cups to Pints (enter 2)" << endl; + cout << "Cups to Liters (enter 3)" << endl; + cout << "Liters to Pints (enter 4)" << endl; + cout << "Liters to Cups (enter 5)" << endl; + + do { + cout << "?: "; + cin >> sel; + if (sel > 5) + cout << "Enter a number between 0 and 5" << endl; + } while (sel > 5); + + do { + switch (sel) { + case 0: // fallthrough + case 1: + cout << "Enter the amount in Pints: "; + break; + case 2: + case 3: + cout << "Enter the amount in Cups: "; + break; + case 4: + case 5: + cout << "Enter the amount in Liters: "; + break; + } + cin >> amount; + if (amount < 0) + cout << "Enter a positive number" << endl; + } while (amount < 0); + + double result = conversion[sel] * amount; + switch (sel) { + case 0: + cout << amount << " Pints is " << result << " Liters" << endl; + break; + case 1: + cout << amount << " Pints is " << result << " Cups" << endl; + break; + case 2: + cout << amount << " Cups is " << result << " Pints" << endl; + break; + case 3: + cout << amount << " Cups is " << result << " Liters" << endl; + break; + case 4: + cout << amount << " Liters is " << result << " Pints" << endl; + break; + case 5: + cout << amount << " Liters is " << result << " Cups" << endl; + break; + } + return 0; +} diff --git a/Q3.cpp b/Q3.cpp new file mode 100644 index 0000000..17f385a --- /dev/null +++ b/Q3.cpp @@ -0,0 +1,41 @@ + +#include + +using namespace std; + +int main() +{ + unsigned int n1, n2; + bool good; + do { // read in valid numbers + cout << "Enter two integers between 0 and 999999: "; + cin >> n1 >> n2; + good = n1 <= 999999 && n1 >= 0 && n2 <= 999999 && n2 >= 0; + if (!good) + cout << "The numbers must be between 0 and 999999" << endl; + } while (!good); + + int distance = 0; + int d1, d2; + int no1 = n1; //store original + int no2 = n2; + + /* + * The Hamming distance between 2 decimal numbers + * + * Compare 1's place of both numbers, + * if not equal, the distance increases by 1. + * Move to the next place value and repeat + * we're done when both numbers get to 0 + */ + while (n1 != 0 || n2 != 0) { + d1 = n1 % 10; + d2 = n2 % 10; + if (d1 != d2) + distance++; + n1 /= 10; + n2 /= 10; + } + cout << "Hamming distance between " << no1 << " and " << no2 << " is " << distance << endl; + return 0; +} diff --git a/Q4.cpp b/Q4.cpp new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3-54-g00ecf