Passing a matrix from one methot to another
Apr 22, 2009 at 2:55pm UTC
Hi,
I call a method
matrix = matrix_read(iFile, lines, cols);
that will return a matrix.
There is a problem in the way I am making this call. Netbeans output:
main.cpp: In function 'double matrix_read(std::istream&, int, int)':
main.cpp:83: error: cannot convert 'double (*)[(((unsigned int)(((int)cols) - 1)) + 1u)]' to 'double' in return
main.cpp: In function 'int main(int, char**)':
main.cpp:100: error: incompatible types in assignment of 'double' to 'double [(((unsigned int)(((int)lines) - 1)) + 1u)][(((unsigned int)(((int)cols) - 1)) + 1u)]'
But I can not understandt what it means...
Did someone understant what I need to do for solving this?
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
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
/*
*
*/
int matrix_lines(istream &iFile) {
// Método para obter o número linhas da matriz.
iFile.clear();
iFile.seekg(0);
int lines = 0;
string line;
while (!iFile.eof()) {
getline (iFile, line);
lines++;
}
cout << "Número de linhas: " << lines-1 << endl;
return lines-1;
}
int matrix_cols(istream &iFile) {
// Método para obter o número de colunas da matriz.
iFile.clear();
iFile.seekg(0);
string line;
double n;
int cols = 0;
getline (iFile, line);
// cout << line << endl;
stringstream is(line);
n = 0;
while (is >> n) {
// cout << n << endl;
cols++;
}
//
cout << "Número de colunas: " << cols << endl;
return cols;
}
double matrix_read(istream &iFile, int lines, int cols) {
// Método para ler a matriz.
double matrix[lines][cols];
double value, valueOut;
iFile.clear();
iFile.seekg(0);
for (int i = 0; i < lines; i++){
for (int j = 0; j < cols; j++){
valueOut = (iFile >> value,value);
cout << valueOut << " " ;
matrix[i][j] = valueOut;
// cout << iFile.get() << " ";
}
cout << "\n" ;
}
// cout << "Now values in memory:" << endl;
// for(int i = 0; i < lines; i++){
// for(int j = 0; j < cols; j++){
// cout << matrix[i][j] << " ";
// }
// cout << "\n";
// }
return matrix;
}
int main(int argc, char ** argv) {
int lines,cols;
ifstream iFile;
iFile.open("/home/cristiano/matrix3.dat" );
if (!iFile) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
cols = matrix_cols(iFile);
lines = matrix_lines(iFile);
double matrix[lines][cols];
matrix = matrix_read(iFile, lines, cols);
// cout << "oi: " << oi << endl;
iFile.close();
return (EXIT_SUCCESS);
}
Apr 22, 2009 at 2:59pm UTC
Last edited on Apr 22, 2009 at 2:59pm UTC
Apr 24, 2009 at 11:02pm UTC
Loading data. Please wait...
Last edited on Apr 24, 2009 at 11:04pm UTC
Apr 24, 2009 at 11:05pm UTC
Hi,
how can I do to pass
matrix
from method
matrix_read
to
main
?
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
/*
* File: main.cpp
* Author: cristiano
*
* Created on April 22, 2009, 11:32 AM
*/
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
/*
*
*/
int matrix_lines(istream &iFile) {
// Método para obter o número linhas da matriz.
iFile.clear();
iFile.seekg(0);
int lines = 0;
string line;
while (!iFile.eof()) {
getline (iFile, line);
lines++;
}
cout << "Número de linhas: " << lines-1 << endl;
return lines-1;
}
int matrix_cols(istream &iFile) {
// Método para obter o número de colunas da matriz.
iFile.clear();
iFile.seekg(0);
string line;
double n;
int cols = 0;
getline (iFile, line);
// cout << line << endl;
stringstream is(line);
n = 0;
while (is >> n) {
// cout << n << endl;
cols++;
}
//
cout << "Número de colunas: " << cols << endl;
return cols;
}
double ** matrix_read(istream &iFile, int lines, int cols) {
// Método para ler a matriz.
vector<vector<double > > matrix;
matrix.resize(cols);
for (int i = 0; i < cols; i++)
matrix[i].resize(lines);
double value;
iFile.clear();
iFile.seekg(0);
for (int i = 0; i < lines; i++){
for (int j = 0; j < cols; j++){
matrix[i][j] = (iFile >> value,value);
cout << matrix[i][j] << " " ;
}
cout << "\n" ;
}
return matrix;
}
int main(int argc, char ** argv) {
int lines,cols;
ifstream iFile;
iFile.open("/home/cristiano/matrix3.dat" );
if (!iFile) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
cols = matrix_cols(iFile);
lines = matrix_lines(iFile);
// vector<vector<double> > matrix;
// matrix.resize(cols);
// for(int i = 0; i < cols; i++)
// matrix[i].resize(lines);
matrix_read(iFile, lines, cols);
iFile.close();
return (EXIT_SUCCESS);
}
Topic archived. No new replies allowed.