I have to use arrays to calculate some statistics on a set of data. The example of the file looks like this:
6 5
1 3 2 4 3
4 2 4 2 1
2 3 5 6 8
5 6 9 4 3
1 5 7 3 7
2 3 5 6 5
the 6 is the rows and 5 is the columns. I need to be able to calculate:
1. the min and max value for each row
2. the average value for each row
3. min and max foe the entire data
4. the average value of entire data
it is getting stuck in the min and max for eac row it prints the firs row correctly, but then it prints it again for the rest, instead of the correct value.
can anyone help
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int a[];
// Prototype Declarations
void openFile(ifstream& inFile, string name);
int readValues (ifstream& infile, int numbers[]);
void printValues(int y, int k, const int numbers[], int number_elements);
void rowMinMax(int x,int x[], int numberValues, int& max, int& min);
void minMax(int numbers[], int numberValues, int& max, int& min);
int main (void)
{
// Local Declarations
int number_elements; // number of elements actually stored in numbers[] array
int i;
int numberValues;
int numbers[100]; // numbers read from the data file
ifstream infile; // inputfile stream
int min, max;
string filename;
int x,y,k,z;
x=y=k=z=number_elements=numberValues=min=max=i=0;
// Statements
cout << "This program will read numbers from a file, store them in an array" << endl;
cout << "and print the numbers." << endl;
cout << "Enter the file to be processed:" << endl;
cin >> filename; //reading in the filename
cout << endl << endl;
openFile(infile, filename); //opening the file specified by the user
infile >> x >> y;
cout << " " << x << " " << y;
k = x*y;
// read the data and store it in the array
readValues (infile, numbers);
// print the data in the array
printValues (y, k, numbers, number_elements);
rowMinMax(x,numbers, y, max, min);
//cout << "min: " << min << "max: " << max;
// find the minimum and maximum value in the array
minMax(numbers, number_elements, max, min);
cout << "The minimum value is " << min << " and the maximum value is " << max << endl;
system("pause");
return 0;
} // main
/* ================== openFile ==================
Open a text file and return the file stream.
*/
void openFile(ifstream& inFile, string name) //opens the file unless there's an error which causes the program to exit
{
inFile.open(name.c_str());
if (inFile == false)
{
cout << "There was an error opening the file" << endl;
system("pause");
exit(0);
}
}
/* ================== readValues ==================
Read up to 100 integers from file specified by infile into array.
Returns the number of values read from file
*/
int readValues(ifstream& infile, int numbers[])
{
int numbervalues = 0;
int i = 0;
while (i < 100 && infile)
{
infile >> numbers;
i++;
}
numbervalues = i;
return numbervalues;
}
/* ================== printData ==================
Prints the data on multiple lines if needed
Pre data: a filled array
number_elements: size of array to be printed
Post The data have been printed
*/
void printValues (int y, int k, const int numbers[], int number_elements)
{
// Local Declarations
int i, j;
int numPrinted = 0;
int lineSize = y; // number of values to print on each line
number_elements = k;
// Statements
cout << endl << endl;
for (i = 0; i < number_elements; i++)
{
numPrinted++;
cout << setw(4) << numbers;
if (numPrinted >= lineSize)
{
cout << endl;
numPrinted = 0;
} // if
} // for
cout << endl << endl;
return;
}
void rowMinMax(int z,int x[], int numberValues, int& max, int& min)
{
max = -1;
min = 99999;
int i,d,c;
c=0;
for (d = 0; d < z; d++)
{
for (i=numberValues*c; i<numberValues*(c+1); i++)
{
if (x > max)
max = x;
if (x < min)
min = x;
}
cout << "min: " << min << "max: " << max<<endl;
c=c++;
max=-1;
min=99999;
}
}
/* ================== minMax ==================
Return the minimum and maximum number in the array.
*/
void minMax(int numbers[], int numberValues, int& max, int& min)
{
max = -1;
min = 99999;
int i;
for (i=0; i<numberValues; i++)
{
if (numbers > max)
max = numbers;
if (numbers < min)
min = numbers;
}
}