0

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
5
  • When you used the debugger, and watched the variables, which statement is causing the issue? Commented Oct 24, 2015 at 18:24
  • Your output does not match what your code is performing, especially the "After column change" text, it should be printing. Commented Oct 24, 2015 at 18:24
  • Why don't you create a 2d array? Commented Oct 24, 2015 at 18:25
  • @ThomasMatthews it does he didn't copy what is actually in the console. Commented Oct 24, 2015 at 18:29
  • 1
    @JawadLeWywadi: One of my points, the posted output is not from the code he posted. Commented Oct 24, 2015 at 18:32

3 Answers 3

1

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]

Sign up to request clarification or add additional context in comments.

1 Comment

And then use the dimCol for arr[row*dimCol+1]...ect Thanks for the help! ^^
1

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]

Comments

1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.