SD234 - Unit 3 - Assn B

5
Unit 3 – Assignment B 1. Programming Exercise #1 (Chapter 4) Write a C++ program that prompts the user to input a number. The program should then output the number and a message saying whether the number is positive, negative or zero. (5 points) Answer: =START CODE= #include <iostream> #include <string> using namespace std; main () { int inNum; string outStr; cout << "Please enter a #: "; cin >> inNum; if (inNum < 0) { outStr = " is a negative number."; } else if (inNum == 0) { outStr = " is zero."; } else { outStr = " is a positive number."; } cout << inNum << outStr << endl; return 0; } =END CODE= How many runs should you make for the program written in Programming Exercise #1 to verify that it is operating correctly? What data should you input in each of the program runs? (2 points) Answer: 3 Runs total. One to test a negative number, positive number, and a zero. 2. Programming Exercise #2 (Chapter 4) Write a C++ program that prompts the user to input three numbers. The program should then output the numbers in ascending order. (8 points)

Transcript of SD234 - Unit 3 - Assn B

Page 1: SD234 - Unit 3 - Assn B

Unit 3 – Assignment B 1. Programming Exercise #1 (Chapter 4) Write a C++ program that prompts the user to input a number. The program should then output the number and a message saying whether the number is positive, negative or zero. (5 points) Answer: =START CODE= #include <iostream> #include <string> using namespace std; main () { int inNum; string outStr; cout << "Please enter a #: "; cin >> inNum; if (inNum < 0) { outStr = " is a negative number."; } else if (inNum == 0) { outStr = " is zero."; } else { outStr = " is a positive number."; } cout << inNum << outStr << endl; return 0; }

=END CODE= How many runs should you make for the program written in Programming Exercise #1 to verify that it is operating correctly? What data should you input in each of the program runs? (2 points) Answer: 3 Runs total. One to test a negative number, positive number, and a zero. 2. Programming Exercise #2 (Chapter 4) Write a C++ program that prompts the user to input three numbers. The program should then output the numbers in ascending order. (8 points)

Page 2: SD234 - Unit 3 - Assn B

Answer: =START CODE= #include <iostream> using namespace std; int a, b, c; void sortArray() { int arr[3] = {a, b, c}; int t; for (int i=0; i<3; i++) { for (int r=2; r>=i; r--) { if (arr[i] > arr[r]) { t = arr[r]; arr[r] = arr[i]; arr[i] = t; } } } a = arr[0]; b = arr[1]; c = arr[2]; } main () { cout << "Please enter your 1st number: "; cin >> a; cout << "Please enter your 2nd number: "; cin >> b; cout << "Please enter your 3rd number: "; cin >> c; sortArray(); cout << "Your values are now sorted. They are: " << a << ", " << b << ", " << c << endl; return 0; }

=END CODE= 3. Programming Exercise #15 (Chapter 4)

Page 3: SD234 - Unit 3 - Assn B

Write a program that calculates and prints the bill for a cellular telephone company. Use the specifications on page 255 for the details. (15 points) Submit your assignment using the link above (due on Sunday of this unit; 30 points). Answer: =START CODE= #include <iostream> #include <string> using namespace std; main() { //Declare Strings. string acctNum; string servType; //Declare Integers. int regMins = 0; int dayMins = 0; int eveMins = 0; int totMins = 0; //Declare Doubles. double regPrice = 0; double pdayPrice = 0; double pevePrice = 0; double amountDue; //Prompt user to enter the Account Number. cout << "Enter Account Number: "; cin >> acctNum; cout << endl; TryAgain: //Prompt user to enter the Service Type to calcula te total from. cout << "Enter Service Type ('R' = Regular, 'P' = Premium): "; cin >> servType; cout << endl; //If Service Type is Regular, then do... if ((servType == "R") || (servType == "r")) { servType = "Regular"; //Prompt user to enter the total number of minut es. cout << "Enter Total Day & Evening Minutes Used: "; cin >> regMins; //If Service Type is Premium, then do...

Page 4: SD234 - Unit 3 - Assn B

} else if ((servType == "P") || (servType == "p")) { servType = "Premium"; //Prompt the user to input the total number of d aytime minutes. cout << "Enter Total Day (6AM-6PM) Minutes Used: "; cin >> dayMins; //Prompt the user to input the total number of e vening minutes. cout << "Enter Total Evening (6PM-6AM) Minutes U sed: "; cin >> eveMins; } else { //If the Service Type is invalid, kick them back up to select a correct one. cout << "You have entered an incorrect service t ype. Please try again." << endl; goto TryAgain; } //If the service type is Regular, then do... if (servType == "Regular") { regPrice = 10; //Calculate the price if greater than max. if (regMins >= 50) { regPrice = 10 + ((regMins - 50) * .20); } } //If the service type is Premium, then do... if (servType == "Premium") { //Set the default values. pdayPrice = 25; pevePrice = 0; //Calculate the Daytime Minutes Price if greater than max. if (dayMins >= 75) { pdayPrice = 25 + ((dayMins - 75) * .1); } //Calculate the Evening Minutes Price if greater than max. if (eveMins >= 100){ pevePrice = ((eveMins - 100) * .05); } } //Calculate the total amount due and total minutes used. amountDue = (regPrice + pdayPrice + pevePrice); totMins = (regMins + dayMins + eveMins);

Page 5: SD234 - Unit 3 - Assn B

//Output the statement details. cout << endl << "BILLING SUMMARY:" << endl; cout << "=========================" << endl; cout << "Account Number: " << acctNum << endl; cout << "Service Type: " << servType << endl; cout << "Total Minutes Used: " << totMins << endl; cout << "Total Amount Due: $" << amountDue << endl ; cout << "=========================" << endl; return 0; }

=END CODE=