Well, I was able to get my program running at school today. I've started to add some more things, but now I've noticed that my array is acting up. It's repeating the last number in my stat_data file twice. So if the numbers in my file are: 98, 54, 76, 46, and 78 my program outputs: 98, 54, 76, 46, 78, and 78. Sorry for all of the pleas for help, but this is really getting frustrating. Here's my code:
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 );
const int max_list = 50;
int main()
{
//Variable Declarations
int no_values;
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 );
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;
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;
}