//~~~ 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.0 //2. a = sqrt(-9.0); a = -nan(ind) Does not work. Cannot take the squart root of a negative number. //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.0 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CODE FOR PROBLEM #1 ON PAGE 207 IS BELOW //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //#include // //using std::cout; //using std::cin; //using std::endl; // //float GetAvg(); // //int main() //{ // float average = GetAvg(); // // cout << "\n\n\t\tThe average is: " << average << endl; // // return 0; //} // //float GetAvg() //{ // float val1 = 0; // float val2 = 0; // float val3 = 0; // float avg = 0; // // cout << "\n\tEnter value 1: "; // cin >> val1; // // cout << "\n\tEnter value 2: "; // cin >> val2; // // cout << "\n\tEnter value 3: "; // cin >> val3; // // avg = (float) (val1 + val2 + val3) / 3; // // return avg; //} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CODE FOR PROBLEM #1 ON PAGE 214 IS BELOW //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //#include // //using namespace std; // //void GetInput(float&, int&); //void CalcRaise(float&, int&); //int CalcBonus(int); //void PrintCalculations(int, float, int); // //int main() //{ // int years_service = 0; // float salary = 0.0; // // GetInput(salary, years_service); // CalcRaise(salary, years_service); // CalcBonus(years_service); // // int bonus = CalcBonus(years_service); // // PrintCalculations(years_service, salary, bonus); // // return 0; //} // //void GetInput(float& salary, int& years_service) //{ // cout << "Enter the employee's salary: "; // cin >> salary; // // while (salary <= 0) // { // cout << "Enter the employee's salary: "; // cin >> salary; // } // // cout << "\nEnter the employee's years of service: "; // cin >> years_service; // // while (years_service <= 0) // { // cout << "Enter the employee's years of service: "; // cin >> years_service; // } //} // //void CalcRaise(float& salary, int& years_service) //{ // if (years_service > 10) // salary += (salary * 0.1); // // else if (years_service >= 5) // salary += (salary * 0.05); // // else // salary += (salary * 0.02); //} // //int CalcBonus(int years_service) //{ // int bonus = 0; // // if (years_service % 2 == 0) // { // bonus = (years_service / 2) * 500; // } // // else // { // years_service--; // bonus = (years_service / 2) * 500; // } // // return bonus; //} // //void PrintCalculations(int years_service, float salary, int bonus) //{ // cout << "\n\n\tEmployee's years of service: " << years_service; // cout << "\n\tNew salary: " << salary; // cout << "\n\tBonus: " << bonus << "\n\n" << endl; //} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CODE FOR PROBLEM #1 ON PAGE 216 IS BELOW //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //#include //#include // //using namespace std; // //void GetInput(int&, int&, float&); //void DisplayTime(int, int, float, int); // //int main() //{ // int hours = 0; // int minutes = 0; // float seconds = 0.0; // int format = 0; // // GetInput(hours, minutes, seconds); // // do // { // cout << "\n\n\t\tTime output formats"; // cout << "\n\t1) 12hr"; // cout << "\n\t2) 24hr"; // cout << "\n\t3) Military"; // cout << "\n\n\t\tChoose format: "; // cin >> format; // } // while (format != 1 && format != 2 && format != 3); // // cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; // // DisplayTime(hours, minutes, seconds, format); // // return 0; //} // //void GetInput(int& hours, int& minutes, float& seconds) //{ // cout << "\n\tEnter Hours (0 - 23): "; // cin >> hours; // // cout << "\n\tEnter minutes (0 - 59): "; // cin >> minutes; // // cout << "\n\tEnter seconds (0 - 59): "; // cin >> seconds; //} // //void DisplayTime(int hours, int minutes, float seconds, int format) //{ // switch (format) // { // case 1: //12hr format // if (hours > 12) // { // hours = hours - 12; // cout << "\n\tThe time is "; // cout << hours << ":" << minutes << ":" << seconds << "PM" << endl; // } // else if (hours == 12) // { // cout << "\n\tThe time is "; // cout << hours << ":" << minutes << ":" << seconds << "PM" << endl; // } // else // { // cout << "\n\tThe time is "; // cout << hours << ":" << minutes << ":" << seconds << "AM" << endl; // } // break; // // case 2: //24hr format // cout << "\n\tThe time is "; // cout << hours << ":" << minutes << ":" << seconds << endl; // break; // // case 3: //military time // cout << "\n\tThe time is "; // cout << setfill('0') << setw(2) << hours << setw(2) << minutes << ":" << setw(2) << seconds << endl; // break; // // default: //invalid menu choice // cout << "ERROR: Invalid menu choice"; // } //} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CODE FOR DEBUGGING EXERCISE 9.13 ON PAGES 226 - 229 IS BELOW //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /******************************************************************** * File: Chapter 9 Debug.cpp * * General Instructions: Complete each step before proceeding to the * next. * * Debugging Exercise 1 * * 1) Insert a breakpoint on the lines indicated in the code. * 2) Run to Breakpoint 1. * 3) Place a watch on age and days. * 4) Add another watch using &age for the name. This will display * the address of age. * 5) Write down the address of age. * 6) Step Into the code for the function GetAge. * 7) The execution continues to the function header for GetAge. * 8) Step into one more time. * 9) Why did the address of age and value change? * ANSWER: A completely different and seperate variable named "age" was created. * There was no call in the function to any variables from the main function. * 10) Step over the cout and cin statements. * 11) Verify the value entered is stored properly in age. * 12) Step into until the flow returns to main. * 13) Step over one more time. * 14) Why didn't the value entered get transferred back to main? * ANSWER: It involved a different variable. * 15) Stop debugging and fix the error. * 16) Run to Breakpoint 1. * 17) Step over the function call to GetAge. * 18) Verify that the value entered was returned and stored * correctly from GetAge. * 19) Stop debugging. * * Debugging Exercise 2 * * 1) Run to Breakpoint 1. * 2) Step over the call to GetAge. * 3) Step into CalcDays. * 4) Step into one more time so that the current line is the * calculation. * 5) Why is age greyed out in your watch window? * ANSWER: "age" is currently unchagable and utilized elsewhere (not within scope). * 6) Stop debugging. * * Debugging Exercise 3 * * 1) Run to Breakpoint 2. * 2) When asked, enter the value of 20 for your age. * 3) Verify that the variable age is 20 and the variable days * is 7300. * 4) Step into the PrintResults function. * 5) Age is 7300? Not even Ralph is that old. * 6) Why did the values for both variables change? * ANSWER: Call by value happened, new addresses were created for those same names. * 7) Stop debugging and fix the error. * * Debugging Exercise 4 * * 1) Run to Breakpoint 2. * 2) Display your Call Stack window. * 3) View the contents of the window and notice that the top * function on the stack is main. * 4) Step into the PrintResults function. * 5) Notice that the call stack now shows PrintResults on top of * the stack. ********************************************************************/ //#include // //using namespace std; // //const int DAYS_PER_YEAR = 365; // //int GetAge(int&); //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 // GetAge(age); // 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"; //} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CODE FOR PROBLEM #1 ON PAGE 229 IS BELOW //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //#include //#include // //using namespace std; // //void GetInput(int&, int&); //void DrawBox(int, int); //void Welcome(); // //int main() //{ // int width = 0; // int height = 0; // // Welcome(); // // GetInput(width, height); // DrawBox(width, height); // // return 0; // //} // //void Welcome() //{ // cout << "\n\t\t" << char(218) << " - - - - - - - - - " << char(191); // cout << "\n\t\t Simple box maker"; // cout << "\n\t\t" << char(192) << " - - - - - - - - - " << char(217) << "\n"; //} // //void GetInput(int& width, int& height) //{ // cout << "\n\tEnter desired width: "; // cin >> width; // // cout << "\n\tEnter desired height: "; // cin >> height; //} // //void DrawBox(int width, int height) //{ // int count = height - 2; // // //upper left corner, top, upper right corner // cout << char(218) << setw(width - 1) << setfill(char(196)) << char(191) << "\n"; // // //left and right sides // while (count > 0) // { // cout << char(179) << setw(width - 1) << setfill(char(255)) << char(179) << "\n"; // count--; // } // // //lower left corner, bottom, lower right corner // cout << char(192) << setw(width - 1) << setfill(char(196)) << char(217) << "\n" << endl; //}