如何解决为什么我的C代码无法正确存储和/或显示用户输入的数组的值?
#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;
}
当我从数组中打印出平均值,总数或直接值时,它最终会以一串混合字符的形式打印出来,我不确定为什么。我没有任何错误或警告可以解决,所以我发现很难找出问题所在。
解决方法
修改您的代码
"+1y +5m"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。