Formatting output of an array

S

Steve

Hello,
I need help with formatting an output of an array.
I have a two dimensional array that looks something like this:

[1] x x x x
[2] y y y y
[3] b b b b

....and so on...I use [#] to just represent the row number whereas x, y, b
are some values in the array. I have no problem displaying the array on the
screen in the form above by using the following code:

for (down = 0; down < numStudents; down++)
{
cout << arrayStudents[down] <<"\t";

for (accross = 0; accross < numQuizzes; accross++)
{
cout << "\t" << arrayQuizzes[down][accross] <<"\t";

}
cout << endl;
}

But what I need is to display the array in the following form:

[1] [2] [3]
x y b
x y b
x y b

I tried modifying the code above so that the cout would display:
cout << "\t" << arrayQuizzes[accross][down] <<"\t";

But this only works if numStudents and numQuizzes are equal to each other.
If there are more students than there are quizzes then the array doesnt
display properly any more..

How else can I format the output so it looks like this:
[1] [2] [3]
x y b
x y b
x y b
but still works if the number of rows and columns are not equal.

Thanks
 
V

Victor Bazarov

Steve said:
Hello,
I need help with formatting an output of an array.
I have a two dimensional array that looks something like this:

[1] x x x x
[2] y y y y
[3] b b b b

...and so on...I use [#] to just represent the row number whereas x, y, b
are some values in the array. I have no problem displaying the array on the
screen in the form above by using the following code:

for (down = 0; down < numStudents; down++)
{
cout << arrayStudents[down] <<"\t";

for (accross = 0; accross < numQuizzes; accross++)
{
cout << "\t" << arrayQuizzes[down][accross] <<"\t";

}
cout << endl;
}

But what I need is to display the array in the following form:

[1] [2] [3]
x y b
x y b
x y b

I tried modifying the code above so that the cout would display:
cout << "\t" << arrayQuizzes[accross][down] <<"\t";

But this only works if numStudents and numQuizzes are equal to each other.
If there are more students than there are quizzes then the array doesnt
display properly any more..

How else can I format the output so it looks like this:
[1] [2] [3]
x y b
x y b
x y b
but still works if the number of rows and columns are not equal.

How about swapping those 'for' statements of your nested loops instead
of swapping the indices?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top