Я новичок в этом, и это только мой второй семестр 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;
}
Почему закодированное магическое число '7' в' loTemp'? – alk
@Downvoter: Не уменьшайте без указания причины. Не препятствуйте людям, которые просто изучают stackoverflow и C, когда у них есть правильный вопрос. – fritzone
Возможно, вам захочется отладить ваш код, используя отладчик: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – alk