c++arraysendl

cout<<endl; not working for printing a 2d array


I am trying to print a 2d array like this.

1,2

3,4

5,6

7,8

until 20

and this is the code

#include <iostream>
using namespace std;
int main()
{
    int A[10][2]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

    for(int i=0;i<10;i++)
        for(int j=0;j<2;j++)
        {
            cout<<A[i][j]<<" ";
        }
        cout << endl;



}

But everytime it prints it prints them in straight line , like 1 2 3 4 5 6............. What could I be doing wrong?


Solution

  • Hey there you forgot to add {} after first for loop. Here's solution

    #include <iostream>
    using namespace std;
    int main()
    {
        int A[10][2]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<2;j++)
            {
                cout<<A[i][j]<<" ";
            }
            cout << endl;   
        }
    }