The first function is always the hardest v.2

So for my assignment I have to do this.


Write a complete C++ program to read in, sort, and perform other statistical actions on a set of data. The program will read from an input file and write all results to an output file.
From an input file, the program will read a set of student's 3-digit id numbers and their SAT scores.
The program will sort the id numbers into descending numerical order, being sure to carry along the corresponding SAT scores. The program will then print the sorted lists, giving both id numbers and SAT scores.
The program will then sort the SAT scores into ascending numerical order, carrying along the corresponding id numbers. The program will print the sorted lists, giving both id numbers and scores.
Then the program will call functions to find the max and min of the SAT scores, the mean of the SAT scores, and the median of the SAT scores.
NOTE: In order to properly test the median function, you will run the same program TWICE, using different input and output files (in the program, you should change only the names of the files). You must send me two different input files, one with an even number of sets of data (even value for n) and once with an odd number of sets of data (an odd value for n), and two different output files. Make sure the names match to show which is even and which is odd.
Finally, the program will read in two scores and call a function search, once for each value, to see if the score is found in the array scores.

I'm only having trouble with modifying the readdata function which I have here.

How do I modify it so that it doesn't need the n parameter, but instead uses a counter to get n?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//readdata() reads in the values of both arrays from a file.
void readdata(int &n,int nums1[],int nums2[])
{
	infile>>n;
	if(n>0&&n<=LIM)
		outfile<<"There are "<<n<<" values"<<endl;
	else 
	{
		outfile<<"Invalid number of values"<<endl;
		exit(1);
	}
	for(int count=0;count<n;count++)
	{
		infile>>nums1[count];
		infile>>nums2[count];
	}
	return;
}
An expression like infile>>nums1[count]; returns false if an error like end of file occurs, true otherwise. You can use as end condition for the loop like so:
1
2
3
4
5
	for(int count=0;count<n;count++)
	{
		if(!(infile>>nums1[count]>>nums2[count]))
		  break;
	}
Last edited on
@coder777: A not is missing?

1. Avoid global variables.
2. This still reads a (putative) n from infile, but with little tampering ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void readdata( std::istream & infile, int &n, int * nums1, int * nums2, int LIM )
{
  if ( infile >> n && 0 < n && n <= LIM ) {
    int v1 = 0;
    int v2 = 0;
    int count = 0;
    while ( count < n && infile >> v1 >> v2 ) {
      nums1[count] = v1;
      nums2[count] = v2;
      ++count;
    }
    n = count;
  }
  else {
    n = 0; // exit() is harsh, let the caller handle the error instead
  }
}
keskiverto wrote:
A not is missing?
You are right. I changed it in the above code.
Topic archived. No new replies allowed.