I'm trying to create a program with C++ that calculates values for a 2D array and then prints them out in the form of a table. Each nvalue ranges from 1 to 6 decimal places, so I need to give them equal space in the table.
I've tried looking around but I've had trouble understanding it. Can I use printf
to give equal space to each value in a 2D array? Would I use a for loop to print each value using printf
?
Asked
Active
Viewed 916 times
-5

Sourav Ghosh
- 133,132
- 16
- 183
- 261

Balalzeekrya
- 5
- 1
-
Maybe [this](http://stackoverflow.com/questions/2485963/c-alignment-when-printing-cout) helps! – GAVD Mar 08 '16 at 06:45
-
1If you are using C++, why are you using the C based printf? – CJCombrink Mar 08 '16 at 07:08
1 Answers
0
For example you have a two dimensional array like :
int a[5][4];
So if you want to store data and then print, you could try doing it this way:
int i,j=0;
cout<< "\nEnter your array values : \n";
for (i=0;i<5;i++){
for(j=0;j<4;j++){
cin>>a[i][j];
}
}
To print :
for (i=0;i<5;i++){
for(j=0;j<4;j++){
cout<<a[i][j];
}
cout<< "\n";
}
Edit: yikes, just re-read your question, you wish to print them in a table?: yes, you can use printf for equal spaces, just give the new line between the for loops, so that it appears in a table format.

YouHaveaBigEgo
- 220
- 6
- 13
-
1
-
my mistake... i first wrote using printf, and then i realized the user was asking help in C++, I forgot to edit properly – YouHaveaBigEgo Mar 08 '16 at 06:43