Transpose of matrix (15x10) using vector of vector

Hello! I wanna display the transpose of a (15x10) matrix but I'm encountering a few errors that I can't seem to fix. Here are my progress so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include <iostream>
#include <vector>

using namespace std;

void generarMatriz(vector<vector<int> > &m){
    cout << "Introduce los valores de la matriz ordenados por fila y columna: " << endl;
    for (int i = 0; i < m.size(15); i++)//rows
        for (int j = 0; j < m[0].size(10); j++){//columns
            cout << "Introduce el valor de la fila " << i << " y columna " << j << " : ";
            cin >> m[i][j];
        }

}

void escribirMatriz(vector<vector<int> > &m){
    for (int i = 0; i < m.size(15); i++){
        for (int j = 0; j < m[0].size(10); j++)
            cout << m[i][j] << "\t" ;
        cout << endl;
    }
    cout << endl;
}

void transponer (vector<vector<int> > transponer(vector<vector<int> > &m)){
    vector<vector<int> > nueva(m[0].size(10),vector<int>(m.size(15),0.0));
    for ( int i = 0 ; i < m.size(10) ; i++ ){
        for ( int j = 0 ; j < m[0].size(15) ; j++ )
            nueva[j][i] = m[i][j];

    m = nueva;

    }

}



int main()
{

vector <vector<int> > m1(kMAXDIM,vector<int> (kMAXDIM)),
                             m2(kMAXDIM,vector<int> (kMAXDIM)),
                             result(kMAXDIM,vector<int> (kMAXDIM));


        generarMatriz(m);
                cout << "Matriz inicial: " << endl;
                escribirMatriz(m);
                transponer(m);
                cout << "Matriz transpuesta " << endl;
                escribirMatriz(m);



    return 0;
}




Here are the erorrs:

|8|error: no matching function for call to 'std::vector<std::vector<int> >::size(int)'|
|9|error: no matching function for call to 'std::vector<int>::size(int)'|
|17|error: no matching function for call to 'std::vector<std::vector<int> >::size(int)'|
|18|error: no matching function for call to 'std::vector<int>::size(int)'|
|26|error: 'm' was not declared in this scope|
|42|error: 'kMAXDIM' was not declared in this scope|
|47|error: 'm' was not declared in this scope|
Last edited on

m.size(15)

The vector::size() method is nullary. It doesn't take any parameters. Here you pass it an int, which is why you are getting an error.
So how do i form a 15x10 matrix since it's not a square matrix?
You already formed a matrix (vector of vectors) on line 42, 43, 44.

Your main function does not have a variable called m.
You have m1, m2, result defined.

The size() method of vector doesn't take any arguments, as Toaster already said.
(Requiring a size function to take in a size would defeat the purpose of a size function...)

Edit: You also don't have kMAXDIM defined.
if you want one dimension to be different than the other dimension, then you want something like:
vector<int>(rows, vector<int>(columns))
Last edited on
You need to first initialize the vector with 15 subvectors, who have a size of 10:

std::vector<std::vector<int>> a{15, std::vector<int>(10)};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 #include <iostream>
#include <vector>

using namespace std;


void generarMatriz (vector<int> a{15,vector<int>(10))}){

    cout << "Introduce los valores de la matriz ordenados por fila y columna: " << endl;
    for (int i = 0; i < m.size(); i++)//rows
        for (int j = 0; j < m[0].size(); j++){//columns
            cout << "Introduce el valor de la fila " << i << " y columna " << j << " : ";
            cin >> m[i][j];
        }

}

void escribirMatriz (vector<int> a{15,vector<int>(10)}){
    for (int i = 0; i < m.size(); i++){
        for (int j = 0; j < m[0].size(); j++)
            cout << m[i][j] << "\t" ;
        cout << endl;
    }
    cout << endl;
}

void transponer (vector<vector<int> > transponer(vector<vector<int> > &m)){
    vector<vector<int> > nueva(m[0].size(),vector<int>(m.size(),0.0));
    for ( int i = 0 ; i < m.size() ; i++ ){
        for ( int j = 0 ; j < m[0].size() ; j++ )
            nueva[j][i] = m[i][j];

    m = nueva;

    }

}



int main()
{

vector <vector<int> > m1(kMAXDIM,vector<int> (kMAXDIM)),
                             m2(kMAXDIM,vector<int> (kMAXDIM)),
                             result(kMAXDIM,vector<int> (kMAXDIM));


        generarMatriz(m);
                cout << "Matriz inicial: " << endl;
                escribirMatriz(m);
                transponer(m);
                cout << "Matriz transpuesta " << endl;
                escribirMatriz(m);



    return 0;
}


Errors:
|7|error: expected ')' before '{' token|
|7|error: expected ';' before ')' token|
|7|error: expected unqualified-id before ')' token|
Edit:
Aside from the mismatching ( {, }, ), that's not how you pass a vector to a function.

Do:
void myFunction(vector<vector<int>>& m) { ... }

Yes, it looks horrible. (This is one reason why I tend to avoid multi-dimensional vectors or arrays)
Last edited on
consider making your vector<vector<thing>> a type masked name.
consider passing nueva into the function as a reference to save a copy. then just directly assign the result in the loop. The extra copy can be avoided entirely in a number of ways saving time.

you cannot do the initialize in the function parameter list. you have to do it before the function is called or inside the function.
Last edited on
Can you guys help me out and edit my codes. I've been working on it for 12 hours I'm losing my mind T_T
This is how your function headers should look like:
void generarMatriz (vector<vector<int>>& a)

This is how you should pass it in main()
1
2
3
4
5
vector<vector<int>> m1{15, std::vector<int>(10)}, m2{15, std::vector<int>(10)}, result{15, std::vector<int>(10)};

generarMatriz(m);
//...
Last edited on
Topic archived. No new replies allowed.