well obviously the code took -300 also as the temperature
because, it comes under the case
so, it is better if you use a seperate statement likeCode:if(temp<cool)
and make all the other statements else if to this statementCode:if(temp==-300) { break; }
also put the day++ inside the else if blocks
also, while initializing the variables,
you have to put day=0.othrwise an error flags in claculating the error
for your convinience, sorry to say, i am displaying my code
Code://done in TurboC //please see the logic #include <stdio.h> #include<conio.h> void main() { int day=0; int cool=0,hot=0,sum=0; int counter,temp,x,y; float avg; clrscr(); while (temp!=-300) { printf("Enter the temperature for day %d (-300 to end): "); scanf("%d",&temp); ++counter; if(temp==-300) { break; } else if (temp>hot) { day++; hot=temp; x=day; sum+=temp; } else if (temp<cool) { day++; cool=temp; y=day; sum+=temp; } } avg=sum/day; printf("The hottest day is day %d and the temperature is %d degrees Celcius.\n",x,hot); printf("The coolest day is day %d and the temperature is %d degrees Celcius.\n",y,cool); printf("The average temperature over %d day(s) is %f degrees Celcius.\n",day-1,avg); getch(); }



LinkBack URL
About LinkBacks


