Have patience as I am a beginner at this.I have completeed my 2d Array and have displayed its contents successfully. At the end of my program I want to total column 1 and 2 ([0],[1]) and eventually average it, but my code is not giving me the correct math(see input below). I think my operators must be wrong. Below is my code, and as I said everything is good except for the math in the last part. Look at my last for statement. Also I am using Visual studio 2008. ANY help is certainly appreciated.

my input into Array:

40  20
41  21
42  22
43  23
44  24
45  25
46  26

total for col 1 should be 301, but I am getting 602!?!

#include "stdafx.h"
#include "iostream"
#include "iomanip"
#include "cmath"
#include "string"

using namespace std;


int main ()
{
    //declare and intilize array
    int tempav[7][2]= {0};
    int tot = 0;
    int average = 0;

    //enter data into array

    for(int day = 0; day < 7; day ++)
        for (int temps = 0; temps < 2; temps ++)
    {

        cout<<"Day Number "<<day +1 << ", Temperature " << temps + 1 << ": ";
        cin>> tempav[day][temps];

    }//end for

//end for

    //display contents of array
        for (int day = 0; day < 7; day ++)
        {
            cout << "Day " << day + 1 << ": " << endl;
            for (int temps = 0; temps < 2; temps ++)
            {
                cout << " Temperature " << temps + 1 << ": ";
                cout << tempav[day][temps] << endl;
            }//end for
}//end for

              for (int day = 0; day < 7; day += 1)
        for (int temps = 0; temps < 2; temps += 1)
            tot += tempav[day][0];              
        {
            cout << " Total Col 1 " << tot << endl;

        }


    system("pause");
    return 0;
}//end of main function

Dani AI

Generated

The symptom (301 becoming 602) is classic double-counting: the inner temps loop runs twice for each day, so the first-column value is added two times per day. correctly pointed out that you should only add the first column once per day.

Fix checklist:

  • Sum only over the day index when you want a column total (remove the nested loop that iterates temps for this sum).
  • Always use braces for loop bodies, even for single statements, to avoid accidental scoping mistakes.
  • Keep tot initialized (you already do). For the average, use a floating type: cast the total to double before dividing (for example, average = double(total) / numDays) so the fractional part is kept instead of truncated.

Additional practical tips:

  • Use clearer names (e.g., totalCol0, numDays = 7) so the intent is obvious when you read the loops later.
  • Check input validity after each cin >> (use cin.fail() and clear the stream) so bad input does not silently corrupt the sum.
  • If totals might get large, use a wider integer type (long long) to avoid overflow.
  • If you want to debug, print an intermediate running total inside the day loop to confirm each addition.

Quick references that explain loop scoping and integer division in C++:

These points should make the result match the hand-calculated total (301 for the first column) and produce a correct average.

Recommended Answers

All 2 Replies

You adding it twice here :

for (int day = 0; day < 7; day += 1)
for (int temps = 0; temps < 2; temps += 1)
tot += tempav[day][0]; 
{
cout << " Total Col 1 " << tot << endl;

}

just do this:

for (int day = 0; day < 7; day += 1){
 tot += tempav[day][0]; 
}

Thanks you helped me tremendously!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.