简体   繁体   中英

Insert calculated values of an array into a new array

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM