//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];
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;
}
#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;
}
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".