Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,934 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,636 people online right now. Registration is fast and FREE... Join Now!




Arrays

 
Reply to this topicStart new topic

Arrays

pdhbaseball23
3 Dec, 2007 - 09:00 PM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 1


My Contributions
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[5];
// 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];
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[i];
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[i] > max)
max = x[i];
if (x[i] < min)
min = x[i];

}
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[i] > max)
max = numbers[i];
if (numbers[i] < min)
min = numbers[i];
}
}


User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Arrays
3 Dec, 2007 - 11:30 PM
Post #2

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 122



Thanked: 3 times
Dream Kudos: 150
My Contributions
Wel nice try, was a good logic, try to reduce the unnecessary passing of addresses( wel that doesnt affect the output)

Here i have made small corrections. it works fine icon_up.gif


CODE

#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
int a[5];
// 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
numberValues=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, k, 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];
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[i];
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 = 0;
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[i] > max)
max = x[i];
if (x[i] < min)
min = x[i];

}
cout << "min: " << min << "max: " << max<<endl;
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 = 0;
min = 99999;
int i;

for (i=0; i<numberValues; i++)
{
if (numbers[i] > max)
max = numbers[i];
if (numbers[i] < min)
min = numbers[i];
}
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 03:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month