Microsoft visual C++ Problem

how can i write a program that can read a list of integers and then print the largest and the smallest values? thanks!
here's my work.

#include <stdio.h>
#include <math.h>
 
main()
{
      float low, high, in;
      FILE *in;
      fin = fopen("infile.dat", "r");
      fout = fopen("infile.dat", "w");

      while (fscanf( fin, "%d", &in) !=EOF )
      {
            if (in < low)
                  low = in;
            if (in > high)
                  high = in;
      }
      {
            printf( "Smallest number is:\n%f\n", low);
            printf("Largest number is: \n%d\n", high);
            fclose(fin);

JeffLingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cwguesCommented:
Read a list of integers from what???
0
nietodCommented:
We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
0
JeffLingAuthor Commented:
Edited text of question.
0
nietodCommented:
Please don't edit the question once a "dialog" starts. It makes it impossible for other experts to follow the question history.  (the option to do this will hopefully be removed in the next version.)

Your code looks pretty good.  The problem I see is that you try to use "low" and "high" before you ever initialize them.  When the program starts these two varaibles will have "random" values.  The problem with this is that you test these random values against the value read in, like in

if (in < low)
    low = in;

What if the random value stored in "low" is very low, if that is the case it might be lower than all the values yue read from the file.   In that case low will remain this low random value and never get set t a value from the list.

See the problem?

continues.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
One solution is to make sure that the first time through your loop, you set "low" and "high"   Think about it.  What is the lowest valeu of 1 number?  that number.  So when the first number is read, the lowest number so far HAS to be that number.  So the first time through the loop we can always set "low" to the number read.  The same with "high".  For example,

int main()
{
   float low, high, in;
   bool FirstTime = true; // First time through the loop?

   FILE *in;
   fin = fopen("infile.dat", "r");
   fout = fopen("infile.dat", "w");

    while (fscanf( fin, "%d", &in) !=EOF )
   {
      if (FirstTime || in < low)
         low = in;
      if (FirstTime || in > high)
         high = in;
      FirstTime = false; // Next time won't be the first time.
   }
   printf( "Smallest number is:\n%f\n", low);
   printf("Largest number is: \n%d\n", high);
   fclose(fin);
   return 0; // Main should return an integer.
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.