CSCI 240 - Assignment 7 - Spring 2007
Points: 100
Due: Monday, March 26, 3 pm
Overview
Write a program that will analyze a set of midterm test scores read from an explicitly opened file. This assignment will show you how to read from a file; you will also use arrays and write a number of functions to accomplish the purposes of the program.
1. The program will first call a function to read the test scores into an array. (See buildArray()).
2. It will then call a function to display the array contents, multiple values per line (See showArray()).
3. Next, it will sort the values in the array in descending order (See sortArray()).
4. And then it will display the sorted contents (showArray() again).
5. Now it will call functions to determine the average and median of the scores (See calcAverage() and calcMedian()). It will display these.
6. Next, it will determine how many scores were in the 90's, how many in the 80's, etc. (See getScores()) It will then display the counts for each category.
Opening and Reading a File
Here are the things you need to do to open a file from your program and read it. You should do these things (except for #1) in your buildArray() function, which is described further below.
1. #include <fstream>
2. Declare an ifstream variable (an "input-file stream")
ifstream inFile;
3. Open the file:
inFile.open("a8.txt"); // if file is in same dir as your pgm.
4. Immediately after the open, check to make sure that the file was really opened and, if it was not, issue an error message and exit:
if (inFile.fail())
{
cout << "Error opening input file";
exit(1);
}
5. Use inFile like cin to read the file. A loop to read and display all the integers in a file of integers is shown below. Your loop will have the same skeleton, but the loop body details will store the input values into an array and not display anything.
inFile >> temp;
while (!inFile.eof())
{
cout << temp; // display the just-read number
inFile >> temp;
}
Note the loop exit condition: it says "while NOT end-of-file on inFile"
6. After all the values have been read, close the file:
infile.close();
The Functions
1. int buildArray(int ar[])
This function will open the file and (if the file opened successfully) will read values into the array passed as the argument. It will count the entries as it places them into the array, and will return the number of entries to the caller. (The caller will store this returned value so it knows how many entries in the array are used, and so it can pass this value to other functions so they will know how much of the array is used.) The array itself will be declared in main() and should be able to hold up to 200 values.
2. void showArray(int ar[], int numElements, int numPerLine)
This function will display the elements of the array, several items per line as specified by the third argument. The second argument tells the function how many items are in the array, so it knows where to stop. In order to display numPerLine numbers, you should only output a newline character when the number of items already printed is a multiple of numPerLine. Hint: the % operator will help here.
3. void sortArray(int ar[], int numElements)
This function will sort the numbers in the array, in descending order, according to the selection sort algorithm explained in lecture. Do not use other sorting algorithms if you think you may need debugging help. We will only debug selection sort.
4. double calcAverage(int ar[], int numElements)
This function will calculate and return the average of the numbers in the array. Don't forget to do a floating-point calculation (i.e. if the average is 72.6, don't return 72).
5. int calcMedian(int ar[], int numElements)
This function will return the middle element of the sorted scores (so it assumes that the array is sorted before it is called). Just use numElements/2 as the subscript of the array for the value you return.
6. void getScores(int ar[], int numElements, int counts[])
This function will go through the sorted array, from the beginning to end.
Note: the counts array must be initialized to all 0's. You know why.
In this way, the function will return, in the array counts, the number of scores in the 90's, in the 80's, etc. The caller will have passed counts, this function fills it in, and then the caller can display each of these counts from the counts array, after this function returns. Clearly, the counts array will be declared in main() and will contain 6 elements.
Notes
1. Data for this assignment can be found here. Click on the link and use your browser to save the file. In IE, use File/Save As and choose Save File As Type: .txt and name it as a8.txt. Alternately, you can copy it and then open Notepad or the Quincy Editor and paste it and then save it to disk. Save it where your program is saved. You can assume that the TA has a copy of a8.txt.
2. Use <iomanip> formatting to display 2 decimal places in averages, and arrange for your displays to be neat, well labeled, and easy to understand and read.
3. Hand in the usual. Don't forget documentation.