[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

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);

0
JeffLing
Asked:
JeffLing
  • 3
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now