Topics covered on Monday, 2010-08-16
Summary
- Looping constructs: while and for
- Increment operator: ++
- Conditional operator: condition ? expression1 : expression2
Compute Average Program, version 1
1 // average1.cpp
2 //
3 // Get numbers (after prompting for how many) and print average.
4 // Use while loops and simple if test for formatting output.
5 //
6 // Copyright 2010, Raj Mathur
7 //
8 #include <iostream>
9 #include <cstdlib>
10 using namespace std;
11
12 int main(int argc, char *argv[])
13 {
14 int
15 count = 0;
16 float
17 sum = 0;
18 float
19 average;
20 // Get count
21 cout << "How many numbers do you want the average for? ";
22 cin >> count;
23 // Allocate space
24 int
25 numbers[count];
26 // Read the numbers
27 int
28 i = 0;
29 while( i < count )
30 {
31 cout << "Next number: ";
32 cin >> numbers[i];
33 sum += numbers[i];
34 i++;
35 }
36 // Compute average
37 average = sum / count;
38 // Format and print
39 cout << "The average of (";
40 i = 0;
41 while( i < count )
42 {
43 if( i != 0 )
44 {
45 cout << ", ";
46 }
47 cout << numbers[i++];
48 }
49 cout << ") is ";
50 cout << average << endl;
51 exit(0);
52 }
Computer Average Program, version 2
1 // average2.cpp
2 //
3 // Get numbers (after prompting for how many) and print average.
4 // Use for loops and conditional operator.
5 //
6 // Copyright 2010, Raj Mathur
7 //
8 #include <iostream>
9 #include <cstdlib>
10 using namespace std;
11
12 int main(int argc, char *argv[])
13 {
14 int
15 count = 0;
16 int
17 i;
18 float
19 sum = 0;
20 float
21 average;
22 // Get count
23 cout << "How many numbers do you want the average for? ";
24 cin >> count;
25 // Allocate space
26 int
27 numbers[count];
28 // Read the numbers
29 for( i = 0; i < count; i++ )
30 {
31 cout << "Next number: ";
32 cin >> numbers[i];
33 sum += numbers[i];
34 }
35 // Compute average
36 average = sum / count;
37 // Format and print
38 cout << "The average of (";
39 for( i = 0; i < count; i++ )
40 {
41 cout << (i != 0 ? ", " : "") << numbers[i];
42 }
43 cout << ") is ";
44 cout << average << endl;
45 exit(0);
46 }
Computer Average Program, version 3
1 // average3.cpp
2 //
3 // Get numbers (after prompting for how many) and print average.
4 // Use for loops and tricks to print appropriate separator without the
5 // use of conditions
6 //
7 // Copyright 2010, Raj Mathur
8 //
9 #include <iostream>
10 #include <cstdlib>
11 using namespace std;
12
13 int main(int argc, char *argv[])
14 {
15 int
16 count = 0;
17 int
18 i;
19 float
20 sum = 0;
21 float
22 average;
23 // Get count
24 cout << "How many numbers do you want the average for? ";
25 cin >> count;
26 // Allocate space
27 int
28 numbers[count];
29 // Read the numbers
30 for( i = 0; i < count; i++ )
31 {
32 cout << "Next number: ";
33 cin >> numbers[i];
34 sum += numbers[i];
35 }
36 // Compute average
37 average = sum / count;
38 // Format and print
39 cout << "The average of (";
40 const char
41 *prefix = "";
42 for( i = 0; i < count; i++ )
43 {
44 cout << prefix << numbers[i];
45 prefix = ", ";
46 }
47 cout << ") is ";
48 cout << average << endl;
49 exit(0);
50 }
Assignments
Review
Read and solve the attached PDF. Please put your solutions in a single text file and mail it to me as an attachment.
- assn1.pdf: Assignment 1 for Monday, 2010-08-16
Program
Modify your calculator program to do the following:
- Handle (a)ddition, (s)ubtraction, mul(t)iplication and (d)ivision.
- Handle invalid inputs.
- Run in a loop and ask the user if she wants to do another operation before exiting.
A sample run of the program would now look
exactly like this. Entries in
bold are what the user enters, and formatting is important:
Do you want to (a)dd, (s)ubtract, mul(t)iply or (d)ivide?
q
Sorry, I don't understand the 'q' operation. Try again.
Do you want to (a)dd, (s)ubtract, mul(t)iply or (d)ivide?
t
Please enter the first number:
3
Please enter the second number:
5
The result is: 5
That was fun! Would you like to do another computation (y/n)?
y
Do you want to (a)dd, (s)ubtract, mul(t)iply or (d)ivide?
s
Please enter the first number:
23
Please enter the second number:
42
The result is: -19
That was fun! Would you like to do another computation (y/n)?
n
Until later then. Bye!