Arrays 赋值给数组类型的表达式,将值赋值给数组
我需要将这些值赋给数组,但我一直收到表达式错误的赋值,我想知道如何将值赋给这些数组。我尝试将arr的输入值赋值到num中,但我相信我在该表达式中遗漏了一些东西 此代码的工作是读取温度输入,将输入分配到一天类型的类别中,并给出温度的平均值Arrays 赋值给数组类型的表达式,将值赋值给数组,arrays,c,compiler-errors,Arrays,C,Compiler Errors,我需要将这些值赋给数组,但我一直收到表达式错误的赋值,我想知道如何将值赋给这些数组。我尝试将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 qui
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);
}
#包括
内部主(空){
//声明变量
int num[30];
int Cday[30];
国际邮递日[30];
国际日[30];
整数合计;
int total_ave;
双大街;
int-NHday=0;
int NPday=0;
int NCday=0;
int arr;
//向num请求输入存储,然后决定该数字是否进入哪个数组
做{
printf(“输入高温读数(-99以退出)>”;
scanf(“%d”和&arr);
num=arr;
如果(arr==-99)
{
打破
}
否则如果(arr=0){
Cday=num;
}
否则,如果(arr>=60&&arr=0);
//计算平均数
总计=sizeof(num);
对于(int i=0;i
我还想知道我如何使用NHday CDay和Pday计算来计算阵列中的项目数是否正确
我还想知道我对平均值的计算是否正确。非常感谢您提供的任何帮助。您不需要为不同类型的天数设置数组,只需要计数器变量,您可以根据温度范围增加这些变量
total
不应为sizeof(num)
,原因有二:
sizeof
计算字节,而不是数组元素total_ave
变量中即可
在计算ave
时,需要将其中一个变量转换为double
,以便使用浮点除法而不是整数除法
#include <stdio.h>
int main(void){
//declare variables
int num;
int total;
int total_ave = 0;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
//ask input store into num, then decides if the number goes into which array
while (1) {
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &num);
if(num == -99)
{
break;
}
else if(num<=60){
NCday++;
}
else if(num<=84){
NPday++;
}
else {
NHday++;
}
total_ave += num;
}
//calculating the average
total = NHday + NPday + NCday;
ave = (double)total_ave / total;
//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);
return(0);
}
#包括
内部主(空){
//声明变量
int-num;
整数合计;
int total_ave=0;
双大街;
int-NHday=0;
int NPday=0;
int NCday=0;
//向num请求输入存储,然后决定该数字是否进入哪个数组
而(1){
printf(“输入高温读数(-99以退出)>”;
scanf(“%d”和&num);
如果(num==-99)
{
打破
}
否则,如果(num您在不使用索引的情况下将arr值分配给num数组
while(i<30){
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &arr);
if(arr==-99){
break;
}
num[i] = arr;
i++;
total++;
if(arr<=60 && arr>=0){
NCday++;
}
else if(arr>=60 && arr<=84){
NPday++;
}
else if(arr>=84 && arr<=200){
NHday++;
}
}
这将给出num数组的总大小,即30*4=120,因为int数据类型的大小为4字节。
如果您没有填充全部30个值,那么您需要知道将从该数组中使用的值的数量。这就是为什么我在num数组中插入值时增加total,这将在以后有所帮助
for(int i = 0;i< total; i++){
total_ave = total_ave + num[i];
}
ave = total_ave / total;
首先初始化变量
int num[30];
int total=0;
int total_ave=0;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
int arr=0;
int i=0;
谢谢!我甚至没有意识到你使用的逻辑!但这是解决我问题的好方法!
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
int num[30];
int total=0;
int total_ave=0;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
int arr=0;
int i=0;