簡體   English   中英

將數組的計算值插入新數組

[英]Insert calculated values of an array into a new array

我有一個 function computeNormal ,我想對存儲在數組中的值(值 - 數組的四舍五入平均值)執行計算,然后將結果存儲在另一個數組中並在我的另一個compute function 中打印它們。

這是我的代碼,但它返回了錯誤的值。 我究竟做錯了什么?

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 = 用戶輸入的輸入值的數量,在enter function 中分配的 1-10 之間,measurements[] = 存儲原始輸入值的數組,normals[] = 我要打印我的計算結果的數組值。

首先調用computeNormal function 讓它進行計算並將其保存到您的數組中。

然后打印數組中的值。

像這樣:

computeNormal(normals, measurements, nrOfMeasurements);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM