![]() |
| ||
| Passing a matrix from main function to user defined function. 1 Attachment(s) Hello, I am in an entry level C++ course. We have a homework problem that requires us to prompt the user to enter five sets of three numbers (type double) and store the numbers in a 5 x 3 array called Data[5][3]. Then, pass the matrix Data[5 ][3 ] from main ( ) to a function float mean (const int Data [ ][ ], int, int ) that receives the array Data [5 ][3 ] and its dimensions as arguments to find the row and column mean of matrix Data and prints the results in function main ( ). Here is a copy of my syntax: #include <iostream> #include <iomanip> using namespace std; float mean(const double, int, int); int main() { const int row=2, col=3; double data[row][col]; float avgrows[5]; //Entering values for the matrix cout<<"Enter two sets of three number."<<endl; for(int i=0; i<row; i++) { for(int j=0; j<col; j++) { cout<<"Enter row "<<i+1<<": "; cin>>data[i][0]>>data[i][1]<<data[i][2]; } } //Stores row means into an array for(i=0; i<row; i++ ) { avgrows[i]=mean(data, row, col); } //Print array for(int x=0; x<5; x++) { cout<<avgrows[x]<<" "; } return 0; } float mean (const double data, int rows, int columns) { float average, sum=0; for(int i=0; i<rows; i++) { for(int j=0; j<columns; j++) { sum+=data[i][j]; } average=sum/columns; return average; } } Once I compile this cpp file, I get 4 errors, they are: --------------------Configuration: matrix passing - Win32 Debug-------------------- Compiling... matrix passing.cpp 1.) error C2676: binary '<<' : 'class std::basic_istream<char,struct std::char_traits<char> >' does not define this operator or a conversion to a type acceptable to the predefined operator 2.)error C2664: 'mean' : cannot convert parameter 1 from 'double [2][3]' to 'const double' There is no context in which this conversion is possible 3.) error C2109: subscript requires array or pointer type 4.) error C2109: subscript requires array or pointer type Error executing cl.exe. matrix passing.exe - 4 error(s), 0 warning(s) I can't figure what the error mean, nor can I figure out what else to do. I've been racking my brain the last couple of days. If some one could shine some light on this matter, I would greatly appreciate it. I've also attached a copy of my cpp file. Thank you. |
| ||
| Re: Passing a matrix from main function to user defined function. float mean(const double data[][3], int rows, int columns);Yes, you must specify all but the first dimension, though if you want to include the size of the first dimension as well, that's okay too. |
| ||
| Re: Passing a matrix from main function to user defined function. float mean(const double, int, int); this is ur declaration, here the first argument is only double and not a array or pointer. but in the function you are using this as if it is array. sum+=data[i][j]; here u are meaing that data is array, but in ur declation u have given as just double. just chage to float mean( **double, int, int); |
| ||
| Re: Passing a matrix from main function to user defined function. >just chage to >float mean( **double, int, int); Don't answer questions if you don't know the answer yourself. The OP is passing a two dimensional array, which is *not* the same type as a pointer to a pointer. This: double data[][3]is equivalent to this: double (*data)[3]which is completely incompatible with this: double **data |
| ||
| Re: Passing a matrix from main function to user defined function. So, what you are saying is i should change float mean(const double data, int rows, int columns) to float mean(const double (*data)[3], int rows, int columns) I'm still a bit confused. |
| ||
| Re: Passing a matrix from main function to user defined function. What I'm saying is that until you know the difference, you should simply copy your array declaration: double data[5][3];Is copied into the function declaration: double mean ( double data[5][3], int rows, int columns );This will always work, and it saves you the confusion of "clever" people trying to convince you that an array is a pointer. |
| ||
| Re: Passing a matrix from main function to user defined function. Thanks for the help. I am now able to pass the matrix, but I'm still having problems calculating the mean of each row and the mean of each column. here is my mean function: float mean (const double data[2][3], int rows, int columns) { float average, sum=0; for(int i=0; i<rows; i++) { for(int j=0; j<columns; j++) { sum+=data[i][j]; } average = sum /rows; return average; } } |
| All times are GMT -4. The time now is 7:28 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC