| CSCI 240 | Fall 2025 | 
For this assignment, write a program that will read two sets of data representing a series of numeric measurements. The program will display the data, first unsorted and then sorted, and then compute and display various statistics about each of the data sets. In addition, the program will compute and display the differences between the statistics for the two sets of data.
The two sets of data will be needed for later processing (calculating the statistics), so they will be stored in a set of arrays.
For the assignment, declare two arrays, each of which can hold a maximum of 25 elements:
The cpp file that is submitted for grading must be named assign7.cpp.
Declare the two arrays, two integer variables to hold the number of values in each array, and two string variables to hold the names of the input files. If any other variables are needed, declare them as well.
Get the names of the two input files from the user. The values are strings. Use the prompts "First set file name? " and "Second set file name? "
Fill the first array by calling the build() function using the first filename as the first argument to the function. The function returns the number of values that it put into an array. This returned value should be saved in the integer variable that holds the number of values in the first array.
Fill the second array by calling the build() function using the second filename as the first argument to the function. Make sure the returned value is saved in the integer variable that holds the number of values in the second array.
Display the first array by calling the print() function. Make sure to pass the title "Unsorted Set 1", the first array, and the number of values in the first array (the integer value returned from the first build() call).
Display the second array by calling the print() function. Make sure to pass the title "Unsorted Set 2", the second array, and the number of values in the second array (the integer value returned from the second build() call).
Sort the arrays by calling the sort() function twice. Make sure to pass an array and its corresponding integer variable that holds the number of values in the array.
Display the arrays by calling the print() function two more times. Use the titles "Sorted Set 1" and "Sorted Set 2".
Calculate statistics for the first array by calling the various statistics functions (mean, median, standardDeviation). Make sure to pass them the first array and the integer variable that holds number of values in the first array. Note: The statistics functions all return a value. Depending on how you choose to write the program, additional variables may need to be declared to hold the returned values.
Display the statistics for the first array by calling the displayStats() function. Use the title "Set 1 Statistics".
Calculate statistics for the second array by calling the various statistics functions a second time, making sure to pass them the second array and the integer variable that holds number of values in the second array.
Display the statistics for the second array by calling the displayStats() function a second time. Use the title "Set 2 Statistics".
Finally, display the differences between the mean, median, and standard deviations for the two arrays by calling the displayStats() function a third time. Use the title "Differences"
The input for this program will be read from two input files. Each file consists of a single set of data where each line in the file represents one data point. A file resembles the following:
15.95 50.00 4.50 *more data*
There are multiple versions available for download:
Make sure to read the "Using an Input File in a CPP program" section of the handout for tips on how to get information from an input file.
This program MUST use at least 2 symbolic constants.
The second constant represents the maximum size of the arrays. It should have an integer value of 25.
The third constant represents the maximum number of values to display per line when displaying an array. It should have an integer value of 9.
Write and use the following functions in the program. More functions may be added if needed.
This function will read data from an input file and place it in an array.
It takes two arguments: a string that holds the name of the input file that holds the values to be read, and an array of double values to hold the data. It returns the number of values that were placed into the array.
The function should start by declaring any variables that are needed. At a minimum, there should be a double variable to capture incoming double values, an integer to count the number of values read from the input file, and an input file stream.
Next, the function should attempt to open the input file and verify that it opened correctly.
Now that the file has been opened correctly, it's time to read the information, one value at a time.
In a loop that executes as long as there is information in the file and the end of data value has not been read and the maximum number of values has not been surpassed, put the value that was read into the array, update the integer variable to reflect that a value was placed in the array, and read the next value from the input file.
Finally, once all the data has been read from the file, close the file and return the number of values that were placed in the array.
This function will display the information in an array. The values should be displayed with 2 digits after the decimal point, in a field of 8 characters, and 9 values per line.
It takes three arguments: a string that holds a title for the values being displayed, an array of doubles that holds the data to be displayed, and an integer that holds the number of values in the array. It returns nothing.
The function should first display the title.
In a loop that executes numValues number of times, display a value from the array, making sure 9 values are displayed per line.
Make sure to start and end the display with a single newline character.
This function will sort the array in ASCENDING order. Use the Selection Sort algorithm presented in lecture.
It takes two arguments: an array of doubles that holds the data to be sorted, and an integer that holds the number of values in the array. It returns nothing.
This function calculates the mean (average) of the numbers in the passed-in array.
It takes two arguments: an array of doubles that holds the data, and an integer that holds the number of values in the array. It returns a double: the calculated mean.
This function calculates the median of the numbers in the passed-in array.
It takes two arguments: an array of doubles that holds the data, and an integer that holds the number of values in the array. It returns a double: the calculated median.
The median is the middle value from a sorted set of data. If the number of values in the array is ODD, the median can be found by dividing the number of values in the array by 2 and using the result as a subscript. If the number of values in the array is EVEN, the median is the average of the two middle numbers in the array.
This function returns the standard deviation of the values in the array.
It takes two arguments: an array of doubles that holds the data, and an integer that holds the number of values in the array. It returns a double: the calculated standard deviation of the values in the array.
The standard deviation is calculated by implementing the following formula:

where
For example, if an array contains the values:
7.5 3.6 4.8 5.0
then
(7.5 * 7.5) + (3.6 * 3.6) + (4.8 * 4.8) + (5.0 * 5.0)
(7.5 + 3.6 + 4.8 + 5.0) * (7.5 + 3.6 + 4.8 + 5.0)
This function will display three statistics. The values should be displayed with 2 digits after the decimal point, one per line.
It takes four arguments: a string that holds a title for the values being displayed, a double that holds the mean, a double that holds the median, and a double that holds the standard deviation. It returns nothing.
The function should first display the title followed by the three double arguments in the order mean (field of 24 characters), median (field of 22 characters), and standard deviation (field of 10 characters). Use the labels "Mean:", "Median:", and "Standard Deviation:".
Make sure to start the display with two newline characters and end the display with a single newline character.
The arrays should be able to hold 25 elements. Make sure to use the symbolic constant to represent the maximum size of the arrays.
The arrays have the capability to hold 25 elements, however, that does not mean that it will all be used. This is the reason that the number of elements in the array is being passed to the various functions. This value is the return value from the build function.
Add #include <fstream>, #include <cstdlib> and #include <cmath> at the top of the program.
Copy the input file to your computer and write the program so that it reads the data from the current directory (i.e. don't specify a file path).
As with program 6, each of the functions MUST have a documentation box that describes the function. This is the last reminder about function documentation that will appear in the assignment write-ups.
Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.
First set file name? data7A.txt
Second set file name? data7B.txt
Unsorted Set 1
   15.95   50.00    4.50    1.01   12.35    3.05   10.00    0.02    4.58
   11.11   12.74   12.12    0.07   12.32    1.02   98.54    0.11
Unsorted Set 2
    9.51    6.24   14.89    8.88    6.60   45.21    1.11    7.89    5.21
    0.03    0.45    0.87    1.99   99.99    9.99    4.44    4.87    3.57
    8.21   15.56   45.58   55.55    1.21   35.84   22.22
Sorted Set 1
    0.02    0.07    0.11    1.01    1.02    3.05    4.50    4.58   10.00
   11.11   12.12   12.32   12.35   12.74   15.95   50.00   98.54
Sorted Set 2
    0.03    0.45    0.87    1.11    1.21    1.99    3.57    4.44    4.87
    5.21    6.24    6.60    7.89    8.21    8.88    9.51    9.99   14.89
   15.56   22.22   35.84   45.21   45.58   55.55   99.99
Set 1 Statistics
Mean:                   14.68
Median:                 10.00
Standard Deviation:     24.61
Set 2 Statistics
Mean:                   16.64
Median:                  7.89
Standard Deviation:     23.36
Differences
Mean:                   -1.96
Median:                  2.11
Standard Deviation:      1.25
The data for this program will be read from input files rather than using standard input (cin) or the random number generator. The files should be downloaded and saved. As mentioned earlier, there are multiple versions available for download from Blackboard, the autograder, and the course website. They are:
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/data7A.txt
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/data7B.txt
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/data7C.txt
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/data7D.txt
Each file consists of a single set of data where each line in the file represents one data point. A file resembles the following:
15.95 50.00 4.50 *more data*
To start working with an input file, a couple of extra include statements are needed for the fstream and cstdlib libraries.
When a user entered the values for input, the C++ code used cin and the input operator (>>) to get the information from the keyboard and save it in a variable. cin is an input stream. To get information from a file, an input file stream is needed.
ifstream infile; //input file stream variable this will be used instead of cin
The input file stream variable (infile in the example) will be used in place of cin when information is needed.
Now that the input file stream variable has been created, it needs to be "connected" with the text file that holds the information to be read. The "connection" is created by opening the file:
infile.open( "data7A.txt" ); //open the file for reading
OR
infile.open( filename );      //open the file for reading -- assuming the name
                              //of the file to open is in a string variable
                              //named filename
For this assignment, use version 2 with the string variable.
The open statement will try to open the file specified ("data7A.txt" or whatever file name is saved in the filename variable).
Windows Users: the input file MUST be located in the same directory as the CPP file.
Mac Users: there are three options available to handle the input file.
Option 1: put in the set of double quotes ("") between the parenthesis for the open command, find where the file has been saved on the Mac, and then drag and drop the file in between the quotes. It should place the name of the file, including the complete path of where it is located on the Mac, between the quotes. If this option is used, before handing in the CPP file, remove the path name from the file name so that it only has the file name between the double quotes.
Option 2 for newer versions of XCode (12+): follow the directions in the PDF file found here http://faculty.cs.niu.edu/~byrnes/csci240/FilesAndXCode13.pdf
Option 3 for older versions of XCode: follow the directions in the PDF file found here http://faculty.cs.niu.edu/~byrnes/csci240/FilesAndXCode.pdf
onlinegdb Users: the input file must be uploaded to the online
compiler. To do so, click the cloud/up arrow button ( )
and select the input file. It should open in a tab next to the CPP file in the
online compiler.
 )
and select the input file. It should open in a tab next to the CPP file in the
online compiler.
Once the file has been opened, it's important to make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn't exist or open correctly. To test if the file opened correctly, use something similar to:
if( infile.fail() )       //if the input file failed to open
  {
  cout << endl << "input file did not open" << endl;
  exit(-1);                //stop execution of the program immediately
  }
In the previous programs, the values have been read by executing a cout/cin combination. Since this program is reading input from a file, substitute the name of the input file stream (infile in the example) in place of cin. The cout statement is not needed with the data being read from a file.
So, if a program had something like:
cout << "Value with a decimal point? "; cin >> value;
It should now be:
infile >> value;
When writing a read loop using data from a file, the loop should test to see if there is data in the file. One way to do this is to use the input file stream variable as a boolean variable:
while( infile )
As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is ONLY successful AFTER an attempt has been made to read data from the file. This means that a standard read loop pattern that uses a priming and secondary read should be followed.
Finally, once all the data has been read from the file, the file should be closed. To do this, execute the following:
infile.close();