Loop for fraction matrix

Hey. How do I modify these codes:

1
2
3
4
5
6
7
8
9
10
11
 void generateMatrix(vector <vector<double> > &m){

    cout << "Introduce the values of the matrix by rows and columns: " << endl;

    for (int i = 0; i < m.size(); i++)
        for (int j = 0; j < m[0].size(); j++){
            cout << "Introduce the value of the row " << i << " and column " << j << " : ";
            cin >> m[i][j];
        }

}


Into a loop that asks for each numerator and denominator by rows and columns, USING Struct "tpFraction".
I'm working with my codes below:

1
2
3
4
5
6
7
8
9
10
11
12
13
void generateMatrix ( vector < vector < tpFraction > > & m ){
    int num1, num2, den1, den2, i, j;
    cout << "Introduce the values of the matrix by rows and columns: " << endl;
    for (num1 = 0; num1 < m.size(); num1++)
    for (den1 = 0; den1 < m.size(); den1++)
        for (int num2 = 0; num2 < m[0].size(); num2++)
        for (int den2 = 0; den2 < m[0].size(); den2++){
            cout << "Introduce the value of the row " << num1 << "/" << den1 << " and column " << num2 << "/" << den2 << " : ";
            
            cin >> m[i][j];

}
}


Thanks in advance.
You haven't shown the declaration of tpFraction.

Line 10: i and j are uninitialized variables.

Line 10: Since you say that tpFraction is a struct, I would assume that it does not overload the >> operator, therefore cin does not know how to input a tpFraction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <vector>
#include <iostream>
using namespace std;
struct tpFraction
{	double	num;
	double	den;
};
void generateMatrix(vector < vector < tpFraction > > & m) 
{	int i, j;
	cout << "Introduce the values of the matrix by rows and columns: " << endl;
	for (i = 0; i < m.size(); i++)
		for (j = 0; j < m.size(); j++)
		{	cout << "Enter the numerator for row " << i << " and column " << j << " : ";
			cin >> m[i][j].num;
			cout << "Enter the denominator for row " << i << " and column " << j << " : ";
			cin >> m[i][j].den;
		}
}



I will be modifying the codes below for tpFraction:

1
2
3
4
5
void displayMatriz ( vector < vector < tpFraction > > & m ){
    struct tpFraction {
        cout << (m)[][] << "\t" ;
};
}
Last edited on
That makes no sense.
Here is an example of another coding work but they're in Spanish.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <vector>

using namespace std;

const int kMAXDIM = 3;


void leeMatriz(vector <vector<double> > &m){

    cout << "Introduce los valores de la matriz ordenados por fila y columna: " << endl;

    for (int i = 0; i < m.size(); i++)
        for (int j = 0; j < m[0].size(); j++){
            cout << "Introduce el valor de la fila " << i << " y columna " << j << " : ";
            cin >> m[i][j];
        }

}

void escribeMatriz(vector <vector<double> > &m){

    for (int i = 0; i < m.size(); i++){
        for (int j = 0; j < m[0].size(); j++)
            cout << m[i][j] << "\t" ; // añade un tabulador tras cada valor, para mejorar la visualización
        cout << endl;
    }
    cout << endl;
}


vector<vector<double> > sumaMatrices(vector<vector<double> > &m1, vector<vector<double> > &m2){

    vector<vector<double> > resultado(m1.size(),vector<double>(m1[0].size()));

    for (int i = 0; i < resultado.size(); i++)
        for (int j = 0; j < resultado[0].size(); j++)
            resultado[i][j] = m1[i][j] + m2[i][j];

    return resultado;
}

vector<vector<double> > realxMatriz(double num, vector<vector<double> > &m){

    vector<vector<double> > resultado(m.size(),vector<double>(m[0].size()));

    for (int i = 0; i < resultado.size(); i++)
        for (int j = 0; j < resultado[0].size(); j++)
            resultado[i][j] = m[i][j] * num;

    return resultado;
}

// Función que multiplica m1 por m2 y devuelve la matriz multiplicación
// vector<vector<double> > multiplicaMatrices(vector<vector<double> > &m1, vector<vector<double> > &m2)

void transpuesta (vector<vector<double> > &m){
    vector<vector<double> > nueva(m[0].size(),vector<double>(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;
}


char muestraMenu(){

    char opcion;

    do{
        cout << "CALCULADORA DE MATRICES - MENU" << endl << endl;
        cout << "1. Transpone matriz" << endl;
        cout << "2. Calcula determinante de matriz" << endl;
        cout << "3. Multiplica matriz por real" << endl;
        cout << "4. Suma dos matrices" << endl;
        cout << "5. Multiplica dos matrices" << endl;
        cout << "0. Salir" << endl << endl;
        cin >> opcion;
    } while (opcion < '0' || opcion > '5');

    return opcion;
}


int main(){

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

    char opcionMenu;
    double escalar;

    do{
        opcionMenu = muestraMenu();

        switch (opcionMenu){

            case '0':
                cout << "Hasta luego" << endl;
                break;
            case '1':
                leeMatriz(m1);
                cout << "Matriz inicial: " << endl;
                escribeMatriz(m1);
                transpuesta(m1);
                cout << "Matriz transpuesta " << endl;
                escribeMatriz(m1);
                break;
            case '2':
                // COMPLETAR
                break;
            case '3':
                leeMatriz(m1);
                cout << "Matriz inicial: " << endl;
                escribeMatriz(m1);
                cout << "Introduce escalar: " ;
                cin >> escalar;
                result = realxMatriz(escalar, m1);
                cout << "Resultado: " << endl;
                escribeMatriz(result);
                break;
            case '4':
                cout << "Matriz m1: " << endl;
                leeMatriz(m1);
                cout << "Matriz m2: " << endl;
                leeMatriz(m2);
                result = sumaMatrices(m1, m2);
                cout << "m1 + m2 = " << endl;
                escribeMatriz(result);
                break;
            case '5':
                // COMPLETAR
                break;
        }

    } while (opcionMenu != '0');


    return 0;
}


I'm referring to "void leeMatriz" and "void escribeMatriz"
You can port leeMatriz and escribeMatriz if you create << and >> operators for tpFraction. The examples below are untested.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
istream & operator >>(istream &is, tpFraction &frac)
{
    char ch;
    is >> frac.num >> ch >> frac.denom;
    if (ch != '/') {
        is.setstate(std::ios::failbit);
    }
    return is;
}

ostream &operator << (ostream &os, tpFraction &frac)
{
    return os << frac.num << " / " << frac.denom;
}

Topic archived. No new replies allowed.