【问题标题】:Assignment to expression with array type, assigning values to an array赋值给数组类型的表达式,给数组赋值
【发布时间】: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


    【解决方案1】:

    您不需要针对不同类型的天数的数组,只需根据温度范围递增的计数器变量。

    total 不应该是 sizeof(num),原因有两个:

    1. sizeof 计算字节数,而不是数组元素。
    2. 您只需要输入计数,而不需要数组的总大小。您可以通过添加每种日期的计数器来获得总数。

    您不必将温度放入数组中。只需在用户输入后将每天的温度添加到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);
    }
    

    【讨论】:

    • 谢谢!我什至没有意识到你使用的逻辑!但这是解决我的问题的好方法!
    【解决方案2】:

    您在不使用索引的情况下将 arr 值分配给 num 数组。你的 while 循环应该是这样的

    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++;
                     }
            }
    

    现在

    total = sizeof(num);
    

    这将为您提供 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;
    

    如果您只想知道冷天、热天的数量,那么您可以在 while 循环中增加它们并打印出来。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 2015-10-10
      • 1970-01-01
      相关资源
      最近更新 更多