Practical No. 1 Program For Calculating Mean, Median and Mode

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

PRACTICAL NO.

Program for calculating Mean, Median and Mode -:


#define SIZE 100

#include<stdio.h>

float mean_function(float[],int);

float median_function(float[],int);

float mode_function(float[],int);

void main()

int i,n, choice;

float array[SIZE],mean,median,mode;

printf("Enter the Elements\n");

scanf("%d",&n);

printf("Enter Elements\n");

for(i=0;i<n;i++)

scanf("%f",&array[i]);

do

printf("\n\tEnter Choice\n\t 1. Mean\n\t2. Median\n\t 3.Mode\n\t 4. Exit");

scanf("%d",&choice);

switch (choice)

case 1:
mean=mean_function(array,n);

printf("\n\t Mean=%f\n",mean);

break;

case 2:

median=median_function(array,n);

printf("\n\t Median=%f\n",median);

break;

case 3:

mode=mode_function(array,n);

printf("\n\t Mode=%f\n",mode);

break;

case 4:

break;

default:

printf("Wrong Option");

break;

while(choice!=4);

float mean_function(float array[],int n)

int i;

float sum=0;

for(i=0;i<n;i++)
sum=sum+array[i];

return(sum/n);

float median_function(float a[],int n)

float temp;

int i,j;

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(a[i]>a[j])

temp=a[j];

a[j]=a[i];

a[i]=temp;

} }

if(n%2==0)

return(a[n/2]+a[n/2-1])/2;

else

return a[n/2];

float mode_function(float a[],int n)

{return(3*median_function(a,n)-2*mean_function(a,n));
}

OUTPUT-:
PRACTICAL NO. 2

Program for calculating Variance, Average, S.D -:


#include<stdio.h>

#include<conio.h>

#include<math.h>

#define MAXSIZE 100

void main()

int n,i;

float avrg,var,SD,sum=0,sum1=0;

int x[MAXSIZE];

clrscr();

printf("\nEnter the no.of element\n");

scanf("%d",&n);

printf("\nEnter the elements\n");

for(i=0;i<n;i++)

scanf("%d",&x[i]) ;

for(i=0;i<n;i++)

sum=sum+x[i] ;

avrg=sum/n;

printf("\nThe average is:%f",avrg);


for(i=0;i<n;i++)

sum1=sum1+pow((x[i]-avrg),2);

var=sum1/n;

printf("\n\n\nVariance is:.2f",var);

SD=sqrt(var);

printf("\n\n\nS.D is :%2f",SD);

getch();

OUTPUT-:

PRACTICAL NO. 3
Program for calculating the Coefficient of Correlation:-
#include <stdio.h>

#include <math.h>

int main() {

int x[100], y[100], xy[100], xsquare[100], ysquare[100];

int i, n, xsum, ysum, xysum, xsqr_sum, ysqr_sum;

float coeff, num, deno;

xsum = ysum = xysum = xsqr_sum = ysqr_sum = 0;

/* get the number of entries from the user */

printf("Enter the value for n:");

scanf("%d", &n);

/* get the values for x and y from the user */

printf("Enter the value for x and y:\n");

for (i = 0; i < n; i++) {

printf("x[%d] and y[%d]: ", i, i);

scanf("%d%d", &x[i], &y[i]);

/* find the needed data to manipulate correlation coeff */

for (i = 0; i < n; i++) {

xy[i] = x[i] * y[i];


xsquare[i] = x[i] * x[i];

ysquare[i] = y[i] * y[i];

xsum = xsum + x[i];

ysum = ysum + y[i];

xysum = xysum + xy[i];

xsqr_sum = xsqr_sum + xsquare[i];

ysqr_sum = ysqr_sum + ysquare[i];

num = 1.0 * ((n * xysum) - (xsum * ysum));

deno = 1.0 * ((n * xsqr_sum - xsum * xsum)* (n * ysqr_sum - ysum * ysum));

/* calculate correlation coefficient */

coeff = num / sqrt(deno);

/* print the result */

printf("Correlation Coefficient : %.4f\n", coeff);

return 0;

OUTPUT-:
PRACTICAL NO.4

Program for calculating the Linear Curve Fitting :-


#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int n,i,x[20],y[20],sumx=0,sumy=0,sumxy=0,sumx2=0;

float a,b;

printf("\n C program for Linear Curve Fitting \n ");

printf("\n Enter the value of number of terms n:");

scanf("%d",&n);

printf("\n Enter the values of x:\n");

for(i=0;i<=n-1;i++)

scanf(" %d",&x[i]);

printf("\n Enter the values of y:");

for(i=0;i<=n-1;i++)

scanf("%d",&y[i]);

for(i=0;i<=n-1;i++)

sumx=sumx +x[i];

sumx2=sumx2 +x[i]*x[i];
sumy=sumy +y[i];

sumxy=sumxy +x[i]*y[i];

a=((sumx2*sumy -sumx*sumxy)*1.0/(n*sumx2-sumx*sumx)*1.0);

b=((n*sumxy-sumx*sumy)*1.0/(n*sumx2-sumx*sumx)*1.0);

printf("\n\nThe line is Y=%3.3f +%3.3f X",a,b);

return(0);

OUTPUT-:

PRACTICAL NO. 5

Program for calculating the Exponential Curve Fitting ;-


#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int n,i;

float Y[20],sumx=0,sumy=0,sumxy=0,sumx2=0,x[20],y[20];

float a,b,A;

printf("\n C program for Exponential Curve fitting\n");

printf("\n Enter the value of number of terms n:");

scanf("%d",&n);

printf("\n Enter the values of x:");

for(i=0;i<=n-1;i++)

scanf("%f",&x[i]);

printf("\n Enter the values of y:");

for(i=0;i<=n-1;i++)

scanf("%f",&y[i]);

for(i=0;i<=n-1;i++)

{
Y[i]=log(y[i]);

for(i=0;i<=n-1;i++)

sumx=sumx +x[i];

sumx2=sumx2 +x[i]*x[i];

sumy=sumy +Y[i];

sumxy=sumxy +x[i]*Y[i];

A=((sumx2*sumy -sumx*sumxy)*1.0/(n*sumx2-sumx*sumx)*1.0);

b=((n*sumxy-sumx*sumy)*1.0/(n*sumx2-sumx*sumx)*1.0);

a=exp(A);

printf("\n\n The curve is Y= %4.3fe^%4.3fX",a,b);

return(0);

OUTPUT-:
PRACTICAL NO. 6

Program for calculating the Linear Regression:-


#include "stdio.h"
#include "conio.h"

#include "math.h"

#include "string.h"

float mean(float *a, int n);

void deviation(float *a, float mean, int n, float *d, float *S);

void main() {

float a[20], b[20], dx[20], dy[20];

float sy = 0, sx = 0, mean_x = 0, mean_y = 0, sum_xy = 0;

float corr_coff = 0, reg_coff_xy = 0, reg_coff_yx = 0;

char type_coff[7];

int n = 0, i = 0;

clrscr();

printf("Enter the value of n: ");

scanf("%d", &n);

printf("Enter the values of x and y:\n");

for (i = 0; i < n; i++)

scanf("%f%f", &a[i], &b[i]);

mean_x = mean(a, n);

mean_y = mean(b, n);

deviation(a, mean_x, n, dx, &sx);

deviation(b, mean_y, n, dy, &sy);

for (i = 0; i < n; i++)

sum_xy = sum_xy + dx[i] * dy[i];

corr_coff = sum_xy / (n * sx * sy);


printf("Enter the type of regression coefficient as 'x on y' or 'y on x': ");

fflush(stdin);

gets(type_coff);

if (strcmp(type_coff, "x on y") == 1) {

reg_coff_xy = corr_coff * (sx / sy);

printf("\nThe value of linear regression coefficient is %f",

reg_coff_xy);

} else if (strcmp(type_coff, "y on x") == 1) {

reg_coff_yx = corr_coff * (sy / sx);

printf("\nThe value of linear regression coefficient is %f",

reg_coff_yx);

} else

printf("\nEnter the correct type of regression coefficient.");

getch();

float mean(float *a, int n) {

float sum = 0, i = 0;

for (i = 0; i < n; i++)

sum = sum + a[i];

sum = sum / n;

return (sum);

void deviation(float *a, float mean, int n, float *d, float *s) {

float sum = 0, t = 0;
int i = 0;

for (i = 0; i < n; i++) {

d[i] = a[i] - mean;

t = d[i] * d[i];

sum = sum + t;

sum = sum / n;

*s = sqrt(sum);

OUTPUT-:

PRACTICAL NO. 7

Program for calculating Mean, Median and Mode -:


#define SIZE 100

#include<stdio.h>

float mean_function(float[],int);

float median_function(float[],int);

float mode_function(float[],int);

void main()

int i,n, choice;

float array[SIZE],mean,median,mode;

printf("Enter the Elements\n");

scanf("%d",&n);

printf("Enter Elements\n");

for(i=0;i<n;i++)

scanf("%f",&array[i]);

do

printf("\n\tEnter Choice\n\t 1. Mean\n\t2. Median\n\t 3.Mode\n\t 4. Exit");

scanf("%d",&choice);

switch (choice)

case 1:

mean=mean_function(array,n);

printf("\n\t Mean=%f\n",mean);

break;

case 2:
median=median_function(array,n);

printf("\n\t Median=%f\n",median);

break;

case 3:

mode=mode_function(array,n);

printf("\n\t Mode=%f\n",mode);

break;

case 4:

break;

default:

printf("Wrong Option");

break;

while(choice!=4);

float mean_function(float array[],int n)

int i;

float sum=0;

for(i=0;i<n;i++)

sum=sum+array[i];

return(sum/n);

float median_function(float a[],int n)


{

float temp;

int i,j;

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(a[i]>a[j])

temp=a[j];

a[j]=a[i];

a[i]=temp;

} }

if(n%2==0)

return(a[n/2]+a[n/2-1])/2;

else

return a[n/2];

float mode_function(float a[],int n)

{return(3*median_function(a,n)-2*mean_function(a,n));

}
OUTPUT-:
PRACTICAL NO. 8

Program for calculating Variance, Average, S.D -:


#include<stdio.h>

#include<conio.h>

#include<math.h>

#define MAXSIZE 100

void main()

int n,i;

float avrg,var,SD,sum=0,sum1=0;

int x[MAXSIZE];

clrscr();

printf("\nEnter the no.of element\n");

scanf("%d",&n);

printf("\nEnter the elements\n");

for(i=0;i<n;i++)

scanf("%d",&x[i]) ;

for(i=0;i<n;i++)

sum=sum+x[i] ;

avrg=sum/n;

printf("\nThe average is:%f",avrg);


for(i=0;i<n;i++)

sum1=sum1+pow((x[i]-avrg),2);

var=sum1/n;

printf("\n\n\nVariance is:.2f",var);

SD=sqrt(var);

printf("\n\n\nS.D is :%2f",SD);

getch();

OUTPUT-:

You might also like