This question has largely already been posted, and I apologize for that. However, I cannot get the max an min values to change from anything except -1. I'd love to make them actually work; I feel like the comparison statements are correct. I know the counter is working properly, and I know the sum works.

Arrays are not my friend thus far, and help would be greatly appreciated. Here's the code:

``````#include <iostream>

using namespace std;

//Write a program that prompts the user for test scores (doubles).
//The user enters -1 to stop the entry. After all of the test scores
//have been entered, calculate the average, the highest and the
//lowest test score. Use the code below as a template. Make sure you
//respond appropriately when no test scores are entered.
//Use the following screen shots as a guide.

int main()
{
double scores[75];
int counter = -1;
do
{
counter++;
cout << "Please enter a score (enter -1 to stop): ";
cin >> scores[counter];
} while (scores[counter] >= 0);

// CALCULATE AND DISPLAY THE AVERAGE
// THE HIGHEST AND LOWEST TEST SCORE
// BELOW HERE -- DO NOT MODIFY THE REST OF
// PROGRAM EXCEPT TO PUT YOUR NAME ETC.
// AT THE TOP.

double sum = 0;
double average;
double max_value = scores[counter];
double min_value = scores[counter];
int y;

for (int y = 0; y < counter; y++)
{
sum+=scores[y];
}
cout<<"Sum: "<<sum<<endl;

// average
average = sum/counter;
cout<<"Average: "<<average<<endl;

for (int y=0; y< counter; y++)
{
// min value
if (scores[counter] < min_value)
{
min_value = scores[counter];
}

// max value
if (scores[counter] > max_value)
{
max_value = scores[counter];

}
}
cout<<"Min value: "<<min_value<<endl;
cout<<"Max value: "<<max_value<<endl;

}``````

I cannot change anything above the allcaps comment, but I shouldn't need to. Why won't the max and min change? If I could get an explanation and not just code, that'd be great. Thanks.

Edited by calypso&noname: n/a

Attachments
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by calypso&noname

You want to compare with the current index, not counter. The value of counter is the number of legit values in the array. Using it as an index in an array that was not completely populated is extremely likely to be undefined behavior. This is the algorithm:

``````int min = array[0]; // Start with the first element

for ( int i = 0; i < counter; i++ ) {
if ( array[i] < min ) // Compare with the current element
min = array[i]; // Replace with a smaller value
}

cout<<"The smallest value is "<< min <<'\n';``````

Praise be to the (code) goddess Narue! That was so simple. I had tried using i in the for loop before, but I hadn't used it throughout. I had also tried setting min_value[0] but I guess in that case I didn't change the for loop. Ugh. I just needed to do it all together.

Thanks for helping Narue - and for the explanation!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.