1

So I have this homework, to print a matrix with 3 rows and 4 colums so far that I manage to do it somehow(mostly by reading this forum), since our profesor in University won't explain how this things are done. So cut to the point. My code looks like this. I manage to print the matrix , then I really don't know how those things works , so I try to just move row 1 in temp matrix , then move it back. But this really dosen't look right but I really dont know any other way to do that. What should I do?

int matrix[3][4] = { { 1,2,3,3 },{ 4,5,6,2 },{ 7,8,9,3 } };
 int temp[3][4];

 for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++)

        cout << "  " << matrix[i][j];
    cout << endl;
}


for (int i = 0; i < 3; i++) {

    temp[1][4] = matrix[1][4];
    matrix[3][4] = matrix[1][4];
    matrix[1][4] = temp[1][4];
}
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++)

        cout << "  " << matrix[i][j];
    cout << endl;
}


    return 0;

}
Nikola
  • 53
  • 7
  • 1
    what are you trying to achieve. pls tell. glad to help :) – m0bi5 Oct 24 '15 at 16:32
  • 1
    I agree that it is not clear what you want to do. Anyway, your second loop is certainly wrong. Think about what it does. It iterates 3 times, without ever using the variable `i`, so that all 3 times are the same. And what does an iteration do? Only this: `matrix[3][4] = matrix[1][4];`. Nothing more (ok, it's also setting `temp[1][4]`). If you want to swap, the correct order is: `temp = a; a = b; b = temp;`. So in your case: `temp[1][4] = matrix[1][4]; matrix[1][4] = matrix[3][4]; matrix[3][4] = temp;`. And honestly you don't need a matrix of temp variable, you just need one. – Fabio says Reinstate Monica Oct 24 '15 at 16:41

2 Answers2

2

There is standard function std::swap declared in header <utility> that allows to swap two arrays. Otherwise you can write an appropriate code yourself.

Here is a demonstrative program that shows the both approaches.

#include <iostream>
#include <utility>


int main()
{
    const size_t M = 3;
    const size_t N = 4;

    int matrix[M][N] = 
    { 
        { 1, 2, 3, 3 },
        { 4, 5, 6, 2 },
        { 7, 8, 9, 3 } 
    };

    for ( const auto &row : matrix )
    {
        for ( int x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }

    std::cout << std::endl;

    std::swap( matrix[0], matrix[2] );

    for ( const auto &row : matrix )
    {
        for ( int x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }

    std::cout << std::endl;

    for ( size_t i = 0; i < N; i++ )
    {
        int tmp = matrix[0][i];
        matrix[0][i] = matrix[2][i];
        matrix[2][i] = tmp;
    }

    for ( const auto &row : matrix )
    {
        for ( int x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }

}

Its output is

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

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

1 2 3 3 
4 5 6 2 
7 8 9 3 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thx for the help , I needed the last form with tmp :). You are the best. However next is to swap col 2 and col 4. I Tried your method , but this time it dosent work it swap the elements but print only the last row 7893 and it swaped really to 7398 but the other 2 rows miss why :( for (int i = 0; i < 3; i++) { int tmp1 = matrix[i][1]; matrix[i][1] = matrix[i][3]; matrix[i][3] = tmp1; No idea why it does quote like a code!!! I put 4 spaces ..... – Nikola Oct 24 '15 at 17:55
  • @Nikola Here is the loop for ( size_t i = 0; i < M; i++ ) { int tmp = matrix[i][1]; matrix[i][1] = matrix[i][3]; matrix[i][3] = tmp; } – Vlad from Moscow Oct 24 '15 at 18:01
  • @Nikola I am sorry. I do not understand what does not work. I showed code in the preceding comment how to swap two columns. – Vlad from Moscow Oct 24 '15 at 21:10
  • when I type it like you told it just print after that just one row the other 2 rows are not printed , dunnow why :( – Nikola Oct 25 '15 at 01:14
  • @Nikola Use for loops to output the array for ( size_t i = 0; i < M; i++ ) { for ( size_t j = 0; j < N; j++ ) std::cout << matrix[i][j]; std::cout << std::endl; } – Vlad from Moscow Oct 25 '15 at 08:46
1

if you want to exchange the second row and the third row you can do like this,remember array's index begins at 0 instead of 1

    for (int i = 0; i < 4; i++) {
        temp = matrix[1][i];
        matrix[1][i] = matrix[2][i];
        matrix[2][i] = temp;
    }
龚成玥
  • 111
  • 10