is there anyway i can print arrays in a form of a table?
if so how can i do it?
is there anyway i can print arrays in a form of a table?
if so how can i do it?
Hooked On Phonics Didn't Work For Me!
Print each row of the array and then print a newline.
-Prelude
My best code is written with the delete key.
i know how to print out arrays in two columns but how can i make it so there is an exact number of spaces between each column
Hooked On Phonics Didn't Work For Me!
get a string for the line. figure out how many spaces each number will take up, insert as many space characters as needed. output string.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
That would get the following output:Code:int table[5][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }; for (int i = 0; i<5; i++) { for (int j = 0; j<5; j++) { if (j == 0) // a beginning '|' cout << ' table[j][i] << "| "; cout << "\n"; }
That looks a tad unprofessional. You could improve it by checking to see if 'j' is a single digit, and if it is, print out an extra n number of spaces to even up all the lines. Hope that helps!Code:| 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 10 | | 11 | 12 | 13 | 14 | 15 | | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 |
Brendan
You can experiement with using
printf("%4d", i) right justifiy by 4 and
printf("%-4d", i) left justify by 4.
That's why you use width specifiers. I believe it's something simple like:
cout << width( 10 ) << 120;
Although, I rarely use C++ so I don't recall off the top of my head.
I'm on a call (imagine that) or I'd look it up.
[edit]
Actally, it's 'setw( SIZE )'.
[/edit]
Quzah.
Last edited by quzah; 09-04-2002 at 09:29 PM.
Hope is the first step on the road to disappointment.