Computer Science 2530
Spring 2020
Programming Assignment 1

Assigned: Thursday, January 23
Due: Thursday, January 30, 11:59pm
Points: 150

Table of Contents

  1. Purpose of this assignment
  2. What you will submit
  3. Background
  4. The assignment
  5. Getting started
  6. Writing in C++
  7. Compiling and running your program on xlogin
  8. Issues to be aware of
  9. Submitting your work
  10. Late submissions
  11. Asking questions

Purpose of This Assignment

The purpose of this assignment includes:

  1. to familiarize you with basics of C++;

  2. to help you understand the importance of following the instructions and the requirements of this course;

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


What You Will Submit

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.


Background

Two common notions of average are as follows.

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

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


The Assignment

Write a complete C++ program called average.cpp that does the following.

  1. First it reads three integers and reports the median of those three integers.

  2. Next, it reads integers until it reads −1. It reports the mean of all of those integers, excluding the −1 at the end.

Example 1

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

Example 2

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.

Getting Started

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.


Writing in C++

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.

Create integer variables x, y and z

  int x, y, z;

Create a real-valued variable r

  double r;

Read an integer and store it into x

  scanf("%i", &x);

Read three integers and store them into x, y and z

  scanf("%i%i%i", &x, &y, &z);

Read an integer and store it into x

  scanf("%i", &x);

Write a string

If x = 4, y = 10, z = 3 and median = 4, then
  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.

Write an integer

  printf("%i", x);

Write a real number

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.

Comparisons and boolean operators

If-statement with else

  if(x > y & y > z)
  {
    …
  }
  else
  {
    …
  }

If-statement without else

  if(x > y & y > z)
  {
    …
  }

While-loops

  while(x != -1)
  {
    …
  }

Main function

  int main()
  {
    ...
    return 0;
  }

Compiling and Running Your Program on Xlogin

Make sure that you have Makefile and dotest by doing command

  ls
which shows the names of the files in the current directory.

Use following commands.

make
Just compile average.cpp and create an executable file called average. If there are errors, they are reported. No news is good news.

make test
Run executable file average (compiling average.cpp first if it needs compiling) on some examples that are provided. This is an example of automated testing. A directory called data containing the tests inputs will be created.

Check the results! Are they correct?

make run
Run executable file average (compiling average.cpp first if it needs compiling). The input is read from the terminal.

make clean
Remove file average. After you do this, the next time you do a make, average.cpp will be recompiled.

Issues to Be Aware of

  1. Avoid long lines. Limit lines to about 80 characters.

  2. Indent well throughout. A very poorly indented program will receive a failing grade.

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

  4. The last character that your program writes should be an end-of-line character ('\n'). That is true for all of your programming assignments.

  5. The true-body and false-body of every if-statement must be a compound statement. That is, it must be surrounded by braces.

  6. The body of every while-statement must be a compound statement.

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


Submitting Your Work

To submit your program, log into xlogin and change to the directory contains your work. Run command

  ~abrahamsonk/2530/bin/submit 1 average.cpp
You should get a reply that the submission was successful. If you don't, something went wrong. Command
  ~abrahamsonk/2530/bin/submit 1
lists 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

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.


Asking Questions

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.cpp
Then 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.