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

In C lauangue, You are to write a function int readDouble (double x[]) which wil

ID: 3758450 • Letter: I

Question

In C lauangue, You are to write a function int readDouble (double x[]) which will read (at most 50 ) doubles from the keyboard until a non-double 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 readDouble you need to count the number of doubles that have been inputted and return this value.

You are now to write the following functions:

double myInvSum (double x[], int size)

This function will return the sum 1/x[0]+1/x[1]+...+1/x[size-1]

void myPrint (double x[], int size)

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

void Sort (double x[], int size)

x is an double array of size many elements Sort will sort the array in increasing order

void specialSort (double x[], int size)

x is an double array of size many elements specialSort will sort the array based on the hundredths digit in increasing order. For example if x[] contains the three values 2.7345, 5.6, 3.466, 2.953. Then after calling specialSort the array x[] will be
5.6, 2.7345, 2.953, 3.466

The main will look like

int main()

{

   double List[50]={0};

int size;

.

.

.

size = readDouble (List);

printf(“Inverted sum is %lf ”, myInvSum (List, size));

myPrint (List, size);

Sort (List, size);

myPrint (List, size);

specialSort (List, size);

myPrint (List, size);

printf(“Inverted sum is %lf ”, myInvSum (List, size));

return 0;

}

Above is in only C lanauge

Explanation / Answer

#include <stdio.h>

//this function reads input from console and write into List
//returns size of double List
int readDouble(double* List)
{   
double input;
int size=0;

printf("Please enter double numbers (max 50) terminate at integer or 0 ");
while(1)
{
scanf("%lf",&input);

if(input==(int)input) //to check if input is integer? remember 0 is also a integer
break;

List[size] = input;
size++;
}
return size; //size of list
}

//calculate inverted sum according to the question
double myInvSum(double* List, int size)
{   
double sum = 0;
int i;

for(i=0;i<size;i++)
sum = sum + 1/List[i];
  
return sum;
}

//printing the list in one line, each separated by space
void myPrint(double* List,int size)
{
int i;
for(i=0;i<size;i++)
printf("%lf ", List[i]);

printf(" ");
}

void Sort(double* List,int size)
{

int c,d;
double swap;

//bubble sort
for (c = 0 ; c < ( size - 1 ); c++)
{
for (d = 0 ; d < size - c - 1; d++)
{
if (List[d] > List[d+1]) /* For increasing order use < */
{
swap = List[d];
List[d] = List[d+1];
List[d+1] = swap;
}
}
}

}

void specialSort(double* List,int size)
{
int c,d;
double swap;

//bubble sort
for (c = 0 ; c < ( size - 1 ); c++)
{
for (d = 0 ; d < size - c - 1; d++)
{
//((int)(List[d]*100)%10) gives the hundredth digit of List[d]
if (((int)(List[d]*100)%10) > ((int)(List[d+1]*100)%10))
{
swap = List[d];
List[d] = List[d+1];
List[d+1] = swap;
}
}
}
}

int main()
{
double List[50];
int size;
size = readDouble (List);
printf("Inverted sum is %lf with size = %d ", myInvSum (List, size),size);
myPrint (List, size);
Sort (List, size);
myPrint (List, size);
specialSort (List, size);
myPrint (List, size);
printf("Inverted sum is %lf ", myInvSum (List, size));

return 0;

}