Passing array to void function

I am getting zero, the lowest test score is 10 not zero.
I created my own test file with only 10 scores to be able to test my program.
I am certain min should equal 10.

Here is how I read the data into the array after being sure the file opened correctly:
1
2
3
4
//Read numbers from file into the array.
    int count;
    for(count = 0; count < SIZE && inputFile >> score[count]; count++);
    inputFile >> score[count];


Here is my function prototype
 
void minMax(double [], int);  //Get Min & Max test scores 


Here is the function call
 
minMax(score, SIZE);


Here is the function definition
1
2
3
4
5
6
7
8
9
10
11
12
void minMax(double score[], int SIZE)
{
        int count;
        int min = score[0];
        for(count = 1; count < SIZE; count++);
        {
          if(score[count] < min)
          min = score[count];
        }
        cout << "Min =" << min;
        
}


I haven't attempted to get the max yet.
We must write one function and return the min and max values of test scores.

A void function is required, and it must do both.

Any advice?

Kindest regards,

Joseph
1
2
    for(count = 0; count < SIZE && inputFile >> score[count]; count++);
    inputFile >> score[count]

You're reading twice from the file every type you loop, so you're skipping half the numbers to be read. I still don't see why you're getting a 0 if there's none in the file, but you have to fix it anyway.

Incidentally, don't use "count" as an identifier if you're using namespace std;, as there a std::count() in the standard library. The convention for index is "i", which is shorter, simpler and anyone reading your code will instantly recognize:

1
2
for(int i = 0; i < foo; ++i) /* notice you can declare "i" inside the for statement,
                                thus making it as local as necessary*/
Aside from the counter variable, there's another stylistic issue: you shouldn't use all upper case identifiers (i.e. SIZE) - these are reserved for macros by convention.
I think that's the problem.
if SIZE > count you are trying to find the minimum outside the know input data.
Your call should be minMax(score, counter);

1
2
3
for(count = 0; count < SIZE && inputFile >> score[count]; count++)
   ;//the only instruction is do nothing
inputFile >> score[count]; //this is outside the for bucle (why the extra reading?) 
I'm not sure what will happen when you try to read from a file that already reaches the end of file.

Edit:
offtopic:About indexes, I think i and j are likely to be confused with each other so I use K, L, M
Last edited on

Thanks everyone. The problem was actually with my loop:
 
 for(count = 1; count < SIZE; count++); //It was the semi colon. 


I did make some stylistic changes such as using ' i ' instead of "count"; thanks again.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void minMax(double score[], int SIZE)
{
        int i;
        double min = score[0];
        for(i = 1; i < SIZE; i++)
        {
           if(score[i] < min)
           min = score[i];
        }
        
                
        double max = score[0];
        for(i = 1; i<SIZE; i++)
        {
           if(score[i] > max)
           max = score[i];
        }
    
    cout << "The minimum test score is: " << min << endl;
    cout << "The maximum test score is: " << max << endl << endl;    
   
           
}


Is there a way to send the "min" and the "max" to main using this one function; without writing it from the function to the console?
Return a struct (i.e. a pair<double, double>).
Or pass two parameters by reference and fill in them the result.
Or return a container (array, vector, list).
Topic archived. No new replies allowed.