Hi coders,,,
I have to find mean,mode and median of a 2-D array :
2 5 2 5 9 5 8 3 6 10
8 1 10 10 7 1 4 4 3 8
4 3 1 2 4 10 3 9 8 5
6 10 8 3 6 10 5 1 2 5
2 2 9 2 5 5 1 1 7 5
7 9 9 2 1 5 2 2 2 4
3 6 1 9 3 4 10 7 4 6
7 10 4 6 2 10 10 8 7 6
7 1 3 6 2 4 6 7 8 9
8 5 9 2 3 2 1 5 1 8

I coded for mean but stuck for mode.
Code:

``````#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;

int i,j;

void calc_mean(int arr[10][10],int c)
{

double sum=0,out=0;
for(i=0;i<=9;i++)
{
for(j=0;j<=9;j++)
{

sum=sum+arr[i][j];
}

}

out=sum/c;
cout<< "Mean is:" <<out<<endl;

}

void calc_mode(int arr[10][10],int freq[][]{

// stucked here
}

int main(int argc, _TCHAR* argv[])
{
int count=0,mean;
int arr[10][10]={2,5,2,5,9,5,8,3,6,10,8,1,10,10,7,1,4,4,3,8,4,3,1,2,4,10,3,9,8,5,6,10,8,3,6,10,5,1,2,5,2,2,9,2,5,5,1,1,7,5,7,9,9,2,1,5,2,2,2,4,3,6,1,9,3,4,10,7,4,6,7,10,4,6,2,10,10,8,7,6,7,1,3,6,2,4,6,7,8,9,8,5,9,2,3,2,1,5,1,8};
int freq[10]={};
for(i=0;i<=9;i++)
{
for(j=0;j<=9;j++)
{

cout<<arr[i][j]<<",";

count++;
}

}
cout<<endl;
cout<<"Array size is"<<count<<endl;
calc_mean(arr,count);
calc_mode(arr,freq);

_getch();
return 0;
}
``````

I used the following for mode part(by following the technique of 1-D array) which didn't work,,

``````for(int i=0;j<=9;++i)
{
for(j=0;j<=9;j++)
{

++freq[arr[i][j]];

}
}

cout<<"Array value"<<setw(17)<<"Mode"<<endl;
for(int k=0;k<=9;k++)
{

cout<<setw(6)<<k<<setw(17)<<freq[k]<<endl;

}
``````

Any Guidance will be Appreciated

there are probably several ways to calculate the mode, but I think the simplest was is to create another 2d array, but this time the 1st dim contains one of the numbers from the original 2d array and the second dim contains a count of the number of times it …

yes,

## All 7 Replies

there are probably several ways to calculate the mode, but I think the simplest was is to create another 2d array, but this time the 1st dim contains one of the numbers from the original 2d array and the second dim contains a count of the number of times it appears in the original array.

So, to take just the first line of the data you posted
2 5 2 5 9 5 8 3 6 10

The 2d array would look like this

2 2
3 1
5 3
6 1
8 1
9 1
10 1

``````for each cell in the original array
search new 2d array to see if it's already there
if not, then add it and set it's count to 1
if it is in the array, then increment it's count
end of loop
``````

When done, just pick out the number with the greatest count, in the above case it would be 5.

You could probably simplify all that by using std::map instead of another array, but you may not have that choice.

should the 2D array encompass all the original array?

yes,

its very easy to workout it for a single line of data ,,,,,but how to manage it for 100 pieces of data (2-D array)

Why the heck are you using a 2D array? This could be solved for a 1D array. Either one isn't complicated in the slightest.

Because its the requirement of the task to go for 2D

Have you ever heard of nested loops? You have two loops, one inside the other. The other loops counts the rows in the 2d array, the innter loop counts the columns in each row.

``````int array[5][10]; // original array, 5 rows and 10 columns
int counters[5][2] = {0}; // 5 rows 2 columns
int rows = 5;
int cols = 10;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
if array[i][j] is already in counters array
...
}
}
``````
Be a part of the DaniWeb community

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