Finding the minimum number of a txt file of numbers using input redirection
Author |
Message |
Marvin Odo #1 / 4
|
 Finding the minimum number of a txt file of numbers using input redirection
I have the program working but the minimum part doesn't work. It gives me zero for the minimum. Here is my code. I hope you guys could help me. Any help will be appreciated. #include <stdio.h> int main() { float avg = 0,min = 0,max = 0,t=0,sum = 0; int count=0,counter=0,num=0,cold=0; while (scanf("%f",&t)==1){ /*If statement for finding the maximum Temperature.Displays the Maximum Tempurature */ if (max<t) max =t; printf("The Maximum Temperature is: %.0f\n", max); /*If statement to count the number of temperatures over 85.Displays the number of hot days*/ if(t>=85) num++; printf("The number of hot days are:%d\n",num); /*Display the Minimum Temperature*/ if (t<min) min=t; printf("The Minimum Temperature is: %.0f\n", min); /*If statement to count Temperature between 60 an 84.Display the number of pleasant days */ if(t>=60 && t<=84) count++; printf("The number of pleasant days:%d\n",count); /*If statement to count the number of cold days. Display the number of cold days*/ if(t<=60) cold=cold+1; printf("The number of cold days:%d\n",cold); /*Add all the Temperatures together and counts the number of temperatures and Desplays the Average of list of the Temperatures*/ sum+=t; counter=counter+1; avg=sum/counter; printf("Average of the Temperatures is:%2.0f\n\n",avg); Quote: } return 0; }
|
Fri, 05 Aug 2005 01:51:06 GMT |
|
 |
bd #2 / 4
|
 Finding the minimum number of a txt file of numbers using input redirection
Quote:
> I have the program working but the minimum part doesn't work. > It gives me zero for the minimum. > Here is my code. > I hope you guys could help me. > Any help will be appreciated. > #include <stdio.h> > int main() > { > float avg = 0,min = 0,max = 0,t=0,sum = 0;
You set the minimum to zero. Fixing this is left as an exercise for the reader. Also, if all values are negative max will be 0. -- Freenet distribution (temporary): http://24.25.175.161:8891/EEl6i9sK-ZM/ job interview, n.: The excruciating process during which personnel officers separate the wheat from the chaff -- then hire the chaff.
|
Fri, 05 Aug 2005 02:55:16 GMT |
|
 |
Emmanuel Delahay #3 / 4
|
 Finding the minimum number of a txt file of numbers using input redirection
Quote: > I have the program working but the minimum part doesn't work. > It gives me zero for the minimum. > Here is my code. > I hope you guys could help me. > Any help will be appreciated. > #include <stdio.h> > int main() > { > float avg = 0,min = 0,max = 0,t=0,sum = 0;
You should reread my previous answer to your initial question:
-- -ed- emdel at noos.fr ~]=[o FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/manuals/reader.aspx "Say 'No' to the war!" http://www.votenowar.org/
|
Fri, 05 Aug 2005 04:06:12 GMT |
|
 |
CBFalcone #4 / 4
|
 Finding the minimum number of a txt file of numbers using input redirection
Quote:
> I have the program working but the minimum part doesn't work. > It gives me zero for the minimum.
Let us start by removing most of the vertical space and the silliest comments. This lets the result appear more or less on a single screen: Quote: > #include <stdio.h> > int main() > { > float avg = 0,min = 0,max = 0,t=0,sum = 0; > int count=0,counter=0,num=0,cold=0; > while (scanf("%f",&t)==1){ > if (max<t) > max =t; > printf("The Maximum Temperature is: %.0f\n", max); > if(t>=85) > num++; > printf("The number of hot days are:%d\n",num); > if (t<min) > min=t; > printf("The Minimum Temperature is: %.0f\n", min); > if(t>=60 && t<=84) > count++; > printf("The number of pleasant days:%d\n",count); > if(t<=60) > cold=cold+1; > printf("The number of cold days:%d\n",cold); > sum+=t; > counter=counter+1; > avg=sum/counter; > printf("Average of the Temperatures is:%2.0f\n\n",avg); > } > return 0; > }
as the next stage, we indent properly, and add enough spaces to make it semi-legible: Quote: > #include <stdio.h> > int main(void) /* and add this "void" for good measure */ > { > float avg = 0, min = 0, max = 0, t = 0, sum = 0; > int count = 0, counter = 0, num = 0, cold = 0; > while (scanf("%f", &t) == 1) { > if (max < t) > max = t; > printf("The Maximum Temperature is: %.0f\n", max); > if (t >= 85) > num++; > printf("The number of hot days are:%d\n", num); > if (t < min) > min = t; > printf("The Minimum Temperature is: %.0f\n", min); > if (t >= 60 && t <= 84) > count++; > printf("The number of pleasant days:%d\n", count); > if (t <= 60) > cold = cold + 1; > printf("The number of cold days:%d\n", cold); > sum += t; > counter = counter + 1; > avg = sum / counter; > printf("Average of the Temperatures is:%2.0f\n\n", avg); > } > return 0; > }
Now, bearing in mind the stated problem, we closely examine the initialization of "min = 0" and the following statement, all found by searching for use of the word "min": Quote: > if (t < min) > min = t;
and realize that to alter min we have to enter a value less than 0. Maybe we should initialize min to something different. Let's try 1000. In fact, let's also declare one initialized item per line. We also change the name "count" to "pleasant" and "num" to "hot" throughout in order to reflect their purpose. I also put back the occasional blank line to separate distinct groups of statements. Quote: > int main(void) /* and add this "void" for good measure */ > { > float avg = 0,
min = 1000, max = 0, t = 0, sum = 0; Quote: > int pleasant = 0,
counter = 0, hot = 0, cold = 0; Quote: > while (scanf("%f", &t) == 1) { > if (max < t) > max = t; > printf("The Maximum Temperature is: %.0f\n", max); > if (t >= 85) > hot++; > printf("The number of hot days are:%d\n", hot); > if (t < min) > min = t; > printf("The Minimum Temperature is: %.0f\n", min); > if (t >= 60 && t <= 84) > pleasant++; > printf("The number of pleasant days:%d\n", pleasant); > if (t <= 60) > cold = cold + 1; > printf("The number of cold days:%d\n", cold); > sum += t; > counter = counter + 1; > avg = sum / counter; > printf("Average of the Temperatures is:%2.0f\n\n", avg); > } > return 0; > }
And I suspect the result will work. IMNSHO the result is somewhat more readable. Conclusions: Keep names meaningful, and formatting consistent and accurate. Don't squander vertical space. Do not conserve horizontal blanks around operators. Neatness counts. I would also advise using more parentheses in logical statements, to make things perfectly clear, i.e. "if ((t >= 60) && (t <= 84))". If you don't get in the habit something will bite you sooner or later. Actually your initial effort was pretty good, for a tyro. You actually checked the result from scanf properly, and declared main almost correctly. --
Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address!
|
Fri, 05 Aug 2005 17:30:29 GMT |
|
|
|