SD234 - Unit 3 - Assn B1

2
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) Answer: =START CODE= #include <iostream> using namespace std; int a, b, c;

Transcript of SD234 - Unit 3 - Assn B1

Page 1: SD234 - Unit 3 - Assn B1

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) Answer: =START CODE= #include <iostream> using namespace std; int a, b, c;

Page 2: SD234 - Unit 3 - Assn B1

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) 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).