| | |
Passing a matrix from main function to user defined function.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
float mean(const double data[][3], int rows, int columns);
I'm here to prove you wrong.
•
•
Join Date: Mar 2006
Posts: 3
Reputation:
Solved Threads: 0
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);
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);
>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:
is equivalent to this:
which is completely incompatible with this:
>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:
C++ Syntax (Toggle Plain Text)
double data[][3]
C++ Syntax (Toggle Plain Text)
double (*data)[3]
C++ Syntax (Toggle Plain Text)
double **data
I'm here to prove you wrong.
What I'm saying is that until you know the difference, you should simply copy your array declaration:
Is copied into the function declaration:
This will always work, and it saves you the confusion of "clever" people trying to convince you that an array is a pointer.
C++ Syntax (Toggle Plain Text)
double data[5][3];
C++ Syntax (Toggle Plain Text)
double mean ( double data[5][3], int rows, int columns );
I'm here to prove you wrong.
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
![]() |
Similar Threads
- User-defined functions (C++)
- User-Defined Function - part 2 (C++)
- User-Defined Function - setup right? (C++)
- error in user defined string class (C++)
- using(STL)function object (bind2nd) with a user defined function object (C++)
Other Threads in the C++ Forum
- Previous Thread: Combined data type arrays.
- Next Thread: Help with Map
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






