Título: Ayúdame en este programa!! Publicado por: MJI en 22 Diciembre 2018, 17:12 pm Hola, soy bastante novato con el tema del C++, sobre todo a la hora de mezclar ficheros con funciones....
Me gustaría hacer un programa que leyese de fichero una matriz cuadrada, primero el numero de filas, seguido del numero de columnas, y para finalizar que recorra toda la matriz y diga si hay algún elemento repetido. Mi intento de programa es el siguiente: #include <iostream> #include <fstream> #include <string> using namespace std; const int MAX1 = 10; const int MAX2 = 10; typedef int Matriz[MAX1][MAX2]; void Presentacion(void); bool LeerFilyCol(ifstream& f); int SacarFilyCol(ifstream& f); int ElementosVector(ifstream& f); void GuardarVector(ifstream& f, int totalnums, Matriz m); bool ElemRepVec(Matriz m, int totalnums); int main(void) { bool FilyCol, Rep; int totalnums, filcol; Matriz m1; ifstream f; string nomf; Presentacion(); cout << "Dame fichero con matriz: "; cin >> nomf; cout << endl; f.open(nomf); if(!f) cout << "Error al abrir el archivo" << endl; else { FilyCol = LeerFilyCol(f); if(FilyCol == false) { cout << "La matriz no cumple las condiciones" << endl; } else { totalnums = ElementosVector(f); filcol = SacarFilyCol(f); totalnums = totalnums / filcol; GuardarVector(f, totalnums, m1); ElemRepVec(m1, totalnums); if(Rep == false) cout << "NO hay numeros repetidos en la matriz" << endl; if(Rep == true) cout << "Hay numeros repetidos en la matriz" << endl; } } f.close(); return 0; } void Presentacion(void) { cout << "Este programa determina si existe...." << endl << endl; return; } bool LeerFilyCol(ifstream& f) { bool trueofalse; int fil, col; f >> fil >> col; if (fil != col || fil > MAX1 || col > MAX2) trueofalse = false; else trueofalse = true; return trueofalse; } int ElementosVector(ifstream& f) { int num; int numdenums = 0; int ignorar; f >> ignorar >> ignorar; while(f >> num) numdenums++; return numdenums; } int SacarFilyCol(ifstream& f) { int filcol; f >> filcol; return filcol; } void GuardarVector(ifstream& f, int totalnums, Matriz m) { int num, i, j, ignorar; f >> ignorar >> ignorar; for (i = 0; i < totalnums; i++) { for (j = 0; j < totalnums; j++) { f >> num; m[j] = num; } } return; } bool ElemRepVec(Matriz m, int totalnums) { int i, j, f, c; bool rep = false; for (f = 0; f < totalnums; f++) { for (c = 0; c < totalnums; c++) { for (i = 0; i < totalnums; i++) { for (j = 0; j < totalnums; j++) { if (m[f][c] == m[j] && (f != i) && (c != j)) rep = true; } } } } return rep; } El programa compila y ejecuta perfectamente, pero no hace lo que debería de hacer, alguien sabe como solucionarlo? Título: Re: Ayúdame en este programa!! Publicado por: CalgaryCorpus en 22 Diciembre 2018, 20:15 pm Asignarle algún valor a la variable Rep
|