Jacob J
Jacob J

Reputation: 21

Trying to certain output with nested for loops

#include <iostream>
using namespace std;

int main (){

    int row, 
        column = 0,
        colCount = 3,
        rowCount = 3;

    for (column; column < colCount; column++){

        for(row = 0; row <= (rowCount - 1); row++){

            cout << column << " " << row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

        }

        cout << column;
        cout << endl;

    }

}

Currently producing this output:

0 0, 0 1, 0 20
1 0, 1 1, 1 21
2 0, 2 1, 2 22

Trying to produce this output:

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

Upvotes: 0

Views: 35

Answers (1)

user9849588
user9849588

Reputation: 607

Remove

cout << column;

right above

cout << endl;

Final code:

#include <iostream>
using namespace std;

int main (){

    int row, 
    column = 0,
    colCount = 3,
    rowCount = 3;

    for (column; column < colCount; column++){

        for(row = 0; row <= (rowCount - 1); row++){

            cout << column << " " << row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

        }

        cout << endl;

    }

}

Upvotes: 2

Related Questions