#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* to initialize your random generator */
#define BUFF_SIZE 10
#define FIVE 5
#define TEN 10
#define ZERO 0
#define ONE 1
float min=0;
float max=0;
float average=0;
float input_buffer[BUFF_SIZE+2] = {0};
float output_buffer[FIVE] = {0};
float local_buffer[TEN];
int no_of_data_pts=0;
int function_id=0;
// a function for generating data size, function ID and
// all data, and putting all of it into input_buffer
void generate_data() {
/* initialize random seed: */
srand ( time(NULL) );
/* Generate a random number: */
input_buffer[0] = floor(1 + 10*(float)rand()/RAND_MAX);
input_buffer[1] = floor(1 + 4*(float)rand()/RAND_MAX);
int i;
for (i=0; i < (int)input_buffer[0]; i++ ){
input_buffer[i+2]=(float)rand()/RAND_MAX;
}
}
// a function for copying the content of input_buffer into
// local buffer (called here my_buffer)
void reading() {
no_of_data_pts= (int)input_buffer[0];
function_id= (int)input_buffer[1];
int i;
for (i=0; i < no_of_data_pts; i++ ){
local_buffer[i]=input_buffer[2+i];
}
}
// a function for processing the content of local buffer;
// it reads function_ID and number of data points from my_buffer
// and saves the results also into my_buffer
void processing() {
float num=0;
int i;
float sum = 0;
float min=0;
for (i=0; i<no_of_data_pts; i++){
num = local_buffer[i+1];
if (num < min) {
min=num;
}
}
for (i=0;i<no_of_data_pts;i++){
num = local_buffer[i+1];
if (num < max) {
max=num;
}
}
for (i=0;i<no_of_data_pts;i++) {
sum = sum + local_buffer[i];
}
average = sum/no_of_data_pts;
}
// a function for copying the content of my_buffer to the
// output_buffer (according to the patter explained earlier)
void writing() {
switch (function_id){
case 1:
output_buffer[0]= min;
printf ("Minimum value is: %f ",output_buffer[0]);
break;
case 2:
output_buffer[0]= max;
printf ("Maximum value is: %f ",output_buffer[0]);
break;
case 3:
output_buffer[0]= average;
printf ("Average value is: %f ",output_buffer[0]);
break;
case 4:
output_buffer[0]= min;
output_buffer[1]= max;
output_buffer[2]= average;
printf ("Minimum, maximum and average value is: %f,%f,%f ",output_buffer[0],output_buffer[1],output_buffer[2]);
break;
}
}
int main () {
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
generate_data();
reading();
processing();
writing();
return 0;
}
所以这是C作业。
因此,当我运行此代码时,它应该生成一些从0到1的随机数,并计算最小值,最大值和平均值。
从输入缓冲区读取数据包括:
数据处理涉及:
将数据写入输出缓冲区包括:
写一个数字,称为处理函数的ID,表示操作是什么 对数据执行(范围从1到4)
写出结果,这取决于它的功能:
§如果ID为1:写入最小值
§如果ID为2:写入最大值
§如果ID为3:写入平均值
§如果ID为4:写入最小值,最大值和平均值
然后我在程序的最后打印结果。
代码运行正常。没有错误但由于某种原因,我无法弄清楚为什么min和max的值总是为0!我的平均值很好,但由于某种原因,我的最大值和最小值总是为0,这是不正确的。
答案 0 :(得分:5)
首先,这个
if (num < max) {
max = num;
}
应
if (num > max) {
max = num;
}
至少。
使用上述方法初始化min
和max
至0
可能不适用于任何类型的输入。
确保检测输入初始化的所有可能性
min = FLT_MAX;
和
max = -FLT_MAX;
如果因任何原因未知最小/最大可能值,请更改检测min
和max
的方式:
void processing() {
float num=0;
int i;
float sum = 0;
min = local_buffer[1];
for (i = 1; i < no_of_data_pts; i++){
num = local_buffer[i + 1];
if (num < min) {
min = num;
}
}
max = local_buffer[1];
for (i = 1; i < no_of_data_pts; i++){
num = local_buffer[i + 1];
if (num > max) {
max = num;
}
}
...
此外,^ 2代码定义min
两次:
processing()
,影子1。删除2 nd 定义。
代码错过了原型floor()
。这就是编译器假定它返回int
的原因。这可能会调用未定义的行为。
要修复此添加
#include <math.h>
同样^ 3在local_buffer
中读出processing()
时,代码使用了错误的索引。索引从0
开始,而不是1
。所以它总是读出一个未被发生器设置的最后一个值。最后,0
min
来自。{/ p>
更正此问题会使processing()
的上述代码段显示为:
void processing() {
float num=0;
int i;
float sum = 0;
min = local_buffer[0];
for (i = 1; i < no_of_data_pts; i++){
num = local_buffer[i];
if (num < min) {
min = num;
}
}
max = local_buffer[0];
for (i = 1; i < no_of_data_pts; i++){
num = local_buffer[i];
if (num > max) {
max = num;
}
}
...
最后的说明: