-1
所以我寫了一個代碼,它讀取包含一組數據的文件。之後我將數據四捨五入到小數點後三位。稍後,我試圖在一些特定範圍內取平均數據。範圍介於0,0.5和0.5到1.0和...之間。但問題是,當我這樣做時,它不使用四捨五入的數據,它使用原始數據。我應該如何更改我的代碼,以便它使用四捨五入的數據?我怎樣才能做出代表四捨五入數據的東西,以便我可以將其用於剩餘的編碼? 我的代碼是使用四捨五入的數據取平均值
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Data size
#define MAX_ROWS 20
#define MAX_COLUMNS 20
#define LOW_ERROR 0.0
#define HIGH_ERROR 2.5
int main(void)
{
// Decalred variables
int rowIndex = 0;
int columnIndex = 0;
double rawData[MAX_ROWS][MAX_COLUMNS]; // 2-dimensional array to store our raw data
int decimalPlaces = 3;
float rangeValue[6] = { 0.0,0.5,1.0,1.5,2.0,2.5 };
int i, num = 0;
float total = 0.0, average;
// Print out the rawdata array
printf(" --- RAW DATA ---\n");
for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++)
{
// Read up until the last value
for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++)
{
printf("%.9lf ", rawData[rowIndex][columnIndex]);
}
printf("\n");
}
// Print out the roundup data array
printf(" --- ROUNDED DATA ---\n");
for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++)
{
// Read up until the last value
for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++)
{
if (rawData[rowIndex][columnIndex] < LOW_ERROR)
printf("%.3f ", LOW_ERROR);
else if (rawData[rowIndex][columnIndex] > HIGH_ERROR)
printf("%.3f ", HIGH_ERROR);
else
printf("%.3f ", ceil(rawData[rowIndex][columnIndex] * 1000.0)/1000.0);
}
printf("\n");
}
//Calculate and store the averages for each range
printf(" --- RANGE TABLE ---\n");
for (i = 0; i < 5; i++)
{
for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++)
{
for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++)
if (rawData[rowIndex][columnIndex] > rangeValue[i] && rawData[rowIndex][columnIndex] <= rangeValue[i + 1])
{
total = total + rawData[rowIndex][columnIndex];
num++;
}
}
average = total/num;
printf("%f \n", average);
total = 0;
average = 0;
num = 0;
}
return 0;
}
我可以看到你有代碼來printf()取出四捨五入的值,但不是你實際上四捨五入在數組中的值?另外,縮進需要注意 - 我無法清楚地看到main()開始/結束的位置。 –