1. ## Normalization Problem

I must write a function that has a one dimensional double array and the number of values in the array as its arguments. Normalize the values. I must also print the maximum, minimum, average and numbers of values above the average in the array.

The equation that computes the normalized value from a value x is:
Normalized x= (x-min(array))/(max(array)-min(array))

My code does not print the correct normalized value, average and values above average.

Code:
```#include <stdio.h>

int findMax( double array1[], int num_elements) // This function computes the maximum value of the array
{
int i, max=-32000;
for (i=0; i<num_elements; i++)
{
if (array1[i]>max)
{
max=array1[i];
}
}
return(max);
}
int findMin( double array1[], int num_elements) // This function computes the minimum value of the array
{
int i, min=0;
for (i=0; i<num_elements; i++)
{
if (i==0)
min=array1[i];
else
if (array1[i]<min)
min=array1[i];
}
return(min);
}
double averageX(double array1[], int num_elements)  //This function computes the average value of the array, and the number of values that are
// larger then the average
{
float average;
int i, sum=0, valuesAboveAverage=0;

for (i=0; i<num_elements; i++)
{
sum+=array1[i];
average=sum/num_elements;

if (array1[i]>average)
valuesAboveAverage++;
}
printf(" Values above the average: %d\n", valuesAboveAverage);
printf("Average: %f\n", average);

return(average);

}

void norm_1D(int min, int max, int average, double array1[], int num_elements)  // This function computes the normalized values of the original
// array values
{
int i;
double normalizedVal[10];
for (i=1; i<num_elements; i++)
{
normalizedVal[i]=(array1[i]-min)/ (max-min);
}
for (i=0; i<10; i++)
printf("Normalized Values: %5.2f\n", normalizedVal[i]);
}

int main(void)
{
double  array1[10] = {1, 0, -8, 10, 8.8, 4.5, 2, 12, 9, 5};
int max, min;
double average;

max = findMax(array1, 10);
min = findMin(array1, 10);
average = averageX(array1, 10);
norm_1D(min, max, average, array1, 10);

printf("The min is %d\n", min);
printf("The max is %d\n", max);

return(0);
}```

2. Instead of using -32000 as the initial maximum value for findMax, #include <limits.h> and use INT_MIN. For findMin, you use the other approach of setting the initial minimum value to be the value of the first element. In this case, instead of constantly checking for i == 0 in the loop body, just check that num_elements is greater than 0 before the loop. If not, return 0, otherwise initialise min = array1[0] and start looping from the second element.

Now, onto your problem with the average: instead of computing the average inside the loop, compute it after the loop. Only after you have processed all the elements can you compute the average. Consequently, you need to loop again to determine how many elements are greater than the average. In fact, I would split the averageX function into two: one function to compute the arithmetic mean and return it, another function to count the number of elements greater than the arithmetic mean and return that count.

For norm_1D, I suggest that normalizedVal not be a local array. Rather, declare normalizedVal in the main function, then pass a pointer to its first element to norm_1D, just like what you do for array1. This way, norm_1D can be used with arrays of different sizes without problems. That said, I note that you start the first for loop in norm_1D from i = 1 instead of i = 0, and you don't use the average parameter.

EDIT:
I noticed a few other problems too, e.g., sum is an int rather than a double. average should be a double, but if you split the function as I suggested that you don't even need it to begin with. Oh, and your indentation needs work.

3. Okay, so everything is working but the above average function. I put it in another function but it still won't give me the right answer.

Code:
```void aboveAverage(double average, int num_elements, double array1[])
{
int i;
double aboveAverage;
for (i=0; i<num_elements; i++)
{
if (array1[i]>average)
aboveAverage++;
}

printf("Values above the average: %5.2f\n", aboveAverage);

}```

4. You forgot to initialise aboveAverage to 0. Furthermore, it should be an int, not a double, and then you should print it as an int.

5. It is working fine now, thank you very much!

6. You're welcome. You really should move the printf to outside of the function: the function can return the count, then the caller can decide how to print it.