944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14289
  • C++ RSS
Mar 22nd, 2006
0

Passing a matrix from main function to user defined function.

Expand Post »
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.
Attached Files
File Type: cpp matrix passing.cpp (783 Bytes, 42 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjones2k1 is offline Offline
7 posts
since Mar 2006
Mar 22nd, 2006
0

Re: Passing a matrix from main function to user defined function.

C++ Syntax (Toggle Plain Text)
  1. 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 23rd, 2006
0

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);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
craju45 is offline Offline
3 posts
since Mar 2006
Mar 23rd, 2006
0

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:
C++ Syntax (Toggle Plain Text)
  1. double data[][3]
is equivalent to this:
C++ Syntax (Toggle Plain Text)
  1. double (*data)[3]
which is completely incompatible with this:
C++ Syntax (Toggle Plain Text)
  1. double **data
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 23rd, 2006
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjones2k1 is offline Offline
7 posts
since Mar 2006
Mar 23rd, 2006
0

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:
C++ Syntax (Toggle Plain Text)
  1. double data[5][3];
Is copied into the function declaration:
C++ Syntax (Toggle Plain Text)
  1. 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 25th, 2006
0

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;
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjones2k1 is offline Offline
7 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Combined data type arrays.
Next Thread in C++ Forum Timeline: Help with Map





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC