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);
}