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

Write a program using C language Exploring Arrays Compile and run the program av

ID: 656155 • Letter: W

Question

Write a program using C language

Exploring Arrays

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

Here I am finding few problems with your code.

1. You have given maxval as 100. Its ok but In the output you are displaying six elements so you should ask user to input how many numbers of data user wants to calculate..here it is six. In your code, by not asking size of array, you are attracting garbage values at the vacant positions in the array of 100 elements.

2. Then program should ask those six elements to enter from user, in array and then display the array elements.

3. Next problem is you are taking two or three parameters each time in all the user defined functions. This is complicating the code.

----------------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>

#define MAXVALS 100       /* max number of values we can process */

int main()
{

double num[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 */

Printf("Enter the number of elements you want to enter");

Scanf ("%d" &n);

While (n > MAXVALS or n<=0)

Printf ("Error in Input: Please re-enter the numbers between 1 and 100 only");

Scanf ("%d", &n);

For (i=0; i<n; i++)

printf ("Enter next number ");
scanf ("%d" &num[i]);
Printf (num[i]);

Sum= Sum+ num[1];

Average= Sum/n;

printf("Average of the %d values read is: %f", n, Average);

For (i=0; i<=n; i++)

{

If (num[i]==Average)

Equal= Equal + 1;

printf("There are %d values equal to the average. ", Equal);

Elseif (num[i] < Average)
Less = Less+1;

printf("There are %d values less than the average. ", Less);   

Elseif (num[i] > Average)
High = High+1;

printf("There are %d values greater than the average. ", High);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote