Read a file then use for another function
May 1, 2011 at 2:52am UTC
Hello
Im trying to create a separate function that reads a file for everything i will need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
int text(double array1[], double array2[], char readthefile[])
{
double xarray[200];
double yarray[200];
readingfile.open(file);
if (!readingfile.is_open()){
cout<<"Error reading the file\n" ;
readingfile.close();
system("PAUSE" );
}
while (readingfile.good()&&readingfile){
readingfile>>array1[row];
readingfile>>array2[column];
initialx+=array1[row];
initialy+=array2[column];
xy+=(array1[row]*array2[column]);
xsquared+=(array1[row]*array1[row]);
count++;
}
How do I get the value of total of initialx, initialy,xy and xsquared to be used in another function that i will make without reading the file again?
Thanks in advance!
Last edited on May 1, 2011 at 2:55am UTC
May 1, 2011 at 7:32am UTC
Read the file and store its values in a global 2d array which you can use to calculate initialx, initialy, xy and xsquared.
I.e.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
const int height = 5, width = 5;
xyArray[height][width];
void readFile(string filename)
{
ifstream infile.open(filename);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
infile >> xyArray[i][j];
}
}
infile.close();
}
// Calculate XY for example
xy = xyArray[0][0] * xyArray [1][0];
This is on condition that your file reflects the constraints of the logic above and looks something like this:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Last edited on May 1, 2011 at 7:33am UTC
May 1, 2011 at 9:11am UTC
Thanks, for the idea
I actually figured it out before you posted
Thanks anyway
Topic archived. No new replies allowed.