Problem Statement:- Program to Find the mean and median of an unsorted array.
Example:-
Input : Given
size=6
arr[]=[4,3,7,5,6,2]
Output:
mean=(4+3+7+5+6+2)/6=4.5
sort Arr[]=[2,3,4,5,6,7]
median=(arr[6/2]+(arr[(6/2)-1]))/2.0
=(arr[3]+arr[2])/2
=(5+4)/2=9/2=4.5
Data requirement:-
Input Data:- size, arr
Output Data:-mean, median
Additional Data:- i,temp
Program in C
Input Data:- size, arr
Output Data:-mean, median
Additional Data:- i,temp
Program in C
Here is the source code of the C Program to Find the mean and median of an unsorted array.
Code:
#include<stdio.h>
void sort(int arr[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
void Find_mean(int arr[],int size)
{
int sum=0,i;
for(i=0;i<size;i++)
{
sum+=arr[i];
}
double mean=(double)sum/(double)size;
printf("Mean = %0.2lf\n",mean);
}
void Find_median(int arr[],int size)
{
sort(arr,size);
if(size%2==1)
{
double median=arr[size/2];
printf("Median= %0.2lf",median);
}
else
{
double median=(arr[size/2]+(arr[(size/2)-1]))/2.0;
printf("Median= %0.2lf\n",median);
}
}
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size];
int i,j;
printf("Enter the Element of the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
Find_mean(arr,size);
Find_median(arr,size);
}
Enter the size of the array:6
Enter the Element of the array:
4 3 7 5 6 2
Mean = 4.50
Median= 4.50
Program in C++
Here is the source code of the C++ Program to Find the mean and median of an unsorted array.
Code:
#include<iostream>
using namespace std;
void sort(int arr[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
void Find_mean(int arr[],int size)
{
int sum=0,i;
for(i=0;i<size;i++)
{
sum+=arr[i];
}
double mean=(double)sum/(double)size;
cout<<"Mean = "<<mean<<"\n";
}
void Find_median(int arr[],int size)
{
sort(arr,size);
if(size%2==1)
{
double median=arr[size/2];
cout<<"Median= "<<median;
}
else
{
double median=(arr[size/2]+(arr[(size/2)-1]))/2.0;
cout<<"Median= "<<median;
}
}
main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size];
int i,temp=0,j;
cout<<"Enter the Element of the array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
Find_mean(arr,size);
Find_median(arr,size);
}
Enter the size of the array:5
Enter the Element of the array:
6 4 7 9 3
Mean = 5.8
Median= 6
Program in Java
Here is the source code of the Java Program to Find the mean and median of an unsorted array.
Code:
import java.util.Scanner;
public class FindMeanMedian {
static void sort(int arr[],int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
static void Find_mean(int arr[],int size)
{
int sum=0,i;
for(i=0;i<size;i++)
{
sum+=arr[i];
}
double mean=(double)sum/(double)size;
System.out.println("Mean = "+mean);
}
static void Find_median(int arr[],int size)
{
sort(arr,size);
if(size%2==1)
{
double median=arr[size/2];
System.out.println("Median= "+median);
}
else
{
double median=(arr[size/2]+(arr[(size/2)-1]))/2.0;
System.out.println("Median= "+median);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,i;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
Find_mean(arr,size);
Find_median(arr,size);
sc.close();
}
}
Enter the size of the array:
4
Enter the Element of the array:
6 7 3 10
Mean = 6.5
Median= 6.5
Program in Python
Here is the source code of the Python Program to Find the mean and median of an unsorted array.
Code:
def Find_mean(arr,size):
sum=0
for i in range(0, size):
sum+=arr[i]
mean=sum/size
print("Mean = ",mean)
def Find_median(arr,size):
arr.sort()
if size%2==1:
median=arr[size//2]
print("\nMedian= ",median)
else:
median = (arr[size // 2] + (arr[(size // 2) - 1])) / 2.0
print("\nMedian= ", median)
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
Find_mean(arr,size)
Find_median(arr,size)
Enter the Element of the array:
8
9
11
5
4
6
14
Mean = 8.142857142857142
Median= 8
0 Comments
Please do not Enter any spam link in the comment box