[英]Why is my c code not properly storing and/or displaying the values of the array inputted by the user?
#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;
}
When I print out the average, the total, or a direct value from the array, it ends up being printed as a string of mixed characters, and I am not sure why.当我打印出数组中的平均值、总数或直接值时,它最终被打印为一串混合字符,我不知道为什么。 There are no errors or warnings for me to go off of, so I am finding it difficult to figure out the issue.
没有任何错误或警告可供我解决,所以我发现很难找出问题所在。
EDITED YOUR CODE编辑您的代码
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.