Hi,
I wrote a program that is using arrays that calculates sum, and avegare of the number user inputs (maximum 20 numbers). Now I'm trying to write one more function that sorts the array, and another function that computes median. I don't even know where to start with these functions. Please help me figure out the sorting function that sorts in ascending order.
As for the median function, I think that I could write a code that finds out whether the array has even or odd number of values and then if odd, just take the middle value; else, add two middle values and divide them by 2.
Here's my code so far,
ThanksCode:#include<stdio.h> /*Function prototypes*/ int getdata(double[],int size); double computesum(int n, double x[]); double computemean(int n, double sum); int main(void) { double x[20]; int size=0; int n; double sum; /*Function calls */ n=getdata(x, 20); computesum(n, x); computemean(n, sum); } /*This function gets data from the user */ int getdata(double x[],int size) { //int x[20]; //int size=0 int tmp; int flag=0; int n=0; char cond, y; while(n<size && flag == 0) { printf("Enter a number\n"); scanf("%d", &tmp); printf("Are you done? Press Y if yes, otherwise press N\n"); scanf("%c", &y); scanf("%c", &cond); if (cond == 'n'){ flag=0; } else if(cond =='y') } else if(cond =='y') flag=1; x[n]=tmp; n = n+1; } return(n); } double computesum(int n, double x[]) { //int j; //for (j=0; j<n; j++) printf("%f", x[j]); int i; double tmp; tmp=0; i=0; int size; while(i<n) { tmp += x[i]; i = i+1; } printf("Sum of the values entered is %f\n",tmp); return(tmp); } double computemean(int n, double sum) { double mean, tmp; mean=tmp/n; printf("Mean of the values entered is %f\n",mean); }



LinkBack URL
About LinkBacks


