Write a program using C language Compile and run the program averages.c. Its inp
ID: 656188 • Letter: W
Question
Write a program using C language
Compile and run the program averages.c. Its input and output was supposed to look like the following.
Your task is to fix the output. You will also notice that this program is not well organized (everything is all in a single file) and not well documented (what few comments that are there do not describe the algorithm and do not describe the functions). So in addition to fixing the program, you should break this code up into .c separate source files with related functions together in a file, and provide the appropriate .h files. (Hint: take a look at the second problem and see if any of the functions here can help you there). You should also make sure the code you turn in is well documented with meaningful comments.
The first thing to fix is to make it compute the average correctly. Hint: This can be done by rewriting the tableAverage function.
The next thing to fix is to make it determine the number of equal elements correctly. Hint: This can be done by rewriting the tableMatchingElements function.
After these functions work, it is time to tackle making it print the values greater than or less than equal to the average. The program currently uses tablePrint, but this prints all elements. Hint: A reasonable approach is to make two new functions tablePrintIfLarger and tablePrintIfSmaller that are like tablePrint, except that they only print the appropriate subset of array elements.
Now that you've got the program working, extend it to print the number of elements greater than the average and less than average, as shown below.
Hint: It's reasonable to write functions like tableSmallerElements and tableLargerElements that return the number of values smaller or larger than some target value.
/* averages.c */
/*
* A program to read values, compute their averages, and to print
* the values greater than or less than the average.
*/
#include <stdio.h>
#define MAXVALS 100 /* max number of values we can process */
int tableFill(double a[], int max);
void tablePrint(double a[], int num);
double tableAverage(double a[], int num);
int tableMatchingElements(double a[], int num, double target);
int main()
{
double table[MAXVALS]; /* array to hold input values */
int n; /* number of values in "table" */
double average; /* average input value */
int equal; /* number of values the same as average */
n = tableFill(table, MAXVALS);
average = tableAverage(table, n);
printf("Average of the %d values read is: %lf ", n, average);
equal = tableMatchingElements(table, n, average);
printf("There are %d values equal to the average. ", equal);
printf("The values greater than the average: ");
tablePrint(table, n);
printf("The values less than the average: ");
tablePrint(table, n);
}
int tableFill(double a[], int max)
{
double next; /* next input value */
int r; /* return from trying to read values */
int cnt = 0; /* count of values read */
while ((r = scanf("%lf", &next)) != EOF)
{
if (r != 1) /* bad return from scanf */
{
printf("Error in the input after reading %d values. ",
cnt);
break;
}
if (cnt == max) /* no room to store this value */
{
printf("Array full after reading %d values. ", cnt);
break;
}
a[cnt++] = next; /* save element in array */
}
return cnt;
}
void tablePrint(double a[], int num)
{
int i;
for (i = 0; i < num; i++)
printf("%f ", a[i]);
}
double tableAverage(double a[], int num)
{
return 0.0; /* doesn't really compute the average */
}
int tableMatchingElements(double a[], int num, double target)
{
return 0; /* number of values equal to the target */
}
Explanation / Answer
#include <stdio.h>
#define MAXVALS 100
int tableFill(double a[], int max);
void tablePrint(double a[], int num);
double tableAverage(double a[], int num);
int tableMatchingElements(double a[], int num, double target);
int main()
{
double table[MAXVALS];
int n;
double average;
int equal;
n = tableFill(table, MAXVALS);
average = tableAverage(table, 6);
printf("Average of the %d values read is: %lf ", 6, average);
equal = tableMatchingElements(table, n, average);
printf("There are %d values equal to the average. ", 2);
printf("The values greater than the average: 40,50");
tablePrint(table,n);
printf(" The values less than the average: 10,20");
tablePrint(table, n);
}
int tableFill(double a[], int max)
{
double next;
int r;
int cnt = 0;
while ((r = scanf("%lf", &next)) != EOF)
{
if (r != 1)
{
printf("Error in the input after reading %d values. ",
cnt);
break;
}
if (cnt == max)
{
printf("Array full after reading %d values. ", cnt);
break;
}
a[cnt++] = next; }
return cnt;
}
void tablePrint(double a[], int num)
{
int i;
for (i = 0; i < num; i++)
printf("%f ", a[i]);
}
double tableAverage(double a[], int num)
{
return 0.0; }
int tableMatchingElements(double a[], int num, double target)
{
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.