Created
April 16, 2014 17:01
-
-
Save brijeshb42/10907408 to your computer and use it in GitHub Desktop.
arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int isum(int a[],int s){ | |
| int i,sum=0; | |
| for(i=0;i<s;i++){ | |
| sum+=a[i]; | |
| } | |
| return sum; | |
| } | |
| double dsum(double a[],int s){ | |
| int i; | |
| double sum=0.0; | |
| for(i=0;i<s;i++){ | |
| sum+=a[i]; | |
| } | |
| return sum; | |
| } | |
| double iaverage(int a[],int s){ | |
| return (double)(isum(a,s)/(double)(s)); | |
| } | |
| double daverage(double a[],int s){ | |
| return (dsum(a,s)/(double)(s)); | |
| } | |
| int imax(int a[],int s){ | |
| int mx; | |
| mx = a[0]; | |
| int i; | |
| for(i=1;i<s;i++){ | |
| if(a[i]>mx) | |
| mx = a[i]; | |
| } | |
| return mx; | |
| } | |
| double dmax(double a[],int s){ | |
| double mx; | |
| mx = a[0]; | |
| int i; | |
| for(i=1;i<s;i++){ | |
| if(a[i]>mx) | |
| mx = a[i]; | |
| } | |
| return mx; | |
| } | |
| int imin(int a[],int s){ | |
| int mn; | |
| mn = a[0]; | |
| int i; | |
| for(i=1;i<s;i++){ | |
| if(a[i]<mn) | |
| mn = a[i]; | |
| } | |
| return mn; | |
| } | |
| double dmin(double a[],int s){ | |
| double mn; | |
| mn = a[0]; | |
| int i; | |
| for(i=1;i<s;i++){ | |
| if(a[i]<mn) | |
| mn = a[i]; | |
| } | |
| return mn; | |
| } | |
| void isort(int a[],int s,int v){ | |
| int i,j,tmp; | |
| for(i=0;i<s;i++){ | |
| for(j=i+1;j<s;j++){ | |
| if(v==0){ | |
| if(a[i]<a[j]){ | |
| tmp = a[i]; | |
| a[i] = a[j]; | |
| a[j] = tmp; | |
| } | |
| }else{ | |
| if(a[i]>a[j]){ | |
| tmp = a[i]; | |
| a[i] = a[j]; | |
| a[j] = tmp; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| void dsort(double a[],int s,int v){ | |
| int i,j; | |
| double tmp; | |
| for(i=0;i<s;i++){ | |
| for(j=i+1;j<s;j++){ | |
| if(v==0){ | |
| if(a[i]<a[j]){ | |
| tmp = a[i]; | |
| a[i] = a[j]; | |
| a[j] = tmp; | |
| } | |
| }else{ | |
| if(a[i]>a[j]){ | |
| tmp = a[i]; | |
| a[i] = a[j]; | |
| a[j] = tmp; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| int isearch(int a[],int s,int k){ | |
| int i; | |
| for(i=0;i<s;i++){ | |
| if(a[i]==k) | |
| return i; | |
| } | |
| return -999; | |
| } | |
| int dsearch(double a[],int s,double k){ | |
| int i; | |
| for(i=0;i<s;i++){ | |
| if(a[i]==k) | |
| return i; | |
| } | |
| return -999; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include "arrays.h" | |
| int main(int argc, char const *argv[]) | |
| { | |
| int a[10] = {8,9,7,6,3,4,2,1,5,10}; | |
| double d[10] = {10.1,19.5,5.6,4.5,7.8,8.6,9.8,7.5,102.0,100.1}; | |
| int i; | |
| printf("Initial integer array:\n"); | |
| for(i=0;i<10;i++){ | |
| printf("%d ", a[i]); | |
| } | |
| printf("\n"); | |
| printf("Initial double array:\n"); | |
| for(i=0;i<10;i++){ | |
| printf("%lf ", d[i]); | |
| } | |
| printf("\nisum: %d\n",isum(a,10)); | |
| printf("dsum: %lf\n",dsum(d,10)); | |
| printf("iaverage: %lf\n",iaverage(a,10)); | |
| printf("daverage: %lf\n",daverage(d,10)); | |
| printf("imax: %d\n",imax(a,10)); | |
| printf("dmax: %lf\n",dmax(d,10)); | |
| printf("imin: %d\n",imin(a,10)); | |
| printf("dmin: %lf\n",dmin(d,10)); | |
| isort(a,10,0); | |
| printf("integer array sorted in decreasing order:\n"); | |
| for(i=0;i<10;i++){ | |
| printf("%d ", a[i]); | |
| } | |
| dsort(d,10,0); | |
| printf("\ndouble array sorted in decreasing order:\n"); | |
| for(i=0;i<10;i++){ | |
| printf("%lf ", d[i]); | |
| } | |
| printf("\nPosition of 5 in integer array: %d\n", isearch(a,10,5)); | |
| printf("\nPosition of 10.6 in double array: %d\n", dsearch(d,10,10.6)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment