0% found this document useful (0 votes)
3 views47 pages

Practical C Programming

The document contains multiple C programming practicals focused on statistical computations, including calculating mean, median, mode, factorial, and matrix operations. It also covers generating random samples from various distributions such as uniform, exponential, gamma, and normal distributions. Each practical is presented with code snippets and prompts for user input to perform the respective calculations.

Uploaded by

imshivam502
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views47 pages

Practical C Programming

The document contains multiple C programming practicals focused on statistical computations, including calculating mean, median, mode, factorial, and matrix operations. It also covers generating random samples from various distributions such as uniform, exponential, gamma, and normal distributions. Each practical is presented with code snippets and prompts for user input to perform the respective calculations.

Uploaded by

imshivam502
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRACTICAL -1

#include<stdio.h>

int main()

float

ll[15],ul[15],md[15],sum1=0.0,sum2=0.0,mean;

int freq[15],i,n;

printf("No. of observations=");

scanf("%d",&n);

printf("\nlowerlimit\tupperlimit\tfrequency");

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

{scanf("%f%f%d",&ll[i],&ul[i],&freq[i]);

sum1=sum1=freq[i];

md[i]=(ll[i]=ul[i])/2;

sum2=sum2+(md[i]*freq[i]);

mean=sum2/sum1;

printf("The mean of grouped data=%f",mean);

}
PRACTICAL- 2

#include <stdio.h>

int main()

int ll[10],ul[10],freq[10],i,n;

float median ,sum1=0.0,h,c,f,cf[10],l;

printf("No. of observations=");

scanf("%d",&n);

printf("\nlowerlimit\tupperlimit\tfrequency\n");

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

scanf("%d%d%d",&ll[i],&ul[i],&freq[i]);

sum1=sum1=freq[i];

cf[0]=freq[0];

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

cf[i]=cf[i-1]+freq[i];

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

if(sum1/2<cf[i])

l=ll[i];

f=freq[i];

c=cf[i-1];

h=ul[i]-ll[i];
break;

median=l=((sum1/2-c)*(h/f));

printf("The median of the continuous data=%f",median);

}
PRACTICAL – 3

#include<stdio.h>

int main()

int freq[10],n,i;

float ll[10],ul[10],h,max,mode;

printf("No. of observations=");

scanf("%d",&n);

printf("\nlowerlimit\tupperlimit\tfrequency");

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

scanf("%f%f%d",&ll[i],&ul[i],&freq[i]);

h=ul[0]-ll[0];

printf("class size: %f\n",h);

freq[0]=max;

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

if(freq[i]>max)

max=freq[i];

mode=ll[i]+(((h)*(freq[i]-freq[i-1]))/(2*freq[i]-freq[i-1]-freq[i+1]));

printf("The mode of the continuous data=%f",mode);

}
PRACTICAL –5

#include<stdio.h>

int main()

int i,n,j,m;

long int fact;

printf("enter the number n=");

scanf("%d",&n);

printf("Enter the value for m=");

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

scanf("%d",&m);

fact=1;

for(j=1;j<=m;j++)

fact=fact*j;

printf("The factorial of the given number=%lu\n",fact);

}
PRACTICAL –6

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main ()

int i,j,n,k,f[100];

float o[100],max,min,freq,ll[20],ul[20],w,range;

printf("enter number of observations=");

scanf("%d",&n);

printf("\nEnter the observations=");

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

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

max=o[0];

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

if(max<=o[i])

max=o[i];

min=o[0];

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

if(min>=o[i])
min=o[i];

range=max-min;

printf("maxvalue=%f\t&\tminvalue=%f\t&\trange=%f",max,min,range);

k=ceil(1+3.322*log10(n));

w=range/k;

ll[0]=min;

ul[0]=ll[0]+w;

for(i=1;i<k;i++)

ll[i]=ul[i-1];

ul[i]=ul[i-1]+w;

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

f[i]=0;

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

if(o[j]>=ll[i]&&o[j]<ul[i])

f[i]=f[i]+1;

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

if(o[i]==ul[k-1])

f[k-1]=f[k-1]+1;

printf("\nlowerlimit\tupperlimit\tfrequency\n");

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

printf("%12.2f%12.2f\t%d\n",ll[i],ul[i],f[i]);

getch();

}
PRACTICAL –7

#include<stdio.h>

#include<math.h>

int main()

int x2,x;

float a,b,c,root1,root2,D,i,rp,ip;

printf("enter the value of a,b,c:");

scanf("%f%f%f",&a,&b,&c);

printf("The quadartic equation is %1.0fx2+%[Link]\n",a,b,c);

D=b*b-(4*a*c);

if(D==0)

root1 =-b/2*a;

root2=root1;

printf("Roots are real and equal");

printf("\nRoots of the equations are %f and %f",root1,root2);

else if (D>0)

root1=(-b-sqrt(D))/(2*a);

root2=(-b+sqrt(D))/(2*a);

printf("Roots are real and distinct");

printf("\nRoots of the equations are %f and %f",root1,root2);

}
else

rp=-b/(2*a);

ip=sqrt(-D)/(2*a);

printf("Roots are imaginary");

printf("\nRoots of the equation are=%f+%fi and %f-%fi",rp,ip,rp,ip);

}
PRACTICAL – 8

#include<stdio.h>

#include<conio.h>

int main()

int i,j,n,m, trace1=0,trace2=0;

float A[15] [15], B[15] [15], C[15] [15], D[15] [15], A_transpose [15] [15], B_transpose [15] [15];

printf("Enter number of rows=",&n);

scanf("%d",&n);

printf("Enter number of columns=", &m);

scanf("%d",&m);

printf("Enter matrix A\n");

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

for(j=0;j<m;j++)

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

printf("Enter the matrixB\n");

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

for(j=0;j<m;j++)

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

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

for(j=0;j<m;j++)

C[i][j]=A[i][j]+B[i][j];

printf("Addition of A and matrix B is\n");

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

{
for(j=0;j<m;j++)

printf("%f\t", C[i][j]);

printf("\n");

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

for(j=0;j<m;j++)

D[i][j]=A[i][j]-B[i][j];

printf("Subtraction of matrix A and matrix B is\n");

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

for(j=0;j<m;j++)

printf("%f\t", D[i][j]);

printf("\n");

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

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

A_transpose [i][j]=A[j][i];

printf("Transpose of A=\n");

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

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

printf("%f\t", A_transpose [i][j]);

printf("\n");

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

B_transpose [i][j]=B[j][i];

printf("Transpose of B=\n");

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

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

printf("%f\t", B_transpose [i][j]);

printf("\n");

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

trace1=trace1+A[i] [i];

printf("Trace of matrix A=%d\n", trace1);

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

trace2=trace2+B[i] [i];

printf("Trace of matrix B=%d", trace2);

}
PRACTICAL – 9

#include<stdio.h>

#include<conio.h>

int main()

float A[20][20], B[20][20],C[20][20];

int m, n, p,q, i,j,k;

printf("The order of A=");

scanf("%d%d", &n,&m);

printf("The order of B=");

scanf("%d%d", &q, &p);

printf("Enter matrix A\n");

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

for(j=0;j<m; j++)

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

printf("Enter matrix B\n");

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

for(j=0;j<p;j++)

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

if (m!=q)

printf("Multiplication of matrices is not possible");

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

for(j=0;j<p;j++){
C[i][j]=0;

for(k=0;k<m;k++){

C[i][j]=C[i][j]+(A[i][k]*B[k][j]);

printf("The multiplication of A and B is \n");

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

for(j=0;j<p;j++)

printf("%f\t",C[i][j]);

printf("\n");

}
PRACTICAL – 10

#include<stdio.h>

#include<math.h>

#define pi 3.414

int main(){

int n,j,fx;

float i, a, b, c, N, sum, mean, var, sd, x [10],ul [10],ll[10],f[10];

FILE *p, *q;

p=fopen("[Link]","r");

q=fopen("[Link]","w");

mean=0;N=0;var=0;

fscanf (p, "%d",&n);

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

fscanf(p,"%f%f%f",&ll[i], &ul[i],&f[i]);

x[i]=(ul[i]+ll[i])/2;

mean=mean+(f[i]*x[i]);

N=N+f[i];

var=var+(f[i]*x[i]*x[i]);

mean=mean/N;

var=(var/N)-pow(mean, 2); sd=sqrt(var);

a=N/(sqrt(2*pi)*sd);

b=-1/(2*var);

fprintf(q,"\n The mean is=%f\n The variance is = %f\n",mean,var);

fprintf (q,"_\n");
for(i=60;i<100;i+=0.5){

c=i-mean;

fx=(int) (a*exp (b*pow(c, 2))+0.5);

fprintf(q,"!");

for(j=1;j<fx;j++)

fprintf(q,"");

fprintf(q,".\n");

fclose(p);

fclose(q);

}
PRACTICAL – 11

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

int i, n;

float x[100], u, a, b;

// Initialize random number generator

srand(time(0));

printf("Input the size of sample: ");

scanf("%d", &n);

printf("The parameter of uniform distribution are (a b): ");

scanf("%f %f", &a, &b);

printf("The random sample drawn from uniform distribution is:\n");

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

u = rand() / (1.0 + RAND_MAX);

x[i] = ((b - a) * u) + a;

printf("%f\t", x[i]);

return 0;

}
PRACTICAL -12

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

int i, n;

float x[100],u, lambda;

// Initialize random number generator

srand(time(0));

printf("Input the size of sample: ");

scanf("%d", &n);

printf("The parameter of exponential distribution are: ");

scanf("%f", &lambda);

printf("The random sample drawn from exponentail distribution is:\n");

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

u = rand() / (1.0 + RAND_MAX);

x[i] = (-log(u))/lambda;

printf("%f\t", x[i]);

return 0;

}
PRACTICAL – 13

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

int i,n,k,j;

float x[100],u, lambda,product;

// Initialize random number generator

srand(time(0));

printf("Input the size of sample: ");

scanf("%d", &k);

printf("The parameter of gamma distribution are : ");

scanf("%d %f", &n, &lambda);

printf("The random sample drawn from gamma distribution is:\n");

printf("\n");

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

product=1.0;

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

u = rand() / (1.0 + RAND_MAX);

product = product * u;

x[i]=-log(product)/lambda;

printf("%f\t",x[i]);

}
PRACTICAL – 14

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

int main() {

int i, n, k, j;

float x[100], z[100], u, mean, variance, sum;

// Initialize random number generator

srand(time(0));

printf("Input the size of sample: ");

scanf("%d", &n);

printf("Input the value of k: ");

scanf("%d", &k);

printf("Input the value of mean and variance: ");

scanf("%f %f", &mean, &variance);

printf("The random sample drawn from normal distribution is:\n");

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

sum = 0.0;
for (j = 0; j < k; j++) {

u = rand() / (float) RAND_MAX;

sum += u;

z[i] = (sum - (k / 2.0)) / sqrt(k / 12.0);

x[i] = mean + (sqrt(variance) * z[i]);

printf("%f\t", x[i]);

return 0;

}
PRACTICAL – 15

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <math.h>

int main() {

int i,n,k,j,mu,nu;

float x,y,z[100],u, lambda,product1,product2;

// Initialize random number generator

srand(time(0));

printf("Input the size of sample: ");

scanf("%d", &k);

printf("The parameter of gamma1 distribution are : ");

scanf("%d %f",&mu,&lambda);

printf("The parameter of gamma2 distribution are : ");

scanf("%d %f",&nu,&lambda);

printf("The random sample drawn from beta1 distribution is:\n");

printf("\n");

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

product1=1.0;

for(j=0;j<mu;j++){

u = rand() / (1.0 + RAND_MAX);

product1=product1*u;

}
product2=1.0;

for(j=0;j<nu;j++){

u = rand() / (1.0 + RAND_MAX);

product2=product2*u;

x = (-log(product1))/lambda;

y = (-log(product2))/lambda;

z[i]=x/(x+y);

printf("%f\t",z[i]);

}
PRACTICAL – 16

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <math.h>

int main() {

int i,n,k,j,mu,nu;

float x,y,z[100],u, lambda,product1,product2;

// Initialize random number generator

srand(time(0));

printf("Input the size of sample: ");

scanf("%d", &k);

printf("The parameter of gamma1 distribution are : ");

scanf("%d %f",&mu,&lambda);

printf("The parameter of gamma2 distribution are : ");

scanf("%d %f",&nu,&lambda);

printf("The random sample drawn from beta2 distribution is:\n");

printf("\n");

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

product1=1.0;

for(j=0;j<mu;j++){

u = rand() / (1.0 + RAND_MAX);

product1=product1*u;

}
product2=1.0;

for(j=0;j<nu;j++){

u = rand() / (1.0 + RAND_MAX);

product2=product2*u;

x = (-log(product1))/lambda;

y = (-log(product2))/lambda;

z[i]=x/y;

printf("%f\t",z[i]);

}
PRACTICAL – 21

#include <stdio.h>

#include <math.h>

float mean(float a[], int n);

float cov(float a[], float b[], int n);

float cc(float a[], float b[], int n);

float mcc(float a[], float b[], float c[], int n);

float pcc(float a[], float b[], float c[], int n);

void main() {

int i, n;

float x[50] = {15, 77, 18, 8, 16, 54, 95, 22, 69, 75, 8, 4, 97, 53, 26, 49, 3, 21, 56, 72, 97, 18, 53, 60, 9},

y[50] = {38, 79, 53, 33, 12, 34, 61, 31, 82, 75, 41, 27, 66, 31, 31, 33, 46, 55, 55, 57, 58, 23, 29, 60,
12},

z[50] = {29, 29, 16, 18, 18, 20, 34, 18, 31, 39, 38, 19, 35, 31, 14, 11, 21, 27, 20, 15, 28, 11, 10, 20,
10},

rxy_z, rxz_y, ryz_x,

Rx_yz, Ry_xz, Rz_xy,

rxy, ryz, rxz;

printf("Enter the no. of observations: ");

scanf("%d", &n);

rxy = cc(x, y, n);

ryz = cc(y, z, n);


rxz = cc(x, z, n);

printf("\nSimple correlation coefficients are:\n");

printf("rxy=%f\nryz=%f\nrxz=%f\n", rxy, ryz, rxz);

Rx_yz = mcc(x, y, z, n);

Ry_xz = mcc(y, x, z, n);

Rz_xy = mcc(z, x, y, n);

printf("\nMultiple correlation coefficients are:\n");

printf("Rx_yz=%f\nRy_xz=%f\nRz_xy=%f\n", Rx_yz, Ry_xz, Rz_xy);

rxy_z = pcc(x, y, z, n);

rxz_y = pcc(x, z, y, n);

ryz_x = pcc(y, z, x, n);

printf("\nPartial correlation coefficients are:\n");

printf("rxy_z=%f\nrxz_y=%f\nryz_x=%f\n", rxy_z, rxz_y, ryz_x);

return 0;

float mean(float a[], int n) {

float sum = 0;

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

sum += a[i];

return sum / n;

float cov(float a[], float b[], int n) {

float sum = 0, mean_a, mean_b;

int i;

mean_a = mean(a, n);

mean_b = mean(b, n);

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

sum += (a[i] - mean_a) * (b[i] - mean_b);

return sum / n;

float cc(float a[], float b[], int n) {

return cov(a, b, n) / (sqrt(cov(a, a, n)) * sqrt(cov(b, b, n)));

float mcc(float a[], float b[], float c[], int n) {

float r_ab = cc(a, b, n);

float r_ac = cc(a, c, n);

float r_bc = cc(b, c, n);


return sqrt((1 - r_ab * r_ab - r_ac * r_ac + r_bc * r_bc) / (1 - r_bc * r_bc));

float pcc(float a[], float b[], float c[], int n) {

float r_ab = cc(a, b, n);

float r_ac = cc(a, c, n);

float r_bc = cc(b, c, n);

return (r_ab - r_ac * r_bc) / sqrt((1 - r_ac * r_ac) * (1 - r_bc * r_bc));

}
PRACTICAL – 22

#include<stdio.h>

#include<math.h>

int rank(int a,int *b,int n);

main()

int i,n,x[20],y[20],rx[20],ry[20],di[20];

void print(int z[],int n);

float rankcorr,ss=0.0;

printf("Enter number of observations :");

scanf("%d",&n);

printf("Enter observations of X:");

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

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

printf("Enter observations of y:");

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

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

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

rx[i]=rank(x[i],x,n);
ry[i]=rank(y[i],y,n);

di[i]=(rx[i]-ry[i]);

ss+=pow(di[i],2);

printf("\nThe Sum of Squares of deviation is: %f\n\n",ss);

rankcorr=1-((6*ss)/(n*(n*n-1)));

printf("%\nThe Spearman's Rank Correlation is %f\n",rankcorr);

printf("\nx\t:\t");

print(x,n);

printf("\n\nRank of [x]\t:\t");

print(rx,n);

printf("\n\ny\t:\t");

print(y,n);

printf("\n\nRank of [y]\t:\t");

print(ry,n);

return 0;

int rank(int a,int *b,int n)

int i,r=1;

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

if(a<*(b+i))

r+=1;
}

return(r);

void print(int z[],int n)

int i;

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

printf("%d\t",z[i]);

}
PRACTICAL – 23

#include <stdio.h>

#include <conio.h>

#include <math.h>

float mean(float a[], int n);

float cov(float a[], float b[], int n);

float cc(float a[], float b[], int n);

void regression(float a[], float b[], int n);

void main(){

int i,n;

float x[50],y[50],rxy;

printf("Enter the number of observations:");

scanf("%d",&n);

printf("\nEnter the observations of variable x:\n");

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

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

printf("\nEnter the observations of variable y:\n");

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

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

rxy=cc(x,y,n);

printf("\nThe simple correlation coefficent is %f",rxy);


printf("\nThe line of regression of y on x is");

regression(x,y,n);

float mean(float x[],int n){

int i;

float sum1=0.0,m;

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

sum1=sum1+x[i];

m=sum1/n;

return(m);

float cov(float a[], float b[], int n){

int i;

float sum1=0.0,c;

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

sum1=sum1+(a[i]*b[i]);

c=sum1/n-(mean(a,n)*mean(b,n));

return c;

float cc(float a[], float b[], int n){

float r;

r=cov(a,b,n)/(sqrt(cov(a,a,n)*cov(b,b,n)));

return(r);
}

void regression(float x[], float y[], int n){

float m1,m2,var1,var2,rxy,s1,s2;

m1=mean(x,n);

m2=mean(y,n);

var1=cov(x,x,n);

s1=sqrt(var1);

var2=cov(y,y,n);

s2=sqrt(var2);

rxy=cc(x,y,n);

printf("\ny-%f=%f*%f/%f(x-%f)",m2,rxy,s2,s1,m1);

}
PRACTICAL – 24

#include <stdio.h>

#include <conio.h>

#include <math.h>

float mean(float a[],int n);

float s1(float a[],int n);

void main(){

float i,n1,n2,n;

float x[20]={74.1,77.4,74.0,74.4,73.8,79.3,75.8,82.8,72.5,75.2,78.2,77.1,78.4,76.3,76.8};

float y[20]={70.8,74.9,74.2,70.4,69.2,72.2,76.8,72.4,77.4,78.1,72.8,74.3,74.7};

float x1,y1,t,s;

printf("Enter the number of observations in x\n");

scanf("%f",&n1);

printf("Enter the number of observations in y\n");

scanf("%f",&n2);

x1=mean(x,n1);

printf("The mean of x is %f",x1);

y1=mean(y,n2);

printf("\nThe mean of y is %f",y1);

s=(1/(n1+n2-2))*(s1(x,n1)+s1(y,n2));

printf("\nThe value of s^2 is %f",s);

t=(x1-y1)/sqrt(s*((1/n1)+(1/n2)));

printf("\nt-test statistics is %f",t);

printf("\nThe tabulated value is 2.06");

if(t<2.06)
printf("\nAccept H0");

else

printf("\nReject H0");

printf("\nAternative hypothesis is accepted since calculated value is greater than tabulated


value");

float mean(float x[],int n){

int i;

float sum=0.0,m;

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

sum=sum+x[i];

m=sum/n;

return(m);

float s1(float a[],int n){

int i;

float sum1=0.0,c;

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

sum1=sum1+(a[i]-mean(a,n))*(a[i]-mean(a,n));

c=sum1;

return(c);

}
PRACTICAL – 25

#include <ctype.h>

#include <stdio.h>

#include <conio.h>

#include <math.h>

float mean(float a[],float b[], float n);

float s1(float a[],float b[],float n);

int main(){

float i,n;

float x1,y1,t,s,d1;

float x[20]={84,48,36,37,54,69,83,96,90,65};

float y[20]={90,58,56,49,62,81,84,86,84,75};

printf("\nEnter the no. of observations in x -");

scanf("%f",&n);

printf("\nEnter the no. of observations in y -");

scanf("%f",&n);

d1=mean(x,y,n);

printf("\nMean is :%f",d1);

s=(1/(n-1))*(s1(x,y,n));

printf("\nValue of s^2 is : %f",s);

t=fabs(d1)/(sqrt(s*(1/n)));

printf("\nTest statistics is : %f",t);

if(t<2.26)

printf("\nAccept H0");

else
printf("\nReject H0");

float mean(float a[],float b[],float n){

int i;

float sum=0.0,m;

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

sum=sum+(a[i]-b[i]);

m=sum/n;

return (m);

float s1(float a[],float b[],float n){

int i;

float sum1=0.0,c;

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

sum1=sum1+(((a[i]-b[i])-mean(a,b,n))*((a[i]-b[i])-mean(a,b,n)));

c=sum1;

return (c);

}
PRACTICAL – 26

#include <stdio.h>

#include <conio.h>

#include <math.h>

float mean(float a[],float n);

float s1(float a[],float n);

void main(){

float i,n,n1,n2;

float x[20]={20,16,26,27,23,22,18,24,25,19};

float y[20]={27,33,42,35,32,34,38,28,41,43,30,37};

float x1,y1,F,sx,sy;

printf("\nEnter the no. of observations in x-");

scanf("%f",&n1);

printf("\nEnter the no. of observations in y-");

scanf("%f",&n2);

x1=mean(x,n1);

printf("\nMean of x is :&f",x1);

y1=mean(y,n2);

printf("\nMean of y is :&f",y1);

sx=(1/(n1-1))*(s1(x,n1));

printf("\nValue of sx^2 is : %f",sx);

sy=(1/(n2-1))*(s1(y,n2));

printf("\nValue of sy^2 is : %f",sy);

F=(sy/sx);

printf("\nTest statistics is : %f",F);


if(F>2.91)

printf("\nAccept H0");

else

printf("\nReject H0");

float mean(float x[],float n){

int i;

float sum=0.0, m;

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

sum=sum+x[i];

m=sum/n;

return(m);

float s1(float a[],float n){

int i;

float sum1=0.0, c;

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

sum1=sum1+(a[i]-mean(a,n))*(a[i]-mean(a,n));

c=sum1;

return(c);

}
PRACTICAL – 27

#include <stdio.h>

#include <conio.h>

#include <math.h>

float chi(int a[],float b,int n);

void main(){

int i,n,observed[10];

float statistic,tv,sum1=0.0,expected;

printf("\nNull Hypothesis : The model is good fit for the given data");

printf("\nAlternative Hypothesis : The model is not good fit for the given data");

printf("\nEnter the number of observations : ");

scanf("%d",&n);

printf("Enter the observations : ");

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

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

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

sum1=sum1+observed[i];

expected = sum1/n;

printf("The expected frequency are %f",expected);

statistic = chi(observed,expected,n);

printf("\nUnder H0 \nThe value of test statistic is :%f",statistic);

printf("\nThe value of chi-square(tabulated) is ");

scanf("%f",&tv);
if(statistic >tv)

printf("\nWe reject the null hypothesis. Therefore, the model doesn't fit good");

else

printf("\nWe accept the null hypothesis. Therefore, the model is fit good ");

float chi(int a[],float b, int n){

int i;

float chi_sq, sum1=0.0;

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

sum1+=((a[i]-b)*(a[i]-b))/b;

chi_sq=sum1;

return(chi_sq);

}
PRACTICAL – 28

#include <stdio.h>

#include <math.h>

float chi(float a[10][10], int n);

void main() {

int i, n, j;

float observed[10][10], statistic, tv;

printf("\nNull hypothesis: The attributes are independent\n");

printf("\nAlternative hypothesis: The attributes are not independent.\n");

printf("Enter the order of contingency table: ");

scanf("%d", &n);

printf("Enter the observations: \n");

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

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

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

statistic = chi(observed, n);

printf("\nUnder H0\nThe value of the test statistic is: %f", statistic);

printf("\nThe tabulated chi-square value is: ");


scanf("%f", &tv);

if (statistic < tv) {

printf("\nWe accept the null hypothesis. Therefore, the attributes are independent.");

} else {

printf("\nWe reject the null hypothesis. Therefore, the attributes are not independent.");

float chi(float a[10][10], int n) {

int i, j;

float chi_sq, sum = 0.0, sum1 = 0.0, sum2 = 0.0, sum3 = 0.0, sum4 = 0.0;

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

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

sum += a[i][j];

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

sum1 += a[0][j];

sum2 += a[1][j];

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


sum3 += a[i][0];

sum4 += a[i][1];

chi_sq = sum * pow(a[0][0] * a[1][1] - a[0][1] * a[1][0], 2) / (sum1 * sum2 * sum3 * sum4);

return chi_sq;

You might also like