Assigned: | Thursday, January 23 |
Due: | Thursday, January 30, 11:59pm |
Points: | 150 |
The purpose of this assignment includes:
to familiarize you with basics of C++;
to help you understand the importance of following the instructions and the requirements of this course;
to allow you to determine whether you are prepared for this course.
Pay attention to following the instructions. Follow the standards. For this assignment, you are allowed to have more than 16 noncomment lines per function.
If you find that you are unable to complete this assignment, then you are not adequately prepared for this course. You are strongly advised to drop this course.
Read the entire assignment before you start writing anything.
You will need to submit one file called average.cpp. Do not use a different name for that file. Notice that the file name contains only lower case letters. Make sure that your file name is exactly average.cpp.
Two common notions of average are as follows.
The mean of n numbers x1, x2, …, xn is (x1 + x2 + … + xn)/n. For example, the mean of 4, 5, 8, 20 and 25 is (4 + 5 + 8 + 20 + 25)/5 = 62/5 = 12.4.
If n is odd, the median of n numbers is the middle number, if the numbers are written in nondescending order (from smallest to largest). For example, the median of 3, 5 and 9 is 5.
Write a complete C++ program called average.cpp that does the following.
First it reads three integers and reports the median of those three integers.
Next, it reads integers until it reads −1. It reports the mean of all of those integers, excluding the −1 at the end.
If the input is
3 9 5 4 5 8 20 25 -1
then your program should say:
The median of (3, 9, 5) is 5. The mean of (4, 5, 8, 20, 25) is 12.40
If the input is
3 6 3 2 4 6 -1
then your program should say:
The median of (3, 6, 3) is 3. The mean of (2, 4, 6) is 4.00.
Create a directory (folder) to hold assignment 1. Download
into that directory. Makefile and dotest help you compile and test your program.
File average.cpp is a template to get you going. Edit it to make your program.
Replace **your name** with your name.
If you use tabs, replace @@ by the spacing between tab stops. For example, if you replace @@ by 4, then you are saying there is a tab stop every 4 characters. If you do not use tabs, replace @@ by none.
If you know Java, you will find C++ familiar. Java was designed to look a lot like C++. Here are some things in C++ that you will want to use.
int x, y, z;
double r;
scanf("%i", &x);
scanf("%i%i%i", &x, &y, &z);
scanf("%i", &x);
printf("The median of %i, %i and %i is %i.\n", &x, &y, &z, &median);writes
The median of 4, 10 and 3 is 4.
printf("%i", x);
If r has type double, then
printf("%0.2lf", r);writes real number r with 2 digits to the right of the decimal point. Use that format for writing means.
x = = y is true if x and y are the same value. There must not be a space between the two = characters.
x != y is true if x and y are not the same value.
x > y is true if x > y.
x < y is true if x < y.
x >= y is true if x ≥ y.
x <= y is true if x ≤ y.
A | | B is true if at least one of A and B is true. For example, expression
4 > 1 || 2 == 3is true. There must not be a space between the two | characters.
A && B is true if A and B are both true. For example, expression
4 > 1 && 2 == 3is false.
if(x > y & y > z) { … } else { … }
if(x > y & y > z) { … }
while(x != -1) { … }
int main() { ... return 0; }
Make sure that you have Makefile and dotest by doing command
lswhich shows the names of the files in the current directory.
Use following commands.
Check the results! Are they correct?
Avoid long lines. Limit lines to about 80 characters.
Indent well throughout. A very poorly indented program will receive a failing grade.
Use margin comments sparingly, if at all. Some students have learned to write comments in the right margin of each line explaining what that line does. Please do not do that.
The last character that your program writes should be an end-of-line character ('\n'). That is true for all of your programming assignments.
The true-body and false-body of every if-statement must be a compound statement. That is, it must be surrounded by braces.
The body of every while-statement must be a compound statement.
Each pair of matching braces must be in the same column, with the right brace immediately below the matching left brace, and with no characters on or to the left of a line segment between the matching braces.
To submit your program, log into xlogin and change to the directory contains your work. Run command
~abrahamsonk/2530/bin/submit 1 average.cppYou should get a reply that the submission was successful. If you don't, something went wrong. Command
~abrahamsonk/2530/bin/submit 1lists the names of the files that you have submitted for assignment 1.
You can do repeated submissions. New submissions will replace old ones.
Late submissions will be accepted for 24 hours after the due date. If you miss a late submission deadline by a microsecond, your work will not be accepted. It is not a microsecond late. It is 24 hours plus a microsecond late.
To ask a question about your program, first submit it, but use assignment name q1. For example, use command
~abrahamsonk/2530/bin/submit q1 average.cppThen send me an email with your question. Do not expect me to read your mind. Tell me what your question(s) are. I will look at the file that you have submitted as q1. If you have another question later, resubmit your new file as assignment q1 and send another email.