-1

I've been trying to write a program which takes a list of numbers, entered by the user, which are stored in a 2-dimensional array (like a matrix). The program prints it out in matrix form, then uses a function to transpose the array, and then the transposed array is printed.

Here is the program:

#include <iostream>
using namespace std;

void transpose(int length, int width, int input[][length], int output[][width])
{
    for(int j=0; j<length; j++)
        for(int i=0; i<width; i++)
        {
            output[j][i]=input[i][j];
        }
}


int main()
{
    int width;
    int length;

    cout << "enter the width followed by the length of the matrix: ";
    cin >> width >> length;

    int input[width][length];
    int output[length][width];

    cout << "enter elements \n";

    for(int j=0; j<length; j++)
        for(int i=0; i<width; i++)
        {
            cin >> input[i][j];
        }

    cout << "your matrix is: \n";
    for(int j=0; j<length; j++)  //output in matrix form
        for(int i=0; i<width; i++)
        {
            cout << input[i][j] << " ";
            if(i==width-1)
                cout << endl;
        }

    /*for(int j=0; j<length; j++)     //to check if transpose works
     for(int i=0; i<width; i++)
     {
     output[j][i]=input[i][j];
     }*/

    transpose(length, width, input, output);

    cout << "the transpose is: \n";
    for(int j=0; j<width; j++)
        for(int i=0; i<length; i++)
        {
            cout << output[i][j] << " ";
            if(i==length-1)
                cout << endl;
        }

    return 0;
}

When I try to call the function in main, it gives the error: "No matching function for call to 'transpose'" and then says "Candidate function not viable: no known conversion from 'int [width][length]' to 'int (*)[length]' for 3rd argument". I don't understand why. I've tried searching but I've had no luck (although it's entirely possible I didn't understand the solutions, or I was searching for the wrong thing).

In case it's relevant, I'm on a Mac using Xcode.

James
  • 1
  • 1
    Use `std::vector` instead. – Jarod42 Jul 14 '14 at 18:36
  • I recommend storing matrices in a 1D array and pass only a pointer to the first value to the function. Parameters like int input[][length] are very confusing. – dari Jul 14 '14 at 18:38
  • Look at this: http://www.programmingsimplified.com/c-program-transpose-matrix – SerhatCan Jul 14 '14 at 18:43
  • dari: Using a 1D array should make it work, it would definitely be simpler, but I'd like to understand how to use a 2 dimensional array if it's possible – James Jul 14 '14 at 18:46

2 Answers2

0

The problem probably stems from using variable length arrays. These aren't standard until c++14. What your compiler might be doing is casting it to an int *[]. You can either use a vector (or other stl container) as @Jarod42 suggested, or actually specify the size

DTSCode
  • 1,062
  • 9
  • 24
0

int input[width][length] with non compile time value is a variable length array which is a possible extension of some compilers.

Prefer using std::vector: Live example

#include <iostream>
#include <vector>

std::vector<std::vector<int>>
transpose (const std::vector<std::vector<int>>& input)
{
    std::vector<std::vector<int>> res(input[0].size(), std::vector<int>(input.size()));
    for (std::size_t i = 0; i != input.size(); ++i) {
        for (std::size_t j = 0; j != input[i].size(); ++j) {
            res[j][i] = input[i][j];
        }
    }
    return res;
}

void print(const std::vector<std::vector<int>>& m)
{
    for (std::size_t i = 0; i != m.size(); ++i) {
        for (std::size_t j = 0; j != m[i].size(); ++j) {
            std::cout << m[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

int main ()
{
    int width;
    int length;

    std::cout << "enter the width followed by the length of the matrix: ";
    std::cin >> width >> length;

    std::vector<std::vector<int>> input(width, std::vector<int> (length));
    std::cout << "enter elements \n";
    for (int j = 0; j < length; j++)
        for (int i = 0; i < width; i++) {
            std::cin >> input[i][j];
        }

    std::cout << "your matrix is: \n";
    print(input);
    std::vector<std::vector<int>> output = transpose(input);

    std::cout << "the transpose is: \n";
    print(output);
    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • There's a lot there I don't understand, so I'll have to go away and learn more I suppose. Is there any particular reason you're avoiding using namespace std? – James Jul 14 '14 at 18:57
  • You may read [why-is-using-namespace-std-considered-bad-practice](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). And feel free to ask what you don't understand. – Jarod42 Jul 14 '14 at 19:02
  • I simply haven't seen most of it, so it's probably better to try and learn it myself before I ask. I've been going through this: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-096-introduction-to-c-january-iap-2011/index.htm, the problem I've been trying to do is in the second set of questions. I had no problem writing the function, but in their solution they don't show how to call the function in main. I assumed it would be possible using only the stuff covered in the first 5 lectures. If it isn't, I might as well move on and come back when I've learned more. – James Jul 14 '14 at 19:07