#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
void inputData(double []);
int lowMonth(double[]);
int highMonth(double[]);
double averageSales(double[]);
const int NUM_MONTHS = 12;
const int STRING_SIZE = 10;
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);
// Display results
cout << "The highest sales were in " << monthArray(high) << " with $" << setprecision(2) << fixed << salesArray[high] << endl;
// Find the month with the lowest sales
low = lowMonth(salesArray);
cout << "The lowest sales were in " << monthArray(low) << " with $" << setprecision(2) << fixed << salesArray[low] << endl;
// Calculate the average monthly sales
average = averageSales(salesArray);
cout << "The average monthly sales were $" << setprecision(2) << fixed << average << endl;
system("pause");
}
// 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;
highest = sales[0];
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] > highest)
{
highest = sales[i];
}
}
return highest;
}
// 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;
lowest = sales[0];
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] < lowest)
{
lowest = sales[i];
}
}
return lowest;
}
// This function computes the average monthly sales
double averageSales(double sales[])
{
double sum = 0;
double average;
for (int i = 1; i < NUM_MONTHS; i++)
{
sum += sales[i];
}
average = (sum/NUM_MONTHS);
return average;
}
I am having trouble with the output. It is showing very crazy numbers and it is not showing the month where sales are highest and lowest. Any help would be appreciated. Below is the original code given by teacher to debug and above is what ive fixed.
// Monthly Sales Analysis
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
void inputData(double[]);
int lowMonth(double[]);
int highMonth(double[]);
double averageSales(double[]);
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[])
{
int sum = 0;
for (int i = 1; i < NUM_MONTHS; i++)
{
sum += sales[i];
}
return sum / NUM_MONTHS;
}
Am i calling my arrays the wrong way and where it declares high, low and average is that right?
In the main function.
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();
}