Code:
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
void program_description();
void get_info_from_file( ifstream &i_file, int arr[], int &no_items );
void display_array( int arr[], int no_items );
void sort_ascending( int arr[], int no_items );
int get_total( int arr[], int no_items );
int calc_range( int arr[], int no_items );
double calc_mean( int sum, int no_items );
double calc_median( int arr[], int no_items );
const int max_list = 50;
int main()
{
//Variable Declarations
int no_values;
int total;
int array[max_list];
ifstream in_file;
no_values = 0;
program_description();
get_info_from_file( in_file, array, no_values );
display_array( array, no_values );
sort_ascending( array, no_values );
get_total( array, no_values );
total = get_total( array, no_values );
calc_range( array, no_values );
calc_mean( total, no_values );
calc_median( array, no_values );
return 0;
}
//Function Definitions
void program_description()
/*========================================================================
This function outputs a description of the program to the user.
INPUT: None
OUTPUT: None
========================================================================*/
{
cout << "Greetings! This program is designed to take the ";
cout << "data currently stored in\nanother file and display it to you. It will then ";
cout << "sort the data, number it,\nand calculate the sum, range, mean, and median. ";
cout << "From there a histogram\nof the data is displayed.\n\n";
}
void get_info_from_file( ifstream &i_file, int arr[], int &no_items )
/*========================================================================
This function opens the input stream, gets the data from the file one item at a time, and
stores each item in an array element. It also counts the number of items in the file.
Lastly, the input stream is closed.
INPUT: A parameter of type ifstream, an array of integers, an integer which
represents the number of items in the file.
OUTPUT: This function returns nothing.
========================================================================*/
{
int data;
no_items = 0;
i_file.open("stat_data");
i_file >> data;
arr[no_items] = data;
no_items ++;
while ( ! i_file.eof() )
{
i_file >> data;
if ( ! i_file.eof() )
{
arr[no_items] = data;
no_items ++;
}
}
i_file.close();
cout << "The number of items in this array is: " << no_items;
cout << endl << endl;
cout << "The array is:\n\n";
}
void display_array( int arr[], int no_items )
/*========================================================================
The function traverses the array of integers, outputting each element in the array.
INPUT: an array of integers, an integer which represents the number of items in
the file.
OUTPUT: This function returns nothing.
========================================================================*/
{
for ( int i = 0; i < no_items; i++ )
{
cout << arr[i] << " ";
}
cout << endl << endl;
}
void sort_ascending( int arr[], int no_items )
/*========================================================================
This function sorts an array of integers in ascending order using a selection sort.
INPUT: an array of integers, an integer which represents the number of items.
OUPUT: This function returns nothing.
========================================================================*/
{
int i, hold;
cout << "This is the array sorted in ascending order: " << endl << endl;
for ( int pass = 0; pass < no_items - 1; pass++ )
for ( i = 0; i < no_items - 1; i++ )
if (arr[i] > arr[i + 1] )
{
hold = arr[i];
arr[i] = arr[i +1];
arr[i+1] = hold;
}
for ( i = 0; i < no_items; i++ )
cout << arr[i] << " ";
cout << endl;
cout << endl << endl;
}
int get_total( int arr[], int no_items )
/*========================================================================
This function traverses the array of integers and totals the elements in the array.
INPUT: an array of integers, an integer which represents the number of items
in the file
OUPUT: This function returns an integer which represents the total of all of
the elements.
========================================================================*/
{
int sum;
sum = 0;
for ( int i = 0; i < no_items; i++ )
{
sum = arr[i]+sum;
}
cout << "The sum of all the data is: " << sum << endl << endl;
return sum;
}
int calc_range( int arr[], int no_items )
/*========================================================================
This function calculates the range of the values (highest value - lowest value) for
an array of integers. It is assumed that the list has already been sorted.
INPUT: an array of integers, an integer which represents the number of items.
OUTPUT: This function returns an integers which reprsents the range.
========================================================================*/
{
int range;
range = arr[no_items - 1] - arr[0];
cout << "The range of the data is: " << range << endl << endl;
return range;
}
double calc_mean( int sum, int no_items )
/*========================================================================
This function calculates the mean (arthimetic average) given the total of the elements
and the number of items.
INPUT: an integer that represents a sum total, an integer which represents the
number of items
OUTPUT: this function returns a double that represents the mean
========================================================================*/
{
double mean;
cout << setiosflags (ios:: fixed);
cout << setiosflags (ios:: showpoint);
cout << setprecision(1);
mean = sum / (no_items);
cout << "The mean of the data is: " << mean;
cout << endl << endl;
return mean;
}
double calc_median( int arr[], int no_items )
/*========================================================================
This function calculates the media (middle score) of a set of numbers. It is assumed
that the list has already been sorted.
INPUT: an array of integers, an integer which represents the number of items
OUTPUT: this function returns an integer which represents the median
========================================================================*/
{
int median;
double median_average;
int median_1;
int median_2;
int no_scores;
cout << setiosflags (ios:: fixed);
cout << setiosflags (ios:: showpoint);
cout << setprecision(1);
no_scores = no_items;
no_scores = no_scores % 2;
if ( no_scores == 0 )
{
cout << setiosflags (ios:: fixed);
cout << setiosflags (ios:: showpoint);
cout << setprecision(1);
median_1 = arr[(no_items / 2) - 1];
median_2 = arr[no_items / 2];
median_average = (median_1 + median_2) / 2;
cout << "There is an even number of items. " << endl;
cout << "The median of the data is: " << median_average << endl << endl;
}
else
{
median = arr[no_items / 2];
cout << "There is an even odd of items." << endl;
cout << "The median of the data is: " << median << endl << endl;
}
return median;
}