-
The Program Is Solved
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 like
Code:
if(temp==-300)
{
break;
}
and make all the other statements else if to this statement
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();
}
-
> but miracle c gives a stupid "C:\Program Files\Miracle C\self\fusun.c: line 17:
Miracle C is the biggest piece of crap on the net - it is NOT a C compiler, it is someone trying to flog their compiler construction homework off as a compiler. It isn't, not by a long way in any shape or form a credible C compiler. Even 20+ years old Turbo-C spanks this lousy excuse for a compiler.
The other point is to not develop your programs in the same directory structure where your compiler is installed. Create a "MyCode" in your own personal area.
You yourself a huge favour and get dev-c++ (www.bloodshed.net) and see what a real compiler will do for you. What's more, it is really FREE, not that nagware P-O-S called miracle-c.
> void main()
Nope - try again. Everyone else was getting it right in this thread.
-
you realize this would only count the days that are the hottest and coolest , give their average, and say that it is the average for (number of hottest days + number of coldest days -1) days?
Quote:
coolshyam
Code:
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;// this should be initialized and used, or omitted
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;// this isn't going to be a float
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();
}
The problem is that the variable day doesn't keep track of the total number of days, only days that are hotter than the hottest or cooler than the coolest. Then you use that number to compute the average of the temperatures that were hotter than the hottest or cooler than the coolest. Also, 0 probably isn't the best initial value for 'cool' since you aren't guarranteed to have a freezing day in your sample set. Lastly, when you divide an int by an int, the result is an int, not a float. You can cast it as a float like in one of the previous posts and do it that way if you want.
So, fixing those things, you might end up with something like
Code:
#include <stdio.h>
int main(void)
{
int cool=100, counter=0, hot=0, sum=0, temp=0, x=0, y=0;
float avg=0;
while (1)
{
++counter;
printf("Enter the temperature for day %d (-300 to end): ", counter);
scanf("%d",&temp);
if(temp==-300)
{
--counter;
break;
}
else if (temp>hot)
{
hot=temp;
x=counter;
}
else if (temp<cool)
{
cool=temp;
y=counter;
}
sum+=temp;
}
if(counter)
{
avg=(float)sum/counter;
}
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",counter, avg);
}
-
Your last code entered would compile and generally work if you changed the test to ( temp <= -274.0 ). I don't think you need to worry about any valid entries below absolute zero. The conflict was between int and float variables.
On the other hand if you only want to enter int values, call temp an int - you can add it to a float total without conversion ( total += temp; ) the compiler will convert it for you from int to float and add it in, but it won't compare unlike variable types without you casting them
to be the same. You can also divide a float by an int ( for total/daycount ).
-
thanks everyone. great community =)