Problem 1: /* Name CS201 HW3 Problem 1 */ #include using namespace std; int main() { for(int i = 1; i < 8; i++){ for(int j = 1; j < 8; j++){ if( j == i || j == (8 - i)) cout << "x"; else cout << " "; } cout << endl; } return 0; } /* x x x x x x x x x x x x x Press any key to continue */ Problem 2: /* Name CS201 HW3 Problem 2 */ #include using namespace std; int main() { int n, inputNumber, firstLargest, secondLargest; do{ // handle unreasonable input cout << "How many integers will you be entering? "; cin >> n; if (n > 50) cout << endl << "Please enter a number less then 50." << endl; else if (n < 2) cout << "You need at least 2 integers." << endl; } while ((n > 50) || (n < 1)); for (int i=1; i<=n; i++){ cout <<"Number " << i << " : "; cin >> inputNumber; cout << endl; if (inputNumber >= firstLargest){ secondLargest = firstLargest; firstLargest = inputNumber; } else if (inputNumber >= secondLargest) secondLargest = inputNumber; } cout << "The two largest numbers: " << firstLargest << " and " << secondLargest << "." << endl; return 0; } /* How many integers will you be entering? 4 Number 1 : 45 Number 2 : -4 Number 3 : 23 Number 4 : 32 The two largest numbers: 45 and 32. Press any key to continue */ Problem 3: /* Name CS201 HW3 Problem 3 */ #include using namespace std; int main() { int x, y; do{ cout << "Enter the number of columms: "; cin >> x; if (x > 50) cout << endl << "Please enter a number less then 50." << endl; else if (x < 1) cout << "It needs to be at least 1." << endl; } while ((x > 50) || (x < 1)); do{ cout << "Enter the number of rows: "; cin >> y; if (y > 50) cout << endl << "Please enter a number less then 50, I'm tired" << endl; else if (y < 1) cout << "It needs to be at least 1." << endl; } while ((y > 50) || (y < 1)); for (int i=0; i < y; i++){ for (int z=0; z < x; z++){ cout << "*"; } cout << endl; } return 0; } /* Enter the number of columms: -4 It needs to be at least 1. Enter the number of columms: 8 Enter the number of rows: 10 ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** Press any key to continue */ Problem 4: /* Name CS201 HW3 Problem 4 */ #include using namespace std; int main() { int x; do{ cout << "Enter a positive integer: "; cin >> x; if (x < 0) cout << "I said a positive integer." << endl; } while (x < 0); cout << endl << x << ", "; while (x != 1){ if (x != 1 && (x % 2) == 0){ x = x / 2; cout << x << ", "; } if (x != 1 && (x % 2) == 1){ x = (3*x) + 1; cout << x << ", "; } } cout << endl; return 0; } /* Enter a positive integer: 23 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, Press any key to continue Enter a positive integer: 48 48, 24, 12, 6, 3, 10, 5, 16, 8, 4, 2, 1, Press any key to continue */ Problem 5: /* Name CS201 HW3 Problem 5 */ #include using namespace std; int main() { int x; do{ do{ cout << "Enter a positive integer from one to nine, zero to quit: "; cin >> x; if (x < 0 || x > 9) cout << "\a Try again" << endl; } while (x < 0 || x > 9); switch (x){ case 1: cout << "one" << endl; break; case 2: cout << "two" << endl; break; case 3: cout << "three" << endl; break; case 4: cout << "four" << endl; break; case 5: cout << "five" << endl; break; case 6: cout << "six" << endl; break; case 7: cout << "seven" << endl; break; case 8: cout << "eight" << endl; break; case 9: cout << "nine" << endl; break; default: cout << "Drunk, fix later" << endl; } } while (x != 0); // sentinel input 0 to terminate return 0; } /* Enter a positive integer from one to nine, zero to quit: -9 Try again Enter a positive integer from one to nine, zero to quit: 1 one Enter a positive integer from one to nine, zero to quit: 3 three Enter a positive integer from one to nine, zero to quit: 2 two Enter a positive integer from one to nine, zero to quit: 5 five Enter a positive integer from one to nine, zero to quit: 6 six Enter a positive integer from one to nine, zero to quit: 4 four Enter a positive integer from one to nine, zero to quit: 9 nine Enter a positive integer from one to nine, zero to quit: 7 seven Enter a positive integer from one to nine, zero to quit: 8 eight Enter a positive integer from one to nine, zero to quit: 0 Press any key to continue */