Monday, May 24, 2010

C++, Arrays, Max/Min Values?

I've designed a program that asks the user to enter a certain number of temperature between 0 and 100, whatever the user wants. After that, the program finds the average, standard deviation and the maximum and minimum values. All that works. The problem is when I output the max/min values, I also need to output which reading number (box in the array) that the max/min value comes from. I cannot figure out how to tell the program to output that value, whatever it may be. To help you with this, I've included the part of the code that declares max/min values





float minTemp = temp[0];


for (i = 0; i %26lt; numTemps; i++)


{


if (minTemp %26gt; temp[i])


minTemp = temp[i];


}


cout %26lt;%26lt; "The Minimum Temperature is: " %26lt;%26lt; minTemp %26lt;%26lt; " in Reading # " %26lt;%26lt; WHAT DO I PUT HERE? %26lt;%26lt; "\n";





float maxTemp = temp[0];


for (i = 0; i %26lt; numTemps; i++)


{


if (maxTemp %26lt; temp[i])


maxTemp = temp[i];


}


cout %26lt;%26lt; "The Maximum Temperature is: " %26lt;%26lt; maxTemp %26lt;%26lt; " in Reading # " %26lt;%26lt; WHAT DO I PUT HERE? %26lt;%26lt; "\n";








return

C++, Arrays, Max/Min Values?
You just need another variable that saves the index in the array.





float minTemp = temp[0];


int index = 0;


for (i = 0; i %26lt; numTemps; i++)


{


if (minTemp %26gt; temp[i]) {


minTemp = temp[i];


index = i;


}


}


cout %26lt;%26lt; "The Minimum Temperature is: " %26lt;%26lt; minTemp %26lt;%26lt; " in Reading # " %26lt;%26lt; index %26lt;%26lt; "\n";





You would do the same for the other part.
Reply:try using java it works better than C++ and it shows your what is exactly wrong.


No comments:

Post a Comment