Exforsys
+ Reply to Thread
Results 1 to 4 of 4

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 ...

  1. #1
    cyfuz is offline Junior Member Array
    Join Date
    Mar 2010
    Answers
    1

    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:

    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;
    }
    Would I need to declare separate variables to hold the calculated values for a given year?


  2. #2
    narendra.b is offline Junior Member Array
    Join Date
    Mar 2010
    Answers
    4
    try to use structures in this... you will find solution...


  3. #3
    shekhar81 is offline Junior Member Array
    Join Date
    Jul 2010
    Answers
    4

    Re: 2D array function calculating sales

    Hello Guys,

    I think you also know about switch case.Use it.

    Shekhar.


  4. #4
    sujeet.varshney is offline Junior Member Array
    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



Latest Article

Network Security Risk Assessment and Measurement

Read More...