-
This is what I have so far but its not working
Code:
int main(void)
{
float i;
int num,sum, high, low, var;
printf("Enter test scores (-1 to terminate list)\n");
sum=0;
i=0;
low=0;
high=0;
while (1)
{
scanf("%d", &num);
if (num >= high)
{
high=num;
}
if (num <= var)
{
low=num;
}
var=num;
if (num<0)
{
break;
sum += num;
}
i++;
}
printf("Sum: %d\n",sum);
printf("High score: %d\n", high);
printf("Low score: %d\n", low);
printf("Average: %0.2f\n", sum/i);
return 0;
}
-
but now my average isnt working and the lowest number its printing is the -1. I think I need to move some things around but not sure where...
-
Oh i figured out why my avg was off. I needed to place my sum statement after the break statement. Now I have avg, high, and sum working. I just need to figure out low.
-
ok here i changed low = 100 but Im having the same problem where it is reading my exit negative number as the lowest number. Heres what I have now
Code:
int main(void)
{
float i;
int num,sum, high, low, var;
printf("Enter test scores (-1 to terminate list)\n");
sum=0;
i=0;
low=100;
high=0;
while (1)
{
scanf("%d", &num);
if (num >= high)
{
high=num;
}
if (num <= low)
{
low=num;
}
if (num<0)
{
break;
}
sum += num;
i++;
}
printf("Sum: %d\n",sum);
printf("High score: %d\n", high);
printf("Low score: %d\n", low);
printf("Average: %0.2f\n", sum/i);
return 0;
}
-
Yes! I think I finally have it working. I changed my low if statement that it must be greater than zero. This is my final draft so let me know what you guys think!
Code:
int main(void)
{
float i;
int num,sum, high, low, var;
printf("Enter test scores (-1 to terminate list)\n");
sum=0;
i=0;
low=100;
high=0;
while (1)
{
scanf("%d", &num);
if (num >= high)
{
high=num;
}
if (num <= low && num>=0)
{
low=num;
}
if (num<0)
{
break;
}
sum += num;
i++;
}
printf("Sum: %d\n",sum);
printf("High score: %d\n", high);
printf("Low score: %d\n", low);
printf("Average: %0.2f\n", sum/i);
return 0;
}
-
move the tests for high and low *below* your exit test... That is test for num < 0 FIRST, before you do anything else... now your code gets a lot simpler.
I already showed you this when calculating the sum... why should high and low be any different?