Tyler Taormina Lab 4 Text File CST CST116 October 20, 2021 7a 6.8 Exercises pp 132-133 5 pts #1-9 Submit: value of “a” after the expression is executed ________________________________________________________________________________________________________________________ 1. a = sqrt( 9.0); a = 3 2. a = sqrt (-9.0); a = nan 3. a = pow( 2.0, 5 ); a = 32 4. a = pow ( 2.0, -2 ); a = 0.25 5. a = ceil( 5.1 ); a = 6 6. a = ceil( 5.9 ); a = 6 7. a = floor( 5.1 ); a = 5 8. a = floor( 5.9 ); a = 5 9. a = sqrt( pow( abs( -2 ), 4 ) ); a = 4 ________________________________________________________________________________________________________________________ 9.3 Exercises p 207 10 pts #1 Submit: code & run Total: 15 pts #include #include // needed for square roots using namespace std; int avg_score (float& num1, float& num2, float& num3); int main() { float value1, value2, value3, average = 0.0; cout << "Enter 3 values that you want averaged." << endl; cout << "Enter value 1: " << endl; cin >> value1; cout << "Enter value 2: " << endl; cin >> value2; cout << "Enter value 3: " << endl; cin >> value3; average = avg_score(value1, value2, value3); cout << "Your average score is: " << average << endl; return 0; } int avg_score (float& num1, float& num2, float& num3) { int avg = 0; avg = ( num1 + num2 + num3 ) / 3; return avg; } RUN: C:\Users\Till\CLionProjects\gitDemo\cmake-build-debug\gitDemo.exe Enter 3 values that you want averaged. Enter value 1: 20 Enter value 2: 19 Enter value 3: 21 Your average score is: 20 Process finished with exit code 0 ________________________________________________________________________________________________________________________ 7b 9.4 Learn by Doing Exercises p 214 10 pts #1 Submit: code & runs CODE: #include using namespace std; void GetInput (float& salary, int& years_service); void CalcRaise (float& salary, int years_service); int CalcBonus (int years_service); void PrintCalc (int years_service, float salary, int bonus); int main() { int years_service = 0, bonus = 0; float salary = 0.0; GetInput(salary, years_service); CalcRaise(salary, years_service); bonus = CalcBonus(years_service); PrintCalc(years_service, salary, bonus); return 0; } void GetInput (float& salary, int& years_service) { cout << "Enter salary: "; cin >> salary; cout << "Enter years_service: "; cin >> years_service; } void CalcRaise (float& salary, int years_service) { if (years_service > 10) salary += (salary * .10); else if (years_service >= 5) salary += (salary * .05); else salary += (salary * .02); } int CalcBonus (int years_service) { int bonus = 0; int bonus_years = 0; bonus_years = years_service / 2; bonus = bonus_years * 500; return bonus; } void PrintCalc (int years_service, float salary, int bonus) { cout << "Employee years of service is " << years_service << endl; cout << "Your current salary is $" << salary << endl; cout << "The bonus you will receive is $" << bonus << endl; } RUN: C:\Users\Till\CLionProjects\gitDemo\cmake-build-debug\gitDemo.exe Enter salary:20000 Enter years_service:10 Employee years of service is 10 Your current salary is $21000 The bonus you will receive is $2500 Process finished with exit code 0 ________________________________________________________________________________________________________________________ 7c 9.5 Learn by Doing Exercises p 216 10 pts #2 Submit: code & run CODE: #include #include using namespace std; void getNum (int&, int&, int&); void printTime (int&, int&, int&, int form = 1); int main() { int hour = 0, min = 0, sec = 0; int format; getNum(hour, min, sec); printTime(hour, min, sec, format); return 0; } void getNum(int& hour, int& min, int& seconds) { cout << "Enter hour for the current time: "; cin >> hour; cout << "Enter minute of the current time: "; cin >> min; cout << "Enter seconds for the current time: "; cin >> seconds; } void printTime(int& hour, int& min, int& seconds, int form) { int time = 0; cout << "Please enter your choice of clock formatting." << endl; cout << "Enter 1 for standard formatting." << endl; cout << "Enter 2 for 24 hour formatting." << endl; cout << "Enter 3 for military formatting." << endl; cin >> form; switch (form) { // standard time case 1: cout << "Enter 1 for AM or 2 for PM: "; cin >> time; cout << "Standard time: " << endl; switch (time) { case 1: cout << hour << ":" << setw(2) << setfill('0') << min << ":"; cout << setw(2) << setfill('0') << seconds << " AM"; break; case 2: cout << hour << ":" << setw(2) << setfill('0') << min << ":"; cout << setw(2) << setfill('0') << seconds << " PM"; break; default: cout << "INVALID ENTRY. TRY AGAIN." << endl; main(); } break; // 24 hour formatting case 2: cout << "Enter 1 for AM or 2 for PM: "; cin >> time; cout << "24-hour Formatting: " << endl; switch (time) { case 1: cout << hour << ":" << setw(2) << setfill('0') << min << ":"; cout << setw(2) << setfill('0') << seconds; break; case 2: hour += 12; cout << hour << ":" << setw(2) << setfill('0') << min << ":"; cout << setw(2) << setfill('0') << seconds; break; default: cout << "INVALID ENTRY. TRY AGAIN." << endl; main(); } break; // military time case 3: cout << "Enter 1 for AM or 2 for PM: "; cin >> time; cout << "Military Formatting: " << endl; switch (time) { case 1: cout << hour << setw(2) << setfill('0') << min; cout << setw(2) << setfill('0') << seconds; break; case 2: hour += 12; cout << hour << setw(2) << setfill('0') << min; cout << setw(2) << setfill('0') << seconds; break; default: cout << "INVALID ENTRY. TRY AGAIN." << endl; main(); } break; default: cout << "INVALID ENTRY. TRY AGAIN." << endl; main (); break; } } RUN: C:\Users\Till\CLionProjects\gitDemo\cmake-build-debug\gitDemo.exe Enter hour for the current time:2 Enter minute of the current time:2 Enter seconds for the current time:2 Please enter your choice of clock formatting. Enter 1 for standard formatting. Enter 2 for 24 hour formatting. Enter 3 for military formatting. 2 Enter 1 for AM or 2 for PM:2 24-hour Formatting: 14:02:02 Process finished with exit code 0 ________________________________________________________________________________________________________________________ 8a 9.13 Debugging Exercises pp 226-229 10 pts #1 Submit: code & runs CODE: #include using std::cout; using std::cin; using std::endl; const int DAYS_PER_YEAR = 365; int GetAge ( ); int CalcDays ( int age ); void PrintResults ( int& age, int days ); int main ( ) { int age = 0; int days = 0; // Breakpoint 1 // Put breakpoint on the following line age = GetAge ( ); days = CalcDays ( age ); // Breakpoint 2 // Put breakpoint on the following line PrintResults ( age, days ); return 0; } int GetAge ( ) { int age; cout << "Please enter your age: "; cin >> age; return age; } int CalcDays ( int years ) { int days; days = years * DAYS_PER_YEAR; return days; } void PrintResults ( int& age, int days ) { cout << age << "! Boy are you old!\n"; cout << "Did you know that you are at least " << days << " days old?\n\n"; } RUN: C:\Users\Till\CLionProjects\gitDemo\cmake-build-debug\gitDemo.exe Please enter your age:21 21! Boy are you old! Did you know that you are at least 7665 days old? Process finished with exit code 0 ________________________________________________________________________________________________________________________ 8b 9.14 Programming Exercises pp 229 10 pts #1 Submit: code & run CODE: #include using namespace std; void draw (int w, int h) { int i; cout << char(218); for (int i = 0; i <= (w); i++) cout << char(196); cout << char(191) << endl; for (int i = 0; i <= (h); i++) { cout << char(179); for (int i = 0; i <= (w); i++) cout << " "; cout << char(179) << endl; } cout << char(192); for (int i = 0; i <= (w); i++) cout << char(196); cout << char(217); } int main() { int h = 0, w = 0; cout << "Enter a height for your rectangle: "; cin >> h; cout << "Enter a width for your rectangle: "; cin >> w; cout << "Rectangle: " << endl; draw(w,h); return 0; } RUN: C:\Users\Till\CLionProjects\gitDemo\cmake-build-debug\gitDemo.exe Enter a height for your rectangle:2 Enter a width for your rectangle:10 Rectangle: ┌─────────┐ │ │ │ │ └─────────┘ Process finished with exit code 0 Total: 55 pts