Hi guys, this 2D arrays is not making sense to me. Some how I am just too confused. What am trying to do is average the rows in 2D array and average the columns in the 2D array seperately. Here is the code snippet that I have trouble implementing. The array is declared as Data[5][4].

double Mean(const int Data[][4], int r[][4], int c[][4])
{
	
	int Avg[5][4];
	int sum=0, i;

	for(int r=0; r<5; r++)
   
   for(int c=0; c<4;c++)
	   Avg[r][c]= r[r][c]+c[r][c];

	return sum;
}

Any help is appreciated.
Thanks.

double Mean(const int Data[][4], int r[][4], int c[][4])
{
	
	int Avg[5][4];
	int sum=0, i;

	for(int r=0; r<5; r++)
   
   for(int c=0; c<4;c++)
	   Avg[r][c]= r[r][c]+c[r][c];

	return sum;
}

Any help is appreciated.
Thanks.

Here is your code simplified:

double Mean(const int Data[][4], int r[][4], int c[][4])
{
     return 0.0;
}

Either that or it's an error because you are returning an int instead of a double. C++ might convert sum to a double for you and it might not. It might give you a "loss of precision" warning. I can't remember. Regardless, your function doesn't really do anything because you declare sum at the top and return it at the bottom and never do anything with it. You don't use i at all.

double Mean(const int Data[][4], int r[][4], int c[][4])
{
	
	int Avg[5][4];
	int sum=0, i;

	for(int r=0; r<5; r++)
   
   for(int c=0; c<4;c++)
	   Avg[r][c]= r[r][c]+c[r][c];

	return sum;
}

You don't use the Data array, you don't use the sum variable, you don't use the i variable. You use Avg, but that's a local variable so all the work that is done is lost. What exactly is this function supposed to accomplish? Is it supposed to fill in the r and c arrays? And are you sure the r and c arrays are supposed to be two-dimensional? What are you supposed to return?

You are also using r and c as integers as counters for your for-loop. Those names are used for your arrays, so pick two letters that aren't used (like i and j) to avoid confusion.

int 2dArray[4][5];
int x = 1;
for(int i=0; i<4; i++)
{
    for(int j=0; i<5; i++)
    {
        2dArray[i][j] = x;
        x++;
    }
}

this sets up an array of 20 integers, 1-20 as follows

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

So to average the values, you'd want to do something like this:

int rowAvg = 0, colAvg = 0;
double finalRowAvg=0;
double finalColAvg=0;
for(int m=0; m<4; m++)
{
    for(int n=0; n<5; n++)
    {
         rowAvg += 2dArray[m][n];
    }
    finalRowAvg = rowAvg / 4;
    cout << "Row Avg for Row: " << m << " is " << finalRowAvg;
    rowAvg = 0; // reset row average for next row
    finalRowAvg = 0.0; // reset final average for next row  
}

Then just do the exact opposite to get the columns:

for(int a=0; a<5; a++)
{
    for(int b=0; b<4; b++)
    {
        colAvg += 2dArray[b][a];
    }
    finalColAvg = colAvg/a;
    cout << "Column Avg for Col: " << a << " is " << finalColAvg ;
    colAvg = 0; // reset
    finalColAvg = 0.0; // reset
}

I know this doesn't solve your problem, but I hope it's helped to clarify what you're trying to accomplish (which I'm still trying to figure out :( )

That makes sense.
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.