/* C Program To Find Mean, Median, Standard Deviation And Mode For A Given Set Of Values */ #include<stdio.h> #include<math.h> #include<conio.h> void main() { int a[10],b[10],i,j,k=0,temp,sum=0,n,choice,count=1,max=0,mode; float mean=0,median=0,sd,x,add=0,adds=0; printf("Enter the size of array:\n"); scanf("%d",&n); printf("Enter The elements:\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Elements before sorting:\n"); for(i=0;i<n;i++){ printf("%d\n",a[i]); } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("Sorted Elements:\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); for(;;) { printf("1.Mean\t2.Median\t3.Standard Deviation\t4.Mode\t5.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: for(i=0;i<n;i++) { sum+=a[i]; mean=(float)sum/n; } printf("Mean= %.2f\n",mean); break; case 2: if(n/2==0){ median=a[n/2]; printf("Median= %f\n",median); break; } else median=(a[n/2]+a[n/2]+1)/2; printf("Median= %.2f\n",median); break; case 3: for(i=0;i<n;i++) add+=a[i]; mean=add/n; for(i=0;i<n;i++) adds+=adds+((a[i]-mean)*(a[i]-mean)); x=adds/n; sd=sqrt(x); printf("Standard deviation= %.2f\n",sd); break; case 4: for(i=0;i<n-1;i++) { mode=0; for(j=i+1;j<n;j++) { if(a[i]==a[j]) { mode++; } } if((mode>max)&&(mode!=0)) { k=0; max=mode; b[k]=a[i]; k++; } else if(mode==max) { b[k]=a[i]; k++; } } for(i=0;i<n;i++) { if(a[i]==b[i]) count++; } if(count==n) printf("There is no mode!\n"); else { printf("Mode:\n"); for(i=0;i<k;i++) printf("%d\n",b[i]); } break; case 5: exit(0); default: printf("Invalid Choice!\n"); break; } } }
Enter the size of array: 6 Enter The elements: 2 3 4 8 2 5 Elements before sorting: 2 3 4 8 2 5 Sorted Elements: 2 2 3 4 5 8 1.Mean 2.Median 3.Standard Deviation 4.Mode 5.Exit 1 Mean= 4.00 1.Mean 2.Median 3.Standard Deviation 4.Mode 5.Exit 2 Median= 4.00 1.Mean 2.Median 3.Standard Deviation 4.Mode 5.Exit 3 Standard deviation= 6.03 1.Mean 2.Median 3.Standard Deviation 4.Mode 5.Exit 4 Mode: 2 1.Mean 2.Median 3.Standard Deviation 4.Mode 5.Exit 5
No comments:
Post a Comment