user5483361
user5483361

Reputation:

Wrong output while printing a multidimensional array

i have some trouble while printing this pseudo-multidimensional array , with elements that are set already. And the point of it is to swap the first and third row and 2nd and 4th column, but the output looks weird...

#include <iostream>

using namespace std;

int main()
{
 int arr[12]= {
 1,2,3,4,5,6,7,8,6,4,5,3
 };
 cout << "Before change: "<<endl;
 for (int row=0;row<3;row++){
    for (int col=0;col<4;col++){
        cout << arr[row*col]<<" ";
    }
    cout <<endl;
 }
 cout << "After the row change: "<<endl;
for (int row=2;row>=0;row--){
    for(int col=0;col<4;col++){
        cout<<arr[row*col]<<" ";
    }
    cout<<endl;
}
 cout << "After the column change: "<<endl;
int temp;
for(int row=0;row<3;row++){
        temp=arr[row*1];
        arr[row*1]=arr[row*3];
        arr[row*3]=temp;
        for (int col=0;col<4;col++){
            cout<<arr[row*col]<<" ";
        }
        cout<<endl;
 }

Instead of having an output like this:

1 2 3 4
5 6 7 8
6 4 5 3

6 4 5 3
5 6 7 8
1 2 3 4

6 3 5 4
5 8 7 6
1 4 3 2

i get this :

1 1 1 1
1 2 3 4
1 3 5 7

1 3 5 7
1 2 3 4
1 1 1 1

1 1 1 1
1 4 3 2
1 7 5 3

Upvotes: 0

Views: 79

Answers (3)

The loop you are using multiplies each time your variable with Zero that's why you are getting 1 at the beginning of every line as your first element is 1, and arr[0] is 1.

*and your line 1 is

1 1 1 1*

because value of outer loop is zero and any value of variable of inner loop multiplied will result in 0.

the reason you are not getting correct output is your logic to print all element is not correct. go for

array[row*4 + col]

Upvotes: 1

Simone Pessotto
Simone Pessotto

Reputation: 1581

You wrong how to calculate the element inside the array arr[row*col] will be always 0 for the first row ( row = 0). So you have to do something like this:

#include <iostream>

using namespace std;

int main()
{
    int arr[12] = {
        1, 2, 3, 4, 5, 6, 7, 8, 6, 4, 5, 3
    };

    **int dimCol = 4;**

    cout << "Before change: " << endl;
    for (int row = 0; row < 3; row++){
        for (int col = 0; col < 4; col++){
            cout << arr[**(row*dimCol) + col**] << " ";
        }
        cout << endl;
    }
    cout << "After the row change: " << endl;
    for (int row = 2; row >= 0; row--){
        for (int col = 0; col < 4; col++){
            cout << arr[**(row*dimCol) + col**] << " ";
        }
        cout << endl;
    }
    cout << "After the column change: " << endl;
    int temp;
    for (int row = 0; row < 3; row++){
        temp = arr[row * 1];
        arr[row * 1] = arr[row * 3];
        arr[row * 3] = temp;
        for (int col = 0; col < 4; col++){
            cout << arr[**(row*dimCol) + col**] << " ";
        }
        cout << endl;
    }
}

The formule will be: array[row*numberOfColumn + Column]

Upvotes: 1

Lasoloz
Lasoloz

Reputation: 262

You got a wrong argument definition:

Instead of

array[row*col]

write this

array[row*4 + col]

So the formula is:

array[row*total_col + col]

Upvotes: 1

Related Questions