Click here to Skip to main content
16,017,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My goal is to make a create a multidimensional array in c++. The problem is when i run my code it displays the whole array as a column but i want it to display it as a table. Please can anyone help me with the code. Thanks.

C++
int array [3] [3];


for (int i = 0; i<3; i++)
{
 for(int j = 0; j<3; j++)
{
cout<<"Enter your array values:"<<endl;
cin>> array[i] [j];
}
}

for (int i = 0; i<3; i++)
{
 for(int j = 0; j<3; j++)
{
cout<<array[i] [j]<<endl;
}
}
Posted
Updated 21-Jul-14 1:41am
v2

Provide the spacing between values

XML
for(int i = 0; i < 3; i++)
               {
                   for (int j = 0; j < 3; j++)
                   {
                       cout<<array[i][j]<<"\t";
                   }
                   cout<<endl;
               }
 
Share this answer
 
I'm not sure what you mean bij displays as a column.
But if you mean everything is shown underneath each other then you shoule do:

for (int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
cout << array[i][j]; // don't put you end-of-line here
cout << (j!=2)?",":""; // insert a delimiter for your values
}

cout << endl; // but instead put it here, so each "row" starts as a new line

}

you may also need to exchange the i and j, depending on what you think your row is: the first or second index.
 
Share this answer
 
To Display it as a table you can modify your cout like this

C++
for (int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
cout<<array[i][j]<<" ";
}
cout<<endl;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900