Some more concrete hints:
Write a program that will calculate statistical information on a set of integer numbers.
1. Your main program must call three functions: readData, calculateStats, saveResults.
2. readData will prompt the user for a file that contains the data, one number per line.
3. calculateStats will return the double average and standard deviation via a call by reference parameter.
4. Your program must process up to 250 numbers.
5. The result output file will contain all the numbers 10 per line follows by the average and
standard deviation.
6. All data must be passed between the functions. |
This gives you some very good, useful information. I will rearrange it a little to help you understand the hints:
The first sentence says that you will use
int to store numbers, but that you must use
double when calculating the average and standard deviation.
4 and 5 say that you must have a way of storing a maximum of 250 numbers. (Use an array, of course!)
6 says you are not allowed to use global variables.
2 says that readData() must do
two things: ask (and get) a filename from the user, and read the file into your array.
There is no instruction on how to get the
output file name -- whether it is obtained from the user, generated from the input filename, or a fixed name -- it is up to you. I recommend you do the same thing as in 2: have saveResults() ask the user for a file name and then write the file data.
With all that in mind, you can begin thinking about what kinds of variables you need. In main(), you'll need:
1. An array of 250 integers.
2. Something to keep track of how many integers are actually used in the array. (Use int.)
3. A double value for the average.
4. A double value for the standard deviation.
So, that's easy enough:
1 2 3 4 5 6
|
int main()
{
int data[250];
int used = 0;
double average;
double std_deviation;
|
Now you need to think about the individual functions. The first thing to do is to get the data. So you need to get the
data[] and the
used using the function
readData(). And since neither of those are global variables, you must also pass them to the function.
Here are some potential prototypes:
void readData( int data[], int size, int& used )
int readData( int data[], int size ) // returns the number of numbers placed in data[]
Now you can update
main() so that it uses your new function.
1 2 3 4 5 6 7 8
|
int main()
{
int data[250];
int used = 0;
double average;
double std_deviation;
readData( data, 250, used );
|
or
1 2 3 4 5 6 7 8
|
int main()
{
int data[250];
int used = 0;
double average;
double std_deviation;
used = readData( data, 250 );
|
The
size argument isn't strictly necessary for your assignment, but it is a good idea anyway. This gives your function enough information to
stop reading data when there is no more room in the array. You should typically write code using this strategy, and I recommend you take the annoyance to do it here also.
Now you can write the function itself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
void readData( int data[], int size, int& used )
{
used = 0;
// ask the user for a file name
cout << "input file name? ";
string s;
getline( cin, s );
// open the file
ifstream f( s );
if (!f) return;
// read the file
int number;
while (f >> number)
{
// make sure used < size
...
// stick number in the proper spot in the array
...
// update used
...
}
}
|
Your next task is to find the average and standard deviation. These are both very basic tasks, but they will require some thinking for a beginner.
The average (also called the
mean) gets you past the idea of looping over numbers to sum them. The final sum is divided by the number of numbers (
used).
The standard deviation moves you one step beyond that: as you loop over the numbers, you do something to them before adding them. The final value is the
sqrt() of the variance, which is calculated from the sum.
The final function will work very much like the first. Just remember that there is a trick to when you output a newline: after every tenth number in the array.
Finally, you need to consider what #includes you need. I/O and file I/O, of course, and C Math routines (for the square root), and strings for the filenames,
Here, then, is the complete (suggested) basic code, with helpful commentary. Now you need to fill in the code that makes everything go.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int readData( int data[], int size )
{
int used = 0;
// ask user for a filename
// open the file
// read the data
return used;
}
void calculateStats( int data[], int used, double& average, double& std_deviation )
{
double sum;
// calculate the average (AKA mean)
sum = 0;
for (...)
{
...
}
average = sum / ???;
// calculate the variance
sum = 0;
for (...)
{
...
}
double variance = sum / ...;
// calculate the standard deviation
std_deviation = ...;
}
void saveResults( int data[], int used, double average, double std_deviation )
{
// ask user for filename
// open file
// write the data[]
// write average and std_deviation
}
int main()
{
int data[250];
int used = 0;
double average;
double std_deviation;
used = readData( data, 250 );
calculateStats( data, used, average, std_deviation );
saveResults( data, used, average, std_deviation );
}
|
This is a lot to be as far behind as you are. Good luck!