Passing a matrix from main function to user defined function.

Reply

Join Date: Mar 2006
Posts: 7
Reputation: kjones2k1 is an unknown quantity at this point 
Solved Threads: 0
kjones2k1 kjones2k1 is offline Offline
Newbie Poster

Passing a matrix from main function to user defined function.

 
0
  #1
Mar 22nd, 2006
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, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Mar 22nd, 2006
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: craju45 is an unknown quantity at this point 
Solved Threads: 0
craju45 craju45 is offline Offline
Newbie Poster

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

 
0
  #3
Mar 23rd, 2006
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);
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Mar 23rd, 2006
>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:
  1. double data[][3]
is equivalent to this:
  1. double (*data)[3]
which is completely incompatible with this:
  1. double **data
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: kjones2k1 is an unknown quantity at this point 
Solved Threads: 0
kjones2k1 kjones2k1 is offline Offline
Newbie Poster

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

 
0
  #5
Mar 23rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #6
Mar 23rd, 2006
What I'm saying is that until you know the difference, you should simply copy your array declaration:
  1. double data[5][3];
Is copied into the function declaration:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: kjones2k1 is an unknown quantity at this point 
Solved Threads: 0
kjones2k1 kjones2k1 is offline Offline
Newbie Poster

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

 
0
  #7
Mar 25th, 2006
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;
}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC