Programming Assignment

You will be writing a program to read student scores from the keyboard, save them in an array, and compute some basic statistics about the scores. Your program must

  1. Prompt the user to enter student scores
  2. Compute the maximum, minimum, and average scores
  3. Display the score statistics to the screen
The max() function is provided. You must implement min() and average().

Here is an example compilation and execution of the program:

   linux3[1]% g++ -Wall -ansi lab2.cpp -o lab2
   linux3[1]% ./lab2
   Enter a score (-1 when done): 80
   Enter a score (-1 when done): 72
   Enter a score (-1 when done): 64
   Enter a score (-1 when done): 91
   Enter a score (-1 when done): 97
   Enter a score (-1 when done): -1
   Max score is 97
   Min score is 64
   Average is 80.8

To get you started, a template of the program is provided below. You can copy and paste the template into a text editor such as Emacs. Alternatively, you may copy the file to your working directory from Prof. Marron's public folder on GL:

   cp /afs/ .

To complete the program, you must:

  1. Add code to prompt the user and read scores from the keyboard; the user should enter a negative value when done entering scores.
  2. Write a min() function to compute the minimum value in an integer array.
  3. Write an average() function to compute the average value of an integer array. Remember that the average is a floating point number, and so you must ensure that floating-point division is used in the computation of the average.
  4. Add calls to the min() and average() functions in main().

#include <iostream> using namespace std; const int NUM_STUDENTS = 25; // maximum number of scores // Function prototypes - do not change! int max(int data[], int dataLen); int min(int data[], int dataLen); float average(int data[], int dataLen); int main() { int scores[NUM_STUDENTS] = {0}; int numScores = 0; int inputValue; ///////////////////////////////////////////////////// // CODE TO READ SCORES FROM THE KEYBOARD GOES HERE // ///////////////////////////////////////////////////// int maxVal = max(scores, numScores); ////////////////////////////////////////// // CALLS TO min() AND average() GO HERE // ////////////////////////////////////////// cout << "Max score is " << maxVal << endl; cout << "Min score is " << minVal << endl; cout << "Average is " << avgVal << endl; return 0; } // max() - computes maximum value in an int array // Assumes data[] contains at least one element int max(int data[], int dataLen) { int currentMax = data[0]; for (int i = 1; i < dataLen; i++) { if (data[i] > currentMax) { currentMax = data[i]; } } return currentMax; } //////////////////////////////////////////////////// // IMPLEMENTATIONS OF min() AND average() GO HERE // ////////////////////////////////////////////////////