SD234 - Unit 3 - Assn B1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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 = "
} else if (inNum
outStr = "
} else {
outStr = "
}

is a negative number.";
== 0) {
is zero.";
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;

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.
b << ", " << c << endl;

They are: " << a << ", " <<

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

You might also like