
- Forum
- Programming Talk
- C and C++
- 2D array function calculating sales
2D array function calculating sales
This is a discussion on 2D array function calculating sales within the C and C++ forums, part of the Programming Talk category; Hi all, I'm having trouble figuring out how to calculate the sales for each individual year. Right now, the loop ...
-
2D array function calculating sales
Hi all,
I'm having trouble figuring out how to calculate the sales for each individual year. Right now, the loop is performing a serial calculation, adding years 0, 1, and 2. How could I separate the calculated sales for that given year?
Here's the code:
Would I need to declare separate variables to hold the calculated values for a given year?Code:void calculateAndDisplayTotalSales(float monthlySales[12][3], const unsigned int numberOfRows, const unsigned int numberOfColumns) { double totalYearlySales = 0.0; for (int year = 0; year < YEARS; year++) { for (int month = 0; month < MONTHS; month++) { totalYearlySales += monthlySales[month][year]; } cout << "Year " << year << " sales were $"; cout.setf(ios_base::fixed, ios_base::floatfield); cout << setprecision(2) << totalYearlySales << "\n"; } cout << "\nThe total for all three years was $" << totalYearlySales; cout << endl << endl; return; }
-
03-09-2010, 04:50 AM #2
- Join Date
- Mar 2010
- Answers
- 4
try to use structures in this... you will find solution...
-
Re: 2D array function calculating sales
Hello Guys,
I think you also know about switch case.Use it.
Shekhar.
-
07-12-2010, 12:35 PM #4
- Join Date
- Jul 2010
- Answers
- 7
simple funda by Sujeet varshney C++ expert since 1996
void calculateAndDisplayTotalSales(float monthlySales[12][3], const unsigned int
numberOfRows, const unsigned int numberOfColumns)
{
//double totalYearlySales = 0.0;
for (int year = 0; year < YEARS; year++)
{
double totalYearlySales = 0.0;//correction
for (int month = 0; month < MONTHS; month++)
{
totalYearlySales += monthlySales[month][year];
}
cout << "Year " << year << " sales were $";
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << setprecision(2) << totalYearlySales << "\n";
}
cout << "\nThe total for all three years was $" << totalYearlySales;
cout << endl << endl;
return;
}
-
Sponsored Ads

Reply With Quote





