Home > Net >  Insert calculated values of an array into a new array
Insert calculated values of an array into a new array

Time:10-07

I have a function computeNormal that I want to perform a calculation on values stored in an array (value - rounded mean value of the array), then store the results in another array and print them in my other compute function.

This is my code, but it returns the wrong values. What am I doing wrong?

int computeNormal(int normals[], int measurements[], int nrOfMeasurements)
{
    int sum = 0, normal = 0;
    float mean;
 
    for (int i = 0; i < nrOfMeasurements; i  )
    {
        sum  = measurements[i];
    }
 
    mean = ((float)sum/nrOfMeasurements);
 
    for (int i = 0; i < nrOfMeasurements; i  )
    {
        normal = measurements[i] - round(mean);
        normals[i] = normal;
    }
 
}
void compute(int measurements[], int nrOfMeasurements)
{
    int normals[nrOfMeasurements];
    printf("[ ");
    for (int i = 0; i < nrOfMeasurements; i  )
    {
        printf("%d ", computeNormal(normals, measurements, nrOfMeasurements));
    }
    printf("]");
    printf("\n");
}

nrOfMeasurements = The amount of input values the user has entered, between 1-10 that has been assigned in an enter function, measurements[] = The array that stores the original input values, normals[] = the array I want to print my calculated values.

CodePudding user response:

First call the computeNormal function to let it do its calculations and save it into your array.

Then print the values from the array.

Like this:

computeNormal(normals, measurements, nrOfMeasurements);

for (int i = 0; i < nrOfMeasurements; i  )
{
    printf("%d ", normals[i]);
}
  • Related