0

As my project states, I need to make a program which reads a file and finds 2 of the largest and the smallest values in the file. This is my program, it seems to find the largest and the smallest, but does get the the second largest and smallest. The fscanf for two small and two big does not seem to get a value from the file, and thus I get random values for the answer as well. I don't know whats wrong, if you can tell me whats wrong it would be great.

#include <stdio.h>

int
main (void)
{
    FILE *in;     /*names the file*/
    int onesmall, twosmall, compare, onebig, twobig;

    in = fopen("file2.data", "r");    /*opening the file*/
    fscanf(in,"%d", &onesmall);    /*scans and inputs first value (lowest value as of now)*/
    fscanf(in,"%d", &onebig);
    fscanf(in,"%d", &twobig);
    fscanf(in,"%d", &twosmall);
    while(!feof(in)){         /*loops till reaches to end of file*/
        fscanf(in,"%d", &compare);     /*scans and inputs first value*/
        if (compare <= onesmall){
            onesmall = compare;   /*checks if value in compare is smaller than value in onesmall if it is then it will relace it*/
        }
        if (compare >= onebig){
            onebig = compare;  /*checks if value in compare is greater than value in onebig if it is then it will relace it*/
        }
    }

    while(!feof(in)){
        fscanf(in,"%d",&compare);
        if (compare <= twosmall && compare != onesmall){   /*if value in compare is smaller than twosmall and not eaual to onsmall replace it*/
            twosmall = compare;
        }
        if(compare >= twobig && compare != onebig){   /*if value in compare is greater than twobig and not eaual to onebig replace it*/
            twobig = compare;
        }
    }
    printf("The lowest value in the file is: %d\n", onesmall);
    printf("The second lowest value in the file is: %d\n", twosmall);
    printf("The greatest value in the file is: %d\n", onebig);
    printf("The second greatest value in the file is: %d\n", twobig);  /*prints output*/

    fclose(in);
    return (0);
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 3
    http://stackoverflow.com/q/5431941/3185968 – EOF Feb 18 '16 at 22:11
  • 1
    You start by reading 4 values from file to smallest, biggest, etc, but how do you know they are so? The first might be larger than the second. Read one value, and assign to all four variables. Better yet, assign `INT_MAX` to the small vars, and `INT_MIN` to the biggest trackers. – Weather Vane Feb 18 '16 at 22:15
  • 2
    The second `while` loop won't even run. The condition that caused the first `while` loop to terminate will also terminate the second `while` loop. The task can/should be done in a single `while` loop. – user3386109 Feb 18 '16 at 22:19
  • for ease of readability and understanding by us humans: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a blank line. – user3629249 Feb 19 '16 at 04:57
  • the function: `feof()` does not work as assumed in the posted code. I.E. do not use `feof()` as the control for a loop. use the following call to `fscanf()` and check the returned value against 1. I.E. `while( 1 == fscanf(in,"%d",&compare) ) {` – user3629249 Feb 19 '16 at 05:00
  • when calling `fopen()`, always check (!=NULL) the returned value to assure the operation was successful. if not successful, call `perror(fopen failed");` then `exit( EXIT_FAILURE );` – user3629249 Feb 19 '16 at 05:03

0 Answers0