【问题标题】:Why is my c code not properly storing and/or displaying the values of the array inputted by the user?为什么我的 c 代码不能正确存储和/或显示用户输入的数组的值?
【发布时间】:2020-10-04 17:21:14
【问题描述】:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define NUMS 3

int main(void) // call main
{
    int high[NUMS] = { 0 }; // single int for high
    int low[NUMS] = { 0 }; // single int for low
    double a = 0; 
    double b = 0;
    double c = 0;
    double d = 0;
    double e = 0;
    double f = 0;
    double total = 0;
    double avg = 0;
    int i = 0;

    for (i = 0; i < NUMS; i = i + 1) // for loop running to have the user input 3 day's worth of temperatures
    {
        printf("Enter the high value for day %d: ", i + 1);
        int tempHigh = scanf("%d", &high[i]);

        printf("Enter the low value for day %d: ", i + 1);
        int tempLow = scanf("%d", &low[i]);

        while (high[i] < low[i] || high[i] > 40 || low[i] < -40) // while loop checks that each temperature fits the criteria
        {
            printf("Incorrect values. Try again.\n");
                
            printf("Enter the high value for day %d: ", i + 1);
            int tempHigh2 = scanf("%d", &high[i]);

            printf("Enter the low value for day %d: ", i + 1);
            int tempLow2 =scanf("%d", &low[i]);     
        }
    }

    a = (double)high[0]; // each temperature stored as its own variable outside of the array and converted to double
    b = (double)high[1];
    c = (double)high[2];
    d = (double)low[0];
    e = (double)low[1];
    f = (double)low[2];
    total = a + b + c + d + e + f; // calculating total
    (double)avg = total / ((double)NUMS * 2); // calculating average

    printf("The average temperature of the three days is: %p", &avg); // printing average.

    return 0;

}

当我从数组中打印出平均值、总数或直接值时,它最终会打印为一串混合字符,我不知道为什么。没有错误或警告可供我处理,因此我发现很难找出问题所在。

【问题讨论】:

  • ...%p", &amp;avg) 正在输出该变量的 地址。我建议...%f", avg)。函数printf()scanf() 相似,但完全不同。
  • 输出中有什么,你不想要什么?
  • @WeatherVane 我将其更改为 %f,现在它会打印数字,但它只会导致 0,所以我认为这些值不会被存储
  • 将违规行更改为printf("The average temperature of the three days is: %f", avg); 后无法重现。这是两个变化。当然,平均值可能 0.000000,但不会是你声称的0
  • @WeatherVane 我每天的测试值是 5 和 2,所以平均值绝对不是 0。

标签: arrays c scanf


【解决方案1】:

编辑了您的代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define NUMS 3

int main(void) // call main
{
    int high[NUMS] = { 0 };  // single int for high
    int low[NUMS] = { 0 };  // single int for low
    double a = 0,b = 0,c = 0,d=0,e=0,f=0,total=0,avg=0;
    int i = 0;

    for (i = 0; i < NUMS; i = i + 1) // for loop running to have the user input 3 day's worth of temperatures
    {
        printf("Enter the high value for day %d: ", i + 1);
        int tempHigh = scanf("%d", &high[i]);

        printf("Enter the low value for day %d: ", i + 1);
        int tempLow = scanf("%d", &low[i]);

        while (high[i] < low[i] || high[i] > 40 || low[i] < -40) // while loop checks that each temperature fits the criteria
        {
            printf("Incorrect values. Try again.\n");
                
            printf("Enter the high value for day %d: ", i + 1);
            int tempHigh2 = scanf("%d", &high[i]);

            printf("Enter the low value for day %d: ", i + 1);
            int tempLow2 =scanf("%d", &low[i]);     
        }
    }

    a = (double)high[0]; // each temperature stored as its own variable outside of the array and converted to double
    b = (double)high[1];
    c = (double)high[2];
    d = (double)low[0];
    e = (double)low[1];
    f = (double)low[2];
    total = a + b + c + d + e + f; // calculating total
    //EDITTED IN THE NEXT LINE//
    avg = total / ((double)NUMS * 2); // calculating average
    //EDITTED IN THE NEXT LINE//
    printf("The average temperature of the three days is: %lf", avg); // printing average.

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2018-09-19
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多