CST116 C++ 6.4 5a pg 126 #1 1. 0 2 3 2 1 7.1 pg 148 #1-6 1. The sign is reversed. it should be <= int_exp1 <= int_exp2 2. There should be 2 equal signs int_exp1 == int_exp2 3. ! should be on the other side int_exp1 != int_exp2 4. Cant compare a string literal using relational quotes char_exp == 'A' 5. This is correct 6. Has to have int_exp1 (on each side of operator) int_exp1 < 2 && int_exp1 > -10 7.2 pg 155 #1 #include #include #include #include using std::cout; using std::endl; using std::string; int main() { int money; int accounts; //take the number of accounts cout << "How many accounts do you have with us today?\n"; std::cin >> accounts; //take the money amount cout << "How much money do you have in your account(s)?\n $"; std::cin >> money; //if x>=25,000 is platinum //if 10,00010,000 but only 1 account is silver //if x<=10,000 is copper if ((money >= 25000) & (accounts == 2)) { cout << "You are a Platinum member!"; } else if ((money >= 25000) & (accounts == 1)) { cout << "You are a Platinum member!"; } else if (((10000 < money) && (money < 25000)) & (accounts > 1)) { cout << "You are a Gold member!"; } else if (((10000 < money) && (money < 25000)) & (accounts == 1)) { cout << "You are a Silver member"; } else if ((money <= 10000) & (accounts == 2)) { cout << "You are a Copper member"; } else if ((money <= 10000) & (accounts == 1)) { cout << "You are a Copper member"; } return 0; } 5b 7.4 pg 161 #1 #include #include using std::endl; using std::cout; int main() { int selection; cout << "Student Grade program\n -Main Menu- \n\n1. Enter Name\n2. Enter test scores\n3. Display test scores\n9. Exit\n\nPlease enter your choice from the list above\n\n"; std::cin >> selection; switch (selection) { case 1 : cout << "\n\nYour choice is: Enter Name?\n\n"; break; case 2: cout << "\n\nYour choice is: Enter Test Scores?\n\n"; break; case 3: cout << "\n\nYour choice is: Display Test Scores?\n\n"; break; case 9: cout << "\n\nYour choice is: to Exit?\n\n"; break; default: cout << "\n\nYour selection is invalid... Try again\n\n"; } } 5c 7.10 pg 168 #2 #include #include #include #include #include using std::cout; using std::endl; using std::string; int main() { int loan; float interest{}; float percent_interest; float sum_of_interest_and_fees; int fee; string response; string agreement; //agreement for margin of acceptance cout << "Just to let you know before we start, \nthe parameters for a loan is between $100 and $1000, \nand the parameters for interest rate is between 1% and 18%, \n\nis that okay?\ntype 'y' for yes and 'n' for no\n\n"; std::cin >> agreement; if (agreement == "y") { cout << "\n\nWe will proceed with the questions, then...\n\n"; } else if (agreement == "n") { cout << "\n\nThat is unfortunate, I cannot continue this transaction, sorry\n\n"; return (0); } else { cout << "Invalid character, try this transaction again\n\n"; return (0); } //enter the loan amount cout << "Enter the loan amount:\n\n"; std::cin >> loan; if (loan >= 100 && loan <= 1000) { cout << "\nAccepted"; } else if (loan <= 100 || loan >= 1000) { cout << "\n\nYou are not elegible for this loan, sorry.\n" << endl; return (0); } //enter the interest amount cout << "\n\nEnter the interest amount:\n\n"; std::cin >> interest; if (interest >= 1 && interest <= 18) { cout << "\nAccepted"; } else if (interest <= 1 || interest >= 18) { cout << "\n\nYou are not elegible for this loan, sorry.\n" << endl; return (0); } cout << "\n\nWe will now calculate your interest and fees"; cout << "\n\n..."; percent_interest = interest / 100; if (loan >= 100 && loan <= 500) { fee = 20; } else if (loan > 500) { fee = 25; } //calculate interest and fees sum_of_interest_and_fees = loan + (loan * percent_interest) + fee; cout << "\n\nHere is a summary of the calculations and data you have given us:"; cout << "\n\nYou are asking for a loan of $" <> response; if (response == "y") { cout << "\n\nThank you for your business.\n\n"; return (0); } else if (response == "n") { cout << "\n\nWell that kind of sucks, I don't know what to tell you.\n\n"; return (0); } else { cout << "\n\nInvalid character, try this transaction again\n\n"; return (0); } } 6a 6.5 pg 126-127 #1-5 1. a += 25; 2. b *= a * 2; 3. b += 1; 4. c %= 5 5. b /= a 8.2 pg 177 #1 1. #include #include #include #include #include using std::cout; using std::endl; using std::string; int main() { int count; int converted_to_even; bool even; bool odd; cout << "Input any number between 1 and 50:\n\n"; std::cin >> count; if (count >= 1 && count <= 50) { cout << "\n\nCounting down the even numbers to 0:\n\n"; } else { cout << "\n\nThis number is not a number between 1 and 50, try again.\n\n"; return (0); } if (count % 2 == 1) { converted_to_even = count - 1; } else { converted_to_even = count; } while (converted_to_even >= 0) { cout << converted_to_even << endl; converted_to_even -= 2; } } 8.3 pg 179 #1 1. #include #include #include #include #include using std::cout; using std::endl; using std::string; int main() { int count; int converted_to_even; cout << "Input any number between 1 and 50:\n\n"; std::cin >> count; if (count >= 1 && count <= 50) { cout << "\n\nCounting down the even numbers to 0:\n\n"; } else do { cout << "\n\nERROR, this number is not between 1 and 50, please enter a number between 1 and 50\n\n"; std::cin >> count; } while (count > 50 || count < 1); if (count % 2 == 1) { converted_to_even = count - 1; } else { converted_to_even = count; } while (converted_to_even >= 0) { cout << converted_to_even << endl; converted_to_even -= 2; } } 6b 8.4 pg 184 #1 1. #include #include #include #include #include using std::cout; using std::endl; using std::string; using namespace std; int main() { int n, i; float num[100], sum = 0.0, average; cout << "How many assignments do you have in one class?\n\n"; cin >> n; for (i = 0; i < n; ++i) { cout << i + 1 << ". Enter Grade: "; cin >> num[i]; sum += num[i]; } average = sum / n; cout << "Your average for each assignment is: " << average << endl; return 0; } 6c 8.10 pg 192 #3 3. #include #include #include #include #include using std::cout; using std::endl; using std::string; using namespace std; int main() { int n, t1 = 0, t2 = 1, next_term = 0; cout << "What is the number you want to count up to in Fibonacci's sequence?\n\n"; cin >> n; cout << "Here is the sequence: " << t1 << ", " << t2 << ", "; next_term = t1 + t2; while (next_term <= n) { cout << next_term << ", "; t1 = t2; t2 = next_term; next_term = t1 + t2; } return 0; }