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