int main()
{
double salesArray[NUM_MONTHS];
int low, high;
double average;
// Input the monthly sales data
inputData(salesArray);
// Find the month with the highest sales
high = highMonth(salesArray);
// Find the month with the lowest sales
low = lowMonth(salesArray);
// Calculate the average monthly sales
average = averageSales(salesArray);
// Display results
cout << "The highest sales were in " << monthArray[high] << " with $" << setprecision(2) << fixed << salesArray[high] << endl;
cout << "The lowest sales were in " << monthArray[low] << " with $" << setprecision(2) << fixed << salesArray[low] << endl;
cout << "The average monthly sales were $" << setprecision(2) << fixed << average << endl;
cin.get();
cin.get();
}
// This functions requests the monthly sales data from the user
void inputData(double sales[])
{
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << "Please enter the sales in dollars for " << monthArray[i] << " ";
cin >> sales[NUM_MONTHS];
}
return;
}
// This function determines which month had the highest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int highMonth(double sales[])
{
double highest = -1;
int highIndex = -1;
int main()
{
double salesArray[NUM_MONTHS];
int low, high;
double average;
// Input the monthly sales data
inputData(salesArray);
// Find the month with the highest sales
high = highMonth(salesArray);
// Find the month with the lowest sales
low = lowMonth(salesArray);
// Calculate the average monthly sales
average = averageSales(salesArray);
// Display results
cout << "The highest sales were in " << monthArray[high] << " with $" << setprecision(2) << fixed << salesArray[high] << endl;
cout << "The lowest sales were in " << monthArray[low] << " with $" << setprecision(2) << fixed << salesArray[low] << endl;
cout << "The average monthly sales were $" << setprecision(2) << fixed << average << endl;
cin.get();
cin.get();
}
// This functions requests the monthly sales data from the user
void inputData(double sales[])
{
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << "Please enter the sales in dollars for " << monthArray[i] << " ";
cin >> sales[NUM_MONTHS];
}
return;
}
// This function determines which month had the highest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int highMonth(double sales[])
{
double highest = -1;
int highIndex = -1;
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] > highest)
{
highest = sales[i];
highIndex = i;
}
}
return highIndex;
}
// This function determines which month had the lowest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int lowMonth(double sales[])
{
double lowest = -1;
int lowIndex = -1;
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] < lowest)
{
lowest = sales[i];
lowIndex = i;
}
}
return lowIndex;
}
// This function computes the average monthly sales
double averageSales(double sales[])
{
double sum = 0;
for (int i = 1; i < NUM_MONTHS; i++)
{
sum += sales[i];
}
return sum / NUM_MONTHS;
}
In the function in which you input the data, you are storing all the data like this cin >> sales[NUM_MONTHS];
This is off the end of your array (remember that the array elements are numbered zero to NUM_MONTHS - 1) and all writing over each other. I expect you meant: cin >> sales[i];
In your lowmonth function, the benchmark (beginning) low score is minus one. What happens if no month has sales below minus one?