Changing code with array

How do I use array in this code?
REWRITE THIS CODE USING ARRAY

#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototype
double caltotalSales(double, double);
double calAverage(double, int);

int main()
{
int years, // The number of years
totalQuarters; // The number of quarters
double sales,
totalSales = 0, // The total sales for all quarters
average; // The average quarterly sales

// Get the number of years.
cout << "This program will calculate average sales over a \n"
<< "period of years. How many years do you wish to average? ";
cin >> years;

// Validate the number of years.
while (years < 1)
{ cout << "Years must be at least one. Please re-enter: ";
cin >> years;
}

for (int yearNumber = 1; yearNumber <= years; yearNumber++)
{
cout << "\nYear " << yearNumber << endl;

// Get the sales amount for each quarter.
for (int quarter = 1; quarter <= 4; quarter++)
{
cout << "sales amount for quarter "
<< setw(2) << quarter << "? ";
cin >> sales;

// Validate the sales amount.
while (sales < 0)
{
cout << "Sales amount must be zero or greater.\n";
cout << "Sales amount for month "
<< quarter << "? ";
cin >> sales;
}

// Get the total sales from funtion
totalSales = caltotalSales(totalSales, sales);
}

}


// Calculate the total number of quarters.
totalQuarters = years * 4;

// Get the average from average function
average = calAverage(totalSales, totalQuarters);
// Calculate the average sales amount.


// Display the total sales amount for the period.
cout << fixed << showpoint << setprecision(2);
cout << "\nOver a period of " << totalQuarters
<< " quarters, " << totalSales <<endl;

// Display the average sales for the period.
cout << fixed << showpoint << setprecision(2);
cout << "Average quarterly sales for the period is " << average << endl;

return 0;
}

//********************************************************
// The caltotalSales function calculates the total sales *
// for a given years. *
// ********************************************************

double caltotalSales(double totalSales, double sale)
{
// Accumulate the sales amount.
return totalSales += sale;
}

//********************************************************
// The average function calculates the average sales *
// for a given years. *
// ********************************************************

double calAverage(double totalSales, int quarters)
{

return totalSales / quarters;
}

Not sure what you want the array to do.

you could do something like


enum
{
q1,q2,q3,q4, qmax
};

double yearsales[qmax];

...
yearsales[q1] = firstquartersales;
yearsales[q2] = secondquartersales;

or something like that and loop over the quarters.


you can also just use [0], [1], ... the enum is just being fancy but I like to name array indices if they have a meaning.



REWRITE THIS CODE USING ARRAY

ok, let's ask ourselves first - which bits of the current program could do with an array - well, of course the sales data because there are four quarters and the current program has no way of saving the incoming sales data whereas the array would provide a container to store the data
actually, you could go one step further:
1
2
{ cout << "Years must be at least one. Please re-enter: ";
cin >> years;

this is crying out for a 2D array where the number of rows == years and the no of cols = 4 and since you are getting the year information during run-time it'd have to be a dynamic array created using the new operator. so for 2 years it'd be a 2X4 array, 3 years 4X4 array and so on
and to get yearly sales, average, print a year's sales etc you'd just have to operator across the particular row of the 2D array and, finally, the dynamic array has to be deleted (delete []) as well
Topic archived. No new replies allowed.