-1

I want to tranpose a matrix using vector?

//I am trying to 'tranpose' a vector matrix, but it's not running

#include <iostream>
#include <vector>

using namespace std;

// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
    int n = arr.size();
    int m = arr[0].size();

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
int temp = arr[i][j];

            arr[i][j] = arr[j][i];
            arr[j][i] = temp;
        }}}

// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
    int n = arr.size();
    int m = arr[0].size();

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m/2; j++) {

            int temp = arr[i][j];
            arr[i][j] = arr[i][j-1-i];
            arr[i][j-1-i] = temp;
        }}}

vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
                         {9,10,11,12},
                         {13,14,15,16} };

int main() {
    transpose(arrh);
    swap_vector(arrh);

    for (int i=0; i<arrh.size(); i++) {
        for (int j=0; j<arrh[0].size(); j++) {
            cout << arrh[i][j] << " ";}
        cout << std::endl;}

    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
wise john
  • 5
  • 3
  • 1
    What does "not running" mean? What **is** it doing? – Drew Dormann Oct 16 '22 at 23:52
  • 1
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Oct 16 '22 at 23:54
  • 1
    Your `transpose` is actually `transpose(transpose)`, so to speak... – Evg Oct 16 '22 at 23:54
  • Yep... you seem to be transposing every element twice. If you swap `arr[1][0]` with `arr[0][1]`, and then later swap `arr[0][1]` with `arr[1][0]`, you'll just have the matrix you started with. [Your debugger](https://stackoverflow.com/questions/25385173) would have shown you this quickly. Why not use a debugger? – Drew Dormann Oct 16 '22 at 23:54

1 Answers1

0
#include <iostream>
#include <vector>

using namespace std;

// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
    int n = arr.size();
    int m = arr[0].size();

    for (int i = 0; i < n; i++) {
        for (int j = i; j < m; j++) {

            int temp = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = temp;

        }
    }
}

// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
    int n = arr.size();
    int m = arr[0].size();

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m/2; j++) {

            int temp = arr[i][j];
            arr[i][j] = arr[i][m-1-j];
            arr[i][m-1-j] = temp;
        }
    }
}

vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
                         {9,10,11,12},
                         {13,14,15,16} };

int main() {
    transpose(arrh);
    swap_vector(arrh);

    for (int i=0; i<arrh.size(); i++) {
        for (int j=0; j<arrh[0].size(); j++) {
            cout << arrh[i][j] << " ";
        }
        cout << std::endl;
    }

    return 0;
}
James Risner
  • 5,451
  • 11
  • 25
  • 47
wise john
  • 5
  • 3
  • Why would you swap elements `arr[i][i]` and `arr[i][i]`? Is there any chance they are different? ;) – Evg Oct 23 '22 at 16:40