【发布时间】:2021-05-16 07:08:28
【问题描述】:
我需要将这些值分配给数组,但我一直收到分配给表达式的错误,我想知道如何将值分配给这些数组。我尝试将 arr 的输入值分配给 num,但我相信我在该表达式中遗漏了一些东西。
此代码的工作是读取温度的输入,将输入分配到一天类型的类别中并给出温度的平均值
Enter a high temp reading (-99 to quit)> 100
Enter a high temp reading (-99 to quit)> 0
Enter a high temp reading (-99 to quit)> 50
Enter a high temp reading (-99 to quit)> -99
Hot days: 1
Pleasant days: 0
Cold days: 2
The average temperature was 50.00 degrees.
这是输出的样子
#include <stdio.h>
int main(void){
//declare variables
int num[30];
int Cday[30];
int Pday[30];
int Hday[30];
int total;
int total_ave;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
int arr;
//ask input store into num, then decides if the number goes into which array
do{
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &arr);
num = arr;
if(arr == -99)
{
break;
}
else if(arr<=60 && arr>=0){
Cday = num;
}
else if(arr>=60 && arr<=84){
Pday = num;
}
else if(arr<=84 && arr>=200){
Hday = num;
}
}while(num >=0);
//calculating the average
total = sizeof(num);
for(int i = 0;i< total; i++){
total_ave = total_ave + num[i];
}
ave = total_ave / total;
//to print the amount of times each day was in a category
NHday = sizeof(Hday)/sizeof(Hday[0]);
NPday = sizeof(Pday)/sizeof(Pday[0]);
NCday = sizeof(Cday)/sizeof(Cday[0]);
//printing the final statement once all the values are calculated
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
printf("The average temperature was %.2f degrees.", ave);
//stops compiling when -99 is entered not collected as information
//
//
return(0);
}
我也很想知道我如何使用 NHday CDay 和 Pday 计算来计算数组中有多少项目是正确的方法。
我也想知道我的平均值计算是否正确。感谢您的帮助。
【问题讨论】:
标签: arrays c compiler-errors