Dev - C++ - Matrices en c++

 
Vista:

Matrices en c++

Publicado por Jorge E Güisa R (2 intervenciones) el 22/03/2018 07:40:24
Tengo un programa en c++ para leer una matriz. y muestra la matriz pero con 0.0000 en todas las posiciones. Si alguien me ayuda le agradecería.

Programa

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
using std::cout;
using std::cin;
using std::endl;
 
int FILAS = 15 ;
int COLS = 4 ;
 
int a = 20 ;
 
 
 
int** leerMatriz(char *fileName) {
	FILE *fp = fopen(fileName, "r");
	if (fp == NULL) {
		exit(EXIT_FAILURE);
	}
	char linea[50];
	fgets(linea, 49, fp);
	char *token = strtok(linea, " ");//10 6
	FILAS = atoi(token);
	token = strtok(NULL, " ");
	COLS = atoi(token);
	int **matriz = new int*[FILAS];
	for (int i = 0; i < FILAS; i++) {
		matriz[i] = new int[COLS];
		fgets(linea,49, fp);
		token = strtok(linea, " ");
		matriz[i][0] = atoi(token);
		for (int j = 1; j < COLS; j++) {
			token = strtok(NULL, " ");
			matriz[i][j] = atoi(token);
		}
	}
	fclose(fp);
	return matriz;
}
 
 
 
int main()
{
 
 
 
	int **Experiencia = leerMatriz("Realizados.txt");
 
    a = FILAS;
    puts("");
    printf("     El valor de A es       %d \n", a);
    puts("");
 
 
 
 
puts("");
puts("");
cout << "      La matriz completa es:   ";
puts("");
 
 
    int k = 0 ; /* indices para filas */
    int Contratos = 0 ; /* indices los contratos a contar */
 
    puts("------------------------");
 
 
    for (Contratos; Contratos<(FILAS); Contratos++) {
       for (k=0; k<COLS; k++) {
          printf("     %8.3f  ", Experiencia[Contratos][k] );
          }
          puts("");
       }
 
system("pause");
 
 
 
    printf("         Fin del programa \n");
    puts("");
    puts("");
    puts("");
 
    system("pause");
 
    return 0;
}


Matriz
la primera fila son las filas y las columnas26 4
64.001 1099.016 2.533 101.500
63.001 0437.152 14.667 735.170
59.001 4224.130 31.233 2008.080
56.001 3765.130 8.600 725.602
44.001 1278.091 32.700 1067.031
41.001 2583.012 12.767 298.350
40.001 2584.120 12.867 199.020
36.001 2079.111 13.867 233.240
34.001 2077.110 17.400 252.290
33.001 2102.111 17.533 100.576
32.001 1371.111 7.167 363.310
31.001 0989.111 11.267 396.960
30.001 1653.111 4.133 60.740
28.001 0568.101 9.100 307.780
27.001 0771.100 12.667 856.716
24.001 2648.081 4.000 77.787
23.001 2366.071 8.100 311.710
19.001 1083.051 4.000 88.911
18.001 1683.041 9.600 725.110
17.001 0503.041 4.800 532.982
16.001 0582.031 13.033 1224.879
13.001 0086.021 6.067 566.692
11.001 0293.021 4.067 218.480
10.001 0345.021 2.567 119.360
5.001 0373.011 7.633 590.070
8.002 0026.071 2.067 19.830
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 417
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Matrices en c++

Publicado por Thanatos (199 intervenciones) el 22/03/2018 20:50: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
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
 
using std::cerr;
using std::cin;
using std::cout;
using std::fixed;
using std::ifstream;
using std::ios;
using std::setprecision;
using std::setw;
using std::string;
using std::vector;
 
void leerMatriz(const string &, vector<vector<float>> &);
void mostrarMatriz(const vector<vector<float>> &);
 
int main() {
    const string archivo = "Realizados.txt";
    const size_t kFils = 26;
    const size_t kCols = 4;
    vector<vector<float>> matriz(kFils, vector<float>(kCols, 0.0f));
 
    leerMatriz(archivo, matriz);
    mostrarMatriz(matriz);
 
    cin.get();
    return 0;
}
 
void leerMatriz(const string &archivo, vector<vector<float>> &matriz) {
    ifstream ifs(archivo, ios::in);
    if (!ifs.is_open()) {
        cerr << "\n* Error al abrir el archivo *\n";
        return;
    }
 
    for (size_t i = 0; i < matriz.size(); ++i) {
        for (size_t j = 0; j < matriz[i].size(); ++j) {
            ifs >> matriz[i][j];
        }
    }
}
 
void mostrarMatriz(const vector<vector<float>> &matriz) {
    for (const vector<float> &fila : matriz) {
        for (const float &numero : fila) {
            cout.setf(ios::fixed, ios::floatfield);
            cout << setw(9) << setprecision(3) << numero;
        }
        cout << '\n';
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 417
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Matrices en c++

Publicado por Thanatos (199 intervenciones) el 23/03/2018 21:05:58
Es posible que el compilador no reconozca correctamente los símbolos >>, que están al final de los parámetros del template vector. Si ese es el problema, se puede solucionar agregando un espacio entre ellos.

En lugar de:
1
vector<vector<float>>

Se tendría:
1
vector<vector<float> >

Si eso no soluciona el problema, es posible que el compilador que estás utilizando sea antiguo. El código debería compilar correctamente con compiladores que soporten el estándar C++ del año 2011 o algún estándar más reciente.

Subí el código y el archivo de texto a este sitio, ahí puedes compilarlo y ejecutarlo para verificar su funcionamiento. Las únicas diferencias que encontrarás en este código y el que publiqué en el foro son estas:

1. Modifiqué la cadena del archivo de texto (línea 21), para que el ejecutable pueda encontrar el archivo de texto en el directorio online.
2. Puse la línea 29 como comentario, porque ahí no es necesario pausar la consola para ver la salida del programa.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar