Tyler Taormina cst116 Module 3 Lab 3 --------------------------------------------------------------------------------------------------------------------------------------------------- 5A) 6.4 exercises page 126 #1 submit ouput 1) The output for the code provided in the text book would be... 0 2 3 2 1 7.1 exercises page 148 #1-6 submit corrected conditions 1) int_expl >= int_exp2; 2) int_expl == int_exp2; 3) int_exp1 != int_exp2; 4) char_exp == 'A'; 5) int_exp1 > char_exp; illegal because we are attempting to compare an integer and a character. 6) int_exp1 < 2 && int_exp1 > -10; 7.2 exercises page 155 #1 submit code & run 1) if (a > 0) cout << "a is greater than 0" << endl; ----------------------------------------------------------------------------------------------------------------------------------------------------- 5B) 7.4 exercises page 161 #1 submit code & run 1) #include #include using namespace std; int main() { int user_choice; cout << "\t\tStudent Grade Program" << endl; cout << "\t\t - Main Menu -" << endl; cout << "1. Enter Name" << endl; cout << "2. Enter test scores" << endl; cout << "3. Display test scores" << endl; cout << "9. Exit" << endl; cout << "Please enter your choice from the list above: "; cin >> user_choice; switch (user_choice) { case 1: cout << "You chose to Enter your name." << endl; break; case 2: cout << "You chose to Enter test scores." << endl; break; case 3: cout << "You chose to display test scores." << endl; break; case 9: cout << "You chose to exit." << endl; break; } return 0; } RUN: Student Grade Program - Main Menu - 1. Enter Name 2. Enter test scores 3. Display test scores 9. Exit Please enter your choice from the list above: 1 You chose to Enter your name. C:\Users\Till\source\repos\hw_practice\Debug\hw_practice.exe (process 7824) exited with code 0. Press any key to close this window . . . 5C) 7.10 exercises page 168 #2 submit code & run 2) sudo code ----------------- intialize loan fee for (little loan) intialize loan fee for (big loan) input loan amount check to make sure if loan amount is within range input interest rate check to make sure interest rate is in range calculate interest calculate total cost including interest and fee display interest amount on loan display requested loan amount display interest rate display total cost with interest and fee code --------------------------------------------------------- #include #include using namespace std; int main() { int fee_1 = 20; int fee_2 = 25; int loan_amount, interest_rate; double interest_dec, interest_amount , total_cost = 0; std::cout << "Please enter a loan amount request: "; cin >> loan_amount; if (loan_amount >= 100 && loan_amount <= 1000) { std::cout << "Please enter an interest rate: "; cin >> interest_rate; if (interest_rate >= 1 && interest_rate <= 18) { if (loan_amount > 500) { total_cost += fee_2; } else { total_cost += fee_1; } interest_dec = interest_rate * .01; interest_amount = interest_dec * loan_amount; total_cost += interest_amount + loan_amount; std::cout << "Interest paid on this loan is: $" << interest_amount << endl; std::cout << "Requested Loan: $" << loan_amount << endl; std::cout << "Interest rate: " << interest_rate << "%" << endl; std::cout << "Total cost including all fees for this loan will be: $" << total_cost << endl; return 0; } else { std::cout << "Error. Invalid interest rate. Please enter an interest rate between 1 and 18." << endl; return 0; } } else { std::cout << "Error. Invalid loan amount. Please enter a loan amount between $100 and $1000." << endl; return 0; } return 0; } RUN: Please enter a loan amount request: 500 Please enter an interest rate: 5 Interest paid on this loan is: $25 Requested Loan: $500 Interest rate: 5% Total cost including all fees for this loan will be: $545 C:\Users\Till\source\repos\hw_practice\Debug\hw_practice.exe (process 14452) exited with code 0. Press any key to close this window . . . 6A) 6.5 Exercises pg 126-127 #1-5 1.) a += 25; 2.) b *= 2 * a; 3.) b++; 4.) c %= 5; 5.) b /= a ---------------------------------------------------------------------------------------------- 8.2 Pg 177 #1 1. #include #include using namespace std; int main() { int number; cout << "Enter a number between 1 and 50: "; cin >> number; if (number % 2 == 0) { while (number >= 0) { cout << number << endl; number -= 2; } } else { cout << number << endl; number -= 1; while (number >= 0) { cout << number << endl; number -= 2; } } return 0; } RUN: Enter a number between 1 and 50: 13 13 12 10 8 6 4 2 0 --------------------------------------------------------------------------------------------------- 8.3 pg 179 #1 using do-while statements 1. Submit code and run #include #include using namespace std; int main() { int number; cout << "Enter a number between 1 and 50: "; cin >> number; do { if (number % 2 == 0) { cout << number << endl; number -= 2; } else { cout << number << endl; number -= 1; while (number >= 0) { cout << number << endl; number -= 2; } } } while (number >= 0); return 0; } RUN: Enter a number between 1 and 50: 25 25 24 22 20 18 16 14 12 10 8 6 4 2 0 C:\Users\Till\source\repos\hw_practice\Debug\hw_practice.exe (process 21168) exited with code 0. Press any key to close this window . . . ------------------------------------------------------------------------------------------------------------- 8.4 Learn by doing exercise Pg. 184 #1 #include #include using namespace std; int main() { int num_assignments, ctr; float score, avg, score_total = 0.0; cout << "Please enter the total number of assignments: "; cin >> num_assignments; ctr = num_assignments; for (ctr; ctr > 0; ctr--) { cout << "Please enter score: "; cin >> score; score_total += score; } avg = score_total / num_assignments; cout << "Your Average score for assignments is: " << avg << endl; return 0; } RUN: Please enter the total number of assignments: 5 Please enter score: 10 Please enter score: 9 Please enter score: 9 Please enter score: 10 Please enter score: 10 Your Average score for assignments is: 9.6 C:\Users\Till\source\repos\hw_practice\Debug\hw_practice.exe (process 21132) exited with code 0. Press any key to close this window . . . ------------------------------------------------------------------------------------------------------------------------------ 8.10 Programming Exercises Pg 192 #3 FIBONACCI SEQUENCE #include using namespace std; int main() { int limit, term1 = 0, term2 = 1, nextTerm = 0; cout << "Enter the limit for the fibonacci sequence: "; cin >> limit; cout << "Fibonacci Sequence: " << term1 << ", " << term2 << ", "; nextTerm = term1 + term2; while (nextTerm <= limit) { cout << nextTerm << ", "; term1 = term2; term2 = nextTerm; nextTerm = term1 + term2; } return 0; } RUN: Enter the limit for the fibonacci sequence: 26 Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, C:\Users\Till\source\repos\hw_practice\Debug\hw_practice.exe (process 19324) exited with code 0. Press any key to close this window . . .