Maximum and Minimum are displaying strange answers.

//For this program, the maximum answer is wrong, and the minimum answer is right.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int total = 0;
int minSold = 0;
int maxSold = 0;
int temp = 0;
const int SIZE = 5;
const int SIZES = 5;
string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int sales[SIZES];
for (int i = 0; i < 5; i++)
{
cout << "Enter the sales for the " << salsas[i] << " salsa: ";
cin >> sales[i];
total += sales[i];
}
temp = sales[0];
if (minSold < temp)
{
minSold = temp;
}

if (maxSold > temp)
{
maxSold = temp;
}


cout << "The total sales are: " << total << endl;
cout << "The least sold is: " << minSold << endl;
cout << "The most sold is: " << maxSold << endl;
cin.get();
cin.get();
return 0;
}

//For this program, the minimum is a random number like -6.05e + 250
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double rainfall[12];
double total = 0;
double average = 0;
double maxRain = 0;
double minRain = 0;
double tempRain = 0;

for (int i = 0; i < 12; i++)
{
cout << "Enter rainfall for month " << i + 1 << ": ";
cin >> rainfall[i];
while (rainfall[i] < 0)
{
cout << "That is a negative number!" << endl;
cout << "Enter rainfall for month " << i + 1 << ": ";
cin >> rainfall[i];
}
total += rainfall[i];
}


average = total / 12;

maxRain = rainfall[0];
minRain = rainfall[0];

for (int i = 1; i <= 12; i++)
{
tempRain = rainfall[i];

if (tempRain < minRain)
minRain = tempRain;

if (tempRain > maxRain)
maxRain = tempRain;


}

cout << "Total rainfall was: " << total << endl;
cout << "Average rainfall was: " << average << endl;
cout << "Maximum rainfall was: " << maxRain << endl;
cout << "Minimum rainfall was: " << minRain << endl;
cin.get();
cin.get();
return 0;
}

int maxSold = 0;

maxSold begins as zero.

When might it be changed?

1
2
3
4
if (maxSold > temp)  // if maxSold is larger than temp
{
maxSold = temp;
}


temp is the number sold. Shouldn't you be checking if temp is larger than maxSold?
Last edited on
Yes, you're right... But the minimum is still zero. Is there a better way to check for min and max, with arrays?
the type of container is not important.

the algorithm should be
max or min = first in container //this ensures it is valid no matter what, assuming container is not empty...
loop
check every remaining item to see if bigger/smaller
if it is bigger/smaller, max/min gets the new value.



Thank you for your help. My rainfall statistics program works now, but I have one more issue though...
I worked on my Chips and Salsa program, and it still doesn't work... Here is my code:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int total = 0;
int minSold = 0;
int maxSold = 0;
int temp = 0;
const int SIZE = 5;
const int SIZES = 5;
string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int sales[SIZES];
for (int i = 0; i < 5; i++)
{
cout << "Enter the sales for the " << salsas[i] << " salsa: ";
cin >> sales[i];
while (sales[i] < 0)
{
cout << "That is a negative number!" << endl;
cout << "Enter the sales for the " << salsas[i] << " salsa: ";
cin >> sales[i];
}
total += sales[i];
}
for (int i = 1; i <= 4; i++)
{
temp = sales[i];
while (minSold > temp)
minSold = temp;

while (maxSold < temp)
maxSold = temp;

}


cout << "The total sales are: " << total << endl;
cout << "The least sold is: " << minSold << endl;
cout << "The most sold is: " << maxSold << endl;
cin.get();
cin.get();
return 0;
}
Never mind! Thank you all for your help! You guys have helped me a great deal!
My working programs are:


Rainfall Statistics:


#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double rainfall[12];
double total = 0;
double average = 0;
double maxRain = 0;
double minRain = 0;
double tempRain = 0;

for (int i = 0; i < 12; i++)
{
cout << "Enter rainfall for month " << i + 1 << ": ";
cin >> rainfall[i];
while (rainfall[i] < 0)
{
cout << "That is a negative number!" << endl;
cout << "Enter rainfall for month " << i + 1 << ": ";
cin >> rainfall[i];
}
total += rainfall[i];
}


average = total / 12;

maxRain = rainfall[0];
minRain = rainfall[0];

for (int i = 1; i <= 11; i++)
{
tempRain = rainfall[i];

while (tempRain < minRain)
minRain = tempRain;

while (tempRain > maxRain)
maxRain = tempRain;


}

cout << "Total rainfall was: " << total << endl;
cout << "Average rainfall was: " << average << endl;
cout << "Maximum rainfall was: " << maxRain << endl;
cout << "Minimum rainfall was: " << minRain << endl;
cin.get();
cin.get();
return 0;
}

Chips and Salsa:


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int total = 0;
int minSold = 0;
int maxSold = 0;
int temp = 0;
const int SIZE = 5;
const int SIZES = 5;
string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int sales[SIZES];
for (int i = 0; i < 5; i++)
{
cout << "Enter the sales for the " << salsas[i] << " salsa: ";
cin >> sales[i];
while (sales[i] < 0)
{
cout << "That is a negative number!" << endl;
cout << "Enter the sales for the " << salsas[i] << " salsa: ";
cin >> sales[i];
}
total += sales[i];
}

maxSold = sales[0];
minSold = sales[0];

for (int i = 1; i <= 4; i++)
{
temp = sales[i];
while (temp < minSold)
minSold = temp;

while (temp > maxSold)
maxSold = temp;

}


cout << "The total sales are: " << total << endl;
cout << "The least sold is: " << minSold << endl;
cout << "The most sold is: " << maxSold << endl;
cin.get();
cin.get();
return 0;
}
Last edited on
I don't know why it doesn't work--you never told us what it's doing wrong.

Comments: Use code tags. It will be much easier for us to read and comment on your code.

http://www.cplusplus.com/articles/jEywvCM9/

Go ahead and edit your post and add the code tags.

What is the difference between SIZE and SIZES? They both seem to refer to the number of salsa flavors. (Hint: FLAVORS might be a better variable name).

When you loop to read in the sales, you should use your variable (SIZES in the existing code) rather than the hard-coded value "5" in the loop. What happens if you add the flavor "original"? Your salsas array, your sales array and your input loop should all use the same value.

You need to initialize minSold and maxSold to the first value in the sales array before entering the loop. And that loop should also be "< FLAVORS" rather than "<= 4".

Last edited on
Topic archived. No new replies allowed.