Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Array Problem in C++

How can I print the entire array backwards and upside down... last line print first and last column on the left. Thx!

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int num_row =7;
const int num_col =2;

int examPrep [num_row][num_col] =
{
{12,2},
{1,2},
{45,13},
{32,12},
{5,6},
{1,0},
{23,4}

};

int main()
{
for(int row=0; row<num_row;row++)
{
for(int col=0; col<num_col;col++)

cout<<" "<<examPrep[row][col];
cout<< endl;

}
return 0;
}
[687 byte] By [leoyous] at [2007-11-11 10:15:45]
# 1 Re: Array Problem in C++
make the for loop decrease :
for(int row=num_row; row>0; row--)
{
for(int col=num_col; col>0; col--)
{
...
it may need another small adjustment .
Amahdy at 2007-11-11 20:59:14 >
# 2 Re: Array Problem in C++
One small adjustment is that the for must begin in num_row - 1 and must continue until is greater or equal to 0. Something like:

...
for (int row = num_row-1; row >= 0; row--)
...

You can instead make this modification in the subscript of the matrix

for (int row = num_row; row > 0; row--)
matrix[row - 1][col - 1]

In math, to appear with a matrix like this, you need to multiply by a permutation matrix in each side of the matrix, this permutation matrix being something like this:

0 0 0 ... 0 0 1
0 0 0 ... 0 1 0
0 0 0 ... 1 0 0
. . . ... . . .
0 0 1 ... 0 0 0
0 1 0 ... 0 0 0
1 0 0 ... 0 0 0

Of course, the dimensions must be the same.
abelsiqueira at 2007-11-11 21:00:08 >