Mustan
Mustan

Reputation: 11

c++ calculations in the nested for loop

I've been trying to do this program but I'm stuck, I'm still a beginner, any help would be appreciated. I need the program to do

  1. Print a 10 X 10 table in which each entry in the table is the sum of the row and column number
  2. Include an accumulator that will calculate the sum of all the table entries.

what I'm having a problem with is when I try to calculate the row and column for each entry and the total sum.

Each time I put any calculation in the nested for loop it messes up. Here's with no calculations:

No Calculations

Here's with the calculations:

With Calculations

The code:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    // r= row, c= column, s= sum(row+column), ts= sum of all entries   
    int r, c, s = 0, ts = 0;  
    for (r = 1; r <= 10; r++)
    {
        for (c = 1; c <= 10; c++)
        s = r + c; ** This one
        ts = ts + s; ** and this

        cout << setw(3) << c;
        cout << endl;

    }
    cout << "the total sum of all table entries is " << ts << endl;
    system("pause");
    return 0;
}

Upvotes: 0

Views: 2568

Answers (3)

Code-Apprentice
Code-Apprentice

Reputation: 83577

Note that a loop will repeat the next statement. When you do it "without calculations", I assume you mean

for (c = 1; c <= 10; c++)
    cout << setw(3) << c;
cout << endl;

Here, the first cout statement is repeated and prints out the table in your first screenshot. (Notice the indentation here which indicates what code is "inside" the for loop.)

Now when you add the calculations, you have

for (c = 1; c <= 10; c++)
    s = r + c; ** This one
    ts = ts + s; ** and this
    cout << setw(3) << c;

cout << endl;

Even if you indent to show what you intend to repeat, the program will only repeat the statement immediately following the for loop header. In this case, you are repeating the calculation s = r + c; over and over. (Since this result is never used, the compiler most likely just throws it away.)

In order to repeat multiple statements, you need to wrap them in a "compound statement" which means using curly braces:

for (c = 1; c <= 10; c++)
{
    s = r + c; ** This one
    ts = ts + s; ** and this
    cout << setw(3) << s;
}

cout << endl;

I also assume that you want to print out the sum of the row and column.

I strongly suggest that you always use curly braces, even when you repeat a single statement. This makes it easier to add more statements inside a loop because you don't have to remember to add the curly braces later.

Upvotes: 2

Anthony Kong
Anthony Kong

Reputation: 40834

You need a pair of curly brackets for your for loop

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int r, c, s = 0, ts = 0;  // r= row, c= column, s= sum(row+column), ts= sum of all entries   
    for (r = 1; r <= 10; r++)
    {
        for (c = 1; c <= 10; c++) { // <- was missing
        s = r + c; ** This one
        ts = ts + s; ** and this

        cout << setw(3) << c;
        cout << endl;
        } // <- was missing

    }
    cout << "the total sum of all table entries is " << ts << endl;
    system("pause");
    return 0;
}

Without the {}, only s = r + c will be considered part of the for loop.

Incidentally this is the cause of the goto fail bugs: http://martinfowler.com/articles/testing-culture.html

Upvotes: 1

Dave Lyndon
Dave Lyndon

Reputation: 796

I think you need to enclose the inner loop in curly brackets like so:

for (r = 1; r <= 10; r++)
{
    for (c = 1; c <= 10; c++)
    {
    s = r + c;
    ts = ts + s;

    cout << setw(3) << c;
    cout << endl;
    }
}

Otherwise you will only run the

    s = r + c;

line in the inner loop.

Upvotes: 2

Related Questions