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:

//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

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

Recommended Answers

All 4 Replies

Won't this for read twice for each iteration

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

Actually on second look what are you doing here? You have a for loop that reads from your file and then you have a read right after the loop...Why? I never saw a file read like that but it works, just tried it.

I found your problem

void minMax(double score[], int SIZE)
{
        int count;
        int min = score[0];
        for(count = 1; count < SIZE; count++);//you have a ; here
        {
          if(score[count] < min)
          min = score[count];
        }
        cout << "Min =" << min;
        
}

Wasn't this same issue already discussed in your introductory thread?

Not only are you double-reading, which will mess you up really bad, you are using a for loop. It is better to use a while loop instead of a for loop to read your file, it's much easier, and less confusing:

int count = 0;
while ((count < MAX_COUNT) && inFile >> scores[count])
  ++count;

I suspect you are getting 0 as your lowest because you are reading 2x the data available in the file. When you do that, you corrupt your file stream which leads to inaccurate results.

Also:

for(count = 1; count < SIZE; count++); //<---VERY naughty programmer

That last semi-colon should not be there; it is a HUGE logic error. When you put a semi-colon immediately after a conditional like that it drastically alters the program's logic by disconnecting your intended statement or statement block from the conditional that is intended to control it.

Thanks gerard4143 and Fbody for the advice

Is there a way to pass back two integers to main without writing it to the console from the function like I did below?

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;    
   
           
}

Well, you can't "pass back" (a.k.a "return") more than 1 value from any given function. But, you can use a "pass by reference" instead of your traditional "pass by value".

Read this. Basically to make this work, you have to change your syntax related to your function definition (and potentially your call) so that the link between the arguments in your call and the parameters in your definition behaves differently.

Essentially, this:

//prototypes:
int calcMax(int[], int);
int calcMin(int[], int);

//calls:
int max = calcMax(array, SIZE);
int min = calcMin(array, SIZE);

//definitions:
int calcMax(int array[], int SIZE) {
  //...
  return localMax;
}

int calcMin(int array[], int SIZE) {
  //...
  return localMin;
}

becomes this:

//prototypes:
void calcMinMax(int[], int, int &, int &);

//calls:
calcMinMax(array, SIZE, mainMin, mainMax);

//definitions:
int calcMinMax(int array[], int SIZE, int &localMin, int &localMax) {
  //...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.