我是新來的,這只是我的第二個學期的C.代碼編譯好。它完成了它應有的大部分工作。由於某些原因,當temp[]
陣列中的最低值位於第一個元素中時,min
函數會返回零。實際上,變量(lo
)設置爲0.函數hiTemp
不具有相同的問題,但它幾乎是相同的代碼,只是符號更改。函數不返回期望值的一個特定情況
#include <stdio.h>
//prototype functions
float avgTemp(float deg[], int size);
float hiTemp(float deg[], int size);
float loTemp(float deg[], int size);
//main
void main(void)
{
char* day[] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh"};
float temp[7] = {0};
int i = 0;
float avg = 0;
float hi = 0;
float lo = 0;
//Do/while loop to collect the temps for the days of the week
do
{
printf("Enter the temp for the %s day:", day[i]);
scanf("%f", &temp[i]);
i++;
}
while(i <= 6);
//math and print for the average temp
avg = avgTemp(temp, 7);
hi = hiTemp(temp, 7);
lo = loTemp(temp, 7);
printf("The high temp was %.2f\n", hi);
printf("The low temp was %.2f\n", lo);
printf("The average temp is %.2f\n", avg);
if(hi > 113)
puts("The high temperature is out of range");
if(lo < -4)
puts("The low temperature is out of range");
}
//function to find the average
float avgTemp(float deg[], int size)
{
float add = 0;
for(size; size >= 0; size--)
add = add + deg[size];
return add/7;
}
//function to find the hi temp
float hiTemp(float deg[], int size)
{
float hi = 0;
int i = 1;
for(i = 1; i <= size; i++)
{
if(deg[0] <= deg[i])
deg[0] = deg[i];
}
hi = deg[0];
return hi;
}
//function to find the lo temp
float loTemp(float deg[], int size)
{
float lo = 0;
for(size; size > 0; size--)
{
if(deg[size] <= deg[7])
deg[7] = deg[size];
}
lo = deg[7];
printf("debug lo:%f\n",lo);
return lo;
}
爲什麼'loTemp'中的硬編碼幻數'7'? – alk 2014-08-29 11:32:17
@Downvoter:如果沒有明確的理由,請不要低估。不要阻止那些只是在學習stackoverflow和C時,他們有一個有效的問題。 – fritzone 2014-08-29 11:35:52
您可能會喜歡使用調試器來調試代碼:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – alk 2014-08-29 11:37:19