aLostMonkey
aLostMonkey

Reputation: 594

C++ Primer Plus: 2D Arrays

I've just started working through C++ Primer Plus and I have hit a little stump.

const int MONTHS = 12;
const int YEARS = 3;
int sales[YEARS][MONTHS] = {0};
const string months[MONTHS] = {"January", "February", "March", "April", "May", "June", "July",
                               "August", "September", "October", "November", "December"
                              };

for (int year = 0; year < YEARS; year++)
{
    for (int month = 0; month < MONTHS; month++)
    {
        cout << "Please enter year " << year + 1 << " book sales for the month of " << months[month] << ": \t";
        cin >> sales[year][month];

    }
}

int yearlyTotal[YEARS][3] = {0};
int absoluteTotal = 0;

cout << "Yearly sales:" << endl;

for (int year = 0; year < YEARS; year++)
{
    cout << "Year " << year + 1 << ":";

    for (int month = 0; month < MONTHS; month++)
    {
        absoluteTotal = (yearlyTotal[year][year] += sales[year][month]);

    }

    cout << yearlyTotal[year][year] << endl;

}

cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;

I wish to display the total of all three years. The rest of the code works fine: input is fine, individual yearly output is fine but I just can't get three years added together for one final total.

Sample data would be entering 1 for every option, to give me three totals of 12:

year 1: 12
year 2: 12
year 3: 12

The total number of books sold over a period of 3 years is: 12

The final 12 should obviously be 36.

I did have the total working at one point but I didn't have the individual totals working. I messed with it and reversed the situation.

Upvotes: 2

Views: 602

Answers (4)

Jorg B Jorge
Jorg B Jorge

Reputation: 1109

Check the code below. It will work.

int yearlyTotal[YEARS];
int absoluteTotal = 0;

cout << "Yearly sales:" << endl;

for (int year = 0; year < YEARS; year++)
{
    yearlyTotal[year] = 0;

    cout << "Year " << year + 1 << ":";

    for (int month = 0; month < MONTHS; month++)
    {
        yearlyTotal[year] += sales[year][month];
        absoluteTotal += sales[year][month];
    }

    cout << yearlyTotal[year] << endl;

}

cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;

Upvotes: 0

Donnie
Donnie

Reputation: 46913

The problem is where you have your absoluteTotal increment you're counting the earlier months multiple times (since yearlyTotal is a counter, it increments each month, so adding it onto absoluteTotal every time counts month 1 12 times, month 2 11 times, etc).

Instead, you want that loop to look like this:

for (int year = 0; year < YEARS; year++)
{
    cout << "Year " << year + 1 << ":";

    for (int month = 0; month < MONTHS; month++)
    {
        (yearlyTotal[year][year] += sales[year][month]);

    }

    absoluteTotal += yearlyTotal[year][year]; 
    cout << yearlyTotal[year][year] << endl;

}

So that you count each month only once.

EDIT: Good Person's comment about only needing a 1D array is also correct, of course. :)

Upvotes: 0

Good Person
Good Person

Reputation: 1443

#include <iostream>
#include <string>
using namespace std;
int main (void)
{
const int MONTHS = 12;
const int YEARS = 3;
int sales[YEARS][MONTHS] = {0};
const string months[MONTHS] = {"January", "February", "March", "April", "May", "June", "July",
                               "August", "September", "October", "November", "December"
                              };

for (int year = 0; year < YEARS; year++)
{
    for (int month = 0; month < MONTHS; month++)
    {
        cout << "Please enter year " << year + 1 << " book sales for the month of " << months[month] << ": \t";
        cin >> sales[year][month];

    }
}

int yearlyTotal[YEARS] = {0};
int absoluteTotal = 0;

cout << "Yearly sales:" << endl;

for (int year = 0; year < YEARS; year++)
{
    cout << "Year " << year + 1 << ":";

    for (int month = 0; month < MONTHS; month++)
    {
        yearlyTotal[year] += sales[year][month];
    }
    absoluteTotal += yearlyTotal[year];

    cout << yearlyTotal[year] << endl;

}

cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;
return 0;
}
  1. You only need to increment the absoluteTotal outside of the per month count
  2. You only need a one dimensional array for the yearly count.

When it comes to things like this it helps to write them out on paper first.

Upvotes: 1

Christopher Bertels
Christopher Bertels

Reputation: 101

It looks like you're resetting absoluteTotal each iteration. Do you really want that?

Maybe this would be what you want?:

absoluteTotal += (yearlyTotal[year][year] += sales[year][month]);

Upvotes: 1

Related Questions