0

I have an assignment to find the transpose of a matrix (in c++) only using array indices. My main calls the function indexTranspose, but I cannot figure out why when I print the array inside the function the output is correct, but when I print it out in the main the matrix is not updated. The matrix is a text file full of 9 random ints.

#include <iostream>
#include <fstream>

using namespace std;

//function declarations
void printMatrix(int m[][3]);
void indexTranspose(int n[3][3]);
//void pointerTranspose(int m[][3]);

//begin main
int main() {
    int m[3][3];
    ifstream values("matrix.txt");
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            values >> m[i][j];
    printMatrix(m);
    indexTranspose(m);
    printMatrix(m);
    /*pointerTranspose(m);
    printMatrix(m);
*/
} //end main

void printMatrix(int m[][3]) {
    for (int i = 0; i < 3; i++) {
    cout << "[ ";
        for (int j = 0; j < 3; j++)
        cout << m[i][j] << " "; 
    cout << "]" << endl;
    }
    cout <<endl;
}

  void indexTranspose (int n[][3]) { 
    cout << "Transposing the matrix using indices..." <<endl;
    int temp[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) {
            temp[j][i] = n[i][j];
        }
    n = temp;
    cout << "Printing n"<< endl;
    printMatrix(n);
    }

The output I get when I run this function is the original matrix, followed by the transpose (printed out inside the function), and then the original again. I am not sure why the transpose function only updates the array locally instead of modifying the array in the main as well.

4
  • 6
    Arrays do not have proper value semantics. You can't assign an array to another directly. Consider using std::array instead. Commented Jan 22, 2018 at 16:16
  • 4
    n = temp; -- This does do what you think it does. Use standard containers, where = works as intended. Commented Jan 22, 2018 at 16:17
  • 4
    Assigning to a parameter has no effect outside the function. Commented Jan 22, 2018 at 16:17
  • Simply change to typedef std::array<std::array<int, 3>> Matrix, declare your 2d matrix like this, pass Matrix by reference, and then things will magically work. Commented Jan 22, 2018 at 16:22

1 Answer 1

2

You shouldn't try to re-assign n inside the indexTranspose function. Instead, swap individual values within the array. Swap i, j with j, i and make sure that you don't separately swap j, i with i, j later.

void indexTranspose (int n[][3])
{ 
    std::cout << "Transposing the matrix using indices..." << std::endl;
    for (int i = 0; i < 3; i++)
    {
        // the j < i ensures, that i, j are not swapped twice and i, i is never swapped because its the same index anyway
        for (int j = 0; j < i; j++)
        {
            std::swap(n[i][j], n[j][i]);
        }
    }
    std::cout << "Printing n"<< std::endl;
    printMatrix(n);
}

To use std::swap, add an include for <algorithm> (C++98) or <utility> (C++11 and above).

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.