Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please help me on my C programming. I had been working on this lab f

ID: 3682485 • Letter: C

Question

Can someone please help me on my C programming. I had been working on this lab for a week and still dont get it. We have to use Array and pointers.I did 5 functions and it shows error. I paste my code below..on the very last.I'll appreciate it.

(1) You are to write a function int initializeArray (double x[]) which will read (at most 50) doubles from the keyboard until a character or zero is inputted from the keyboard. You are to store each double in the array List which you will define in the main. In initializeArray you need to count the number of doubles that have been inputted and return this value.

(2)You are now to write the following functions:

a)      double Average (double x[], int size)

This function will return the average of the array elements.

b)     double Variance (double x[], int size)

This function will return the variance of the array elements.

Formula for calculating variance:

Where N is the total number of elements and µ is the average.

c)      Void Display (double x[], int size)

x is a double array of size many elements and Display prints each element of this double array to the monitor, so that every line has four doubles and the doubles are aligned in a column format.

d)     void Sort (double x[], int size)

x is a double array of size many elements and Sort will sort the array in decreasing order.

e)      void InverseSort (double x[], int size)

x is a double array of size many elements and InverseSort will sort the array based on the inverse of the array elements (i.e. 1/x[0], 1/x[1],..., 1/x[size-1]) in decreasing order.

f)       void SumDigitSort (double x[], int size)

x is a double array of size many elements and SumDigitSort will sort the array in increasing order based on the sum of all digits the double has before the decimal point.

For example if x[] contains the four values 42.7345, 75.6, 300.466, 28.95. Then after calling SumDigitSort the array x[] will be 300.46, 42.734, 28.95, 75.623.

The main will look like

int main()

{

            double List[50]={0};

int size;

.

.

.

size = initializeArray (List);

Display (List, size);

printf (“Average is %lf ”, Average (List, size));

printf (“Variance is %lf ”, Variance (List, size));

            Display (List, size);

Sort (List, size);

Display (List, size);

InverseSort (List, size);

Display (List, size);

SumDigitSort (List, size);

Display (List, size);

printf (“Average is %lf ”, Average (List, size));

printf (“Variance is %lf ”, Variance (List, size));

return 0;

}

Explanation / Answer

#include <stdio.h>

int initializeArray(double x[]){
   int count=0;

   while(count<50){
       double num;
       scanf("%lf",&num);
       if(num==0.0)
           break;
       x[count]=num;
       count++;
   }
   return count;
}

double Average(double x[], int size){
   int i=0;
   double sum=0.0;

   for(i=0;i<size;++i)
       sum=sum+x[i];

   return sum/size;
}

double Variance(double x[],int size){
   double avg=Average(x,size);
   double variance=0.0;
   int i=0;

   for(i=0;i<size;++i)
       variance=variance+((x[i] - avg) * (x[i] - avg));

   return variance/size;
}

void swap(double *xp, double *yp){
double temp = *xp;
*xp = *yp;
*yp = temp;
}

void Display(double x[],int size){
   int i=0;

   for(i=0;i<size;++i){
       if(i%4==0)
           printf(" ");
       printf("%lf ",x[i]);
   }
   printf(" ");
}

void Sort(double x[], int size){
   int i,j,max_idx;

for(i=0; i<size-1;i++){
max_idx=i;
for(j=i+1; j<size; j++){
   if(x[j] > x[max_idx])
   max_idx = j;
}

swap(&x[max_idx], &x[i]);
}
}

void InverseSort(double x[], int size){
   int i,j;

   for(i=0;i<size-1;i++){
       for(j=0;j<size-i-1;j++){
           if(x[j]>x[j+1]){
               swap(&x[j],&x[j+1]);
           }
       }
   }
}

int digitSum(int n){
   int sum=0;

   while(n>0){
       sum=sum+n%10;
       n=n/10;
   }
   return sum;
}

void SumDigitSort(double x[], int size){
   int i,j;

   for(i=0;i<size-1;i++){
       for(j=0;j<size-i-1;j++){
           int d1=digitSum((int)x[j]);
   int d2=digitSum((int)x[j+1]);
           if(d1>d2){
               swap(&x[j],&x[j+1]);
           }
       }
   }
}


int main(){
   double List[50]={0};
   int size;

   size = initializeArray (List);

   Display(List, size);

   printf("Average is %lf ", Average (List, size));

   printf ("Variance is %lf ", Variance (List, size));

   Display (List, size);

   Sort (List, size);

   Display (List, size);

   InverseSort (List, size);

   Display (List, size);

   SumDigitSort (List, size);

   Display (List, size);

   printf ("Average is %lf ", Average (List, size));

   printf ("Variance is %lf ", Variance (List, size));

   return 0;
}