I have been sitting at this for 2 hours now, and can't figure out what the heck the problem is. I believe the program is pretty much finished - the problem is something with the array. I am trying to pass by reference to a function, and then.... well.. maybe you can help? The program is supposed to read from a file minmax.in into an array ... the numbers go one after the other on a single line in a seperate 'txt file'. I have not programmed in 10 years... and now i'm just jumping back into the 'game'.
float x[N]; // the array
int n; // the actual array size
float max; // the maximum
float min; // the minimum
/* read the data into the array */
readdat(x[N],n);
/* compute the maximum and minimum */
minmax(x[N],n,max,min);
outfile << "The array has " << n << " elements\n";
outfile << "The maximum value in the array is " << max << endl;
outfile << "The minimum value in the array is " << min << endl;
return 0;
}
void readdat(float& x,int& n )
{ n=0;
int i=0;
float temp=0;
ifstream fin("minmax.in");
while (!fin.eof())
{
fin>>temp;
x[i]=temp;
i++;
}
n=i;
}
void minmax(float x[],int& n, float& max, float& min )
{ int count=0;
max=x[count];
min=x[count];
count++;
while (count!=n)
{if (x[count]>=max) max=x[count];
if (x[count]<=min) min=x[count];
count++;
}
void readdat(float& x, int& n);
void minmax(float x, int& n, float& max, float& min);
#include <iostream>
#include <fstream>
usingnamespace std;
constint N = 100; // maximum array size
int main(void)
{
ofstream outfile("minmax.out");
float x[N]; // the array
int n; // the actual array size
float max; // the maximum
float min; // the minimum
/* read the data into the array */
readdat(x[N],n);
/* compute the maximum and minimum */
minmax(x[N],n,max,min);
outfile << "The array has " << n << " elements\n";
outfile << "The maximum value in the array is " << max << endl;
outfile << "The minimum value in the array is " << min << endl;
return 0;
}
void readdat(float& x,int& n )
{ n=0;
int i=0;
float temp=0;
ifstream fin("minmax.in");
while (!fin.eof())
{
fin>>temp;
x[i]=temp;
i++;
}
n=i;
}
void minmax(float x[],int& n, float& max, float& min )
{ int count=0;
max=x[count];
min=x[count];
count++;
while (count!=n)
{if (x[count]>=max) max=x[count];
if (x[count]<=min) min=x[count];
count++;
}
}