So my teacher gave us this file

``````#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", n, average);
equal = tableMatchingElements(table, n, average);
printf("There are %d values equal to the average.\n", equal);
printf("The values greater than the average:\n");
tablePrint(table, n);
printf("The values less than the average:\n");
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.\n",
cnt);
break;
}
if (cnt == max)      /* no room to store this value */
{
printf("Array full after reading %d values.\n", 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\n", 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 */
}
``````

And he wants us to break it into .c and .h files and compile using a makefile to produce an output like this

``````            10 20 30 30 40 50
Average of the 6 values read is: 30.000000
There are 2 values equal to the average.
There are 2 values greater than the average:
40
50
There are 2 values less than the average:
10
20
``````

I already break them down and made the makefile but what functions do I need to fix to produce that output? Please help.

## Recommended Answers

put lines 1 thru 8 in a header file
put the remainder in a .c file
at the top of the .c file include the header file that you created in the first step
create the make file

Once you do the above you need to finish writing the functions …

You don't want to do that scanf() stuff because the array has already been filled in the TableFill() function. All you want to do here is just use a loop that counts from 0 to n and sum up the values that are in the array.

variables named sum and …

## All 8 Replies

put lines 1 thru 8 in a header file
put the remainder in a .c file
at the top of the .c file include the header file that you created in the first step
create the make file

Once you do the above you need to finish writing the functions in the .c file. For example function tableAverage() is incomplete. You need to sum all the values in the array then divide the sum by the number of elements in the array.

Yeah I broke them donw into .c and .h and made a makefile already but I didn't post it. But the for function tableAverage() I tried doing it like this:

``````double tableAverage(double a[], int num)
{
int average;  int count; int sum;
count=0; sum=0;
while(scanf("%d",&num)!=EOF)
{
count=count+1;
sum=num+sum;
}
average=(sum)/(count);
}
``````

But it didn't quite work. Can you help me?

You don't want to do that scanf() stuff because the array has already been filled in the TableFill() function. All you want to do here is just use a loop that counts from 0 to n and sum up the values that are in the array.

variables named sum and average will need to be double instead of int because the array is an array of doubles.

Oh ok so would it just be like:

``````double tableAverage(double a[], int num)
{
double average;  int count; double sum;
sum=count=value=0;
while(count>0)
{
count=count+1;
sum=sum+num;
}
average=(sum)/(count);
}
``````

Oh ok so would it just be like:

No, not really. You're summing the contents of the array, so `num` should control the loop and index `a` for the value. You also need to return the average rather than store it in a local variable:

``````double tableAverage(double a[], int num)
{
double sum = 0;
int count = 0;

while (count < num) {
sum += a[count];
++count;
}

return sum / num;
}
``````

I also included two convenience operators for adding and assigning, the += and ++ operators. They do basically the same thing you were doing before, just shorter. Another option for making the code more concise, conventional, and readable might be using a counted loop and changing the variable names a bit:

``````double tableAverage(double a[], int size)
{
double sum = 0;

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

return sum / num;
}
``````

That might not seem more readable to a beginner, but experienced C programmers will have an easier time with it than the while loop and unconventional variable names, which cause us to stumble a bit in terms of pattern matching.

Thank you for helping. But now I have to write 3 functions to match the input value to whether if it is equal, smaller, or larger then the average and print them out. To produce the output:

`````` There are 2 values equal to the average.
There are 2 values greater than the average:
40
50
There are 2 values less than the average:
10
20
``````

I also went ahead and modified my average function:

``````double tableAverage(double a[], int num)
{
double sum = 0;
int count = 0; float average;
while (count < num) {
sum += a[count];
++count;
average=sum/num;
}
return average;  /* doesn't really compute the average */
}
``````

This is my matching input to see if it's the same as the average:

``````int tableMatchingElements(double a[], int num, double target)
{
int k; float average;

while(num< average)
{
++k;}

printf("%d\n", a[k]);

/* number of values equal to the target */
}
``````

The thing is when I compile, and EOF after getting the average, nothing happened so I am stuck here.

These are my functions to determine whether the input is larger or smaller than the average:

``````void tablePrintLarger(double a[], int num)
{
int countl; countl=0; float average;
while (num>average)
{
countl=countl+1;
}
printf("%d\n", a[countl]);
}

void tablePrintSmaller(double a[], int num)
{
int counts; counts=0; float average;
while (num<average)
{
counts=counts+1;
}
printf("%d\n", a[counts]);
}
``````

I also changed the header:

`````` printf("There are &d values greater than the average:\n");
tablePrintLarger(table, n);
printf("There are %d  values less than the average:\n");
tablePrintSmaller(table, n);
}
``````

I deleted the function tablePrint from the problem because I broke it down into these two. However, when I compile it give me a "conflicting types [enable by deafault problem] " and I assume that it is because I use the same (double a[], int num) for both of them. But I want to compare it with the num, or input, so I don't know what to substitute in.

Please Help me. I really appreciate your effort.

The major reason that your tableAverage function fails is because the average is being calculated inside the loop that iterates through the array.

``````    while (count < num) {
sum += a[count];
++count;
average=sum/num; // this should not be inside the loop
}
``````

You also declare `float average` when infact you want a double. There's really no reason to declare a variable for average either. Just follow deceptikon's previous advice for the tableAverage.

``````int tableMatchingElements(double a[], int num, double average)
{
int ct = 0; // to hold the count of matching elements

// loop through the array
for (int i = 0; i < num; ++i)
{
if (a[i] == average)
{
++ct;
}
}

return ct;
}

int equal = tableMatchingElements(table, n, average);
printf("There are %d values equal to the average.\n", equal);
``````

The other functions are basically the same as tableMatchingElements(...), except for where you compare a[i] to the average.
e.g for greater

``````    if (a[i] > average)
{
// TODO print value of a[i]
++ct;
}
``````

Edit:

``````    while (count < num) {
sum += a[count];
++count;
average=sum/num; //
}
``````

That should actually work, though it's not optimal because average is being calculated every time you loop through the array, though the result only becomes valid on the final loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.