Thread: Minimum value won't print

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    4

    Minimum value won't print

    I can't seem to get the correct minimum value to print out no matter what I do. I have tried a while loop, for loop, if statement, if-else statement, but no matter what I do I still get 0.0 as the min value.

    Input file:
    1355
    158.2 157.5
    157.0 156.5
    156.6 151.2
    151.0 148.9
    148.7 146.4
    0


    Code:
    /*Reads file and prints total, minimum, maximum and average weight lost to the screen*/
        int i, j, k;    
        float total, value, value2;
        float average, min, max;
        float startWeek, endWeek, loseWeight;
        for (i = 0; i < n; i++) {
            fscanf(ifp, "%f", &startWeek);
            fscanf(ifp, "%f", &endWeek);
            total = total + (startWeek - endWeek);
            /*computes the average weight lost*/    
            average = total/n;
        
            for(j = 0; j < n; j++) {
                value = startWeek - endWeek;
                if  (value > max) {
                    max = value;
                }
                for (k = 0; k < n; k++) {
                    value2 = startWeek - endWeek;
                    if (min > value2) {
                        min = value2;
                    }
                }
            }
        }    
        printf("Total Weight Lost: %.1f lbs\n", total);
        printf("Min lost in a week: %.1f lbs\n", min);
        printf("Max lost in a week: %.1f lbs\n", max);
        printf("Avg weight lost per week: %.1f lbs\n", average);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this supposed to be C or C++? Although fscanf and printf are from the part of the C++ standard library inherited from C, it is more conventional to use C++-style I/O in C++ programs.

    As for your program: you do not seem to have initialised or otherwise set the value of min before using it. The same goes for max.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Well, technically it looks like 0 is the smallest value in your input value.

    If you don't want 0 to be a valid value, maybe try setting your min variable to the first in the file and then doing your normal checking from there. Keep in mind, 0 will always be smaller than a value larger than 0.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Why do you need 3 loops???

    Code:
    average = total/n; //can put it outside loop, only need to calculate it once then
    if  (value > max) //what is max on first loop? need to initialize
    if (min > value2) //your mistake is pretty obvious here....

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    4
    None of those suggestions work. The only thing I can think would be the problem is that it is reading the 0 but I don't know how to fix that.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    4
    Quote Originally Posted by laserlight View Post
    Is this supposed to be C or C++? Although fscanf and printf are from the part of the C++ standard library inherited from C, it is more conventional to use C++-style I/O in C++ programs.

    As for your program: you do not seem to have initialised or otherwise set the value of min before using it. The same goes for max.
    This is C++ my professor just wants it done a certain way. I also don't want to put my entire code because I don't want it copied.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by creative_design View Post
    None of those suggestions work.
    Show your current code.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by creative_design View Post
    None of those suggestions work. The only thing I can think would be the problem is that it is reading the 0 but I don't know how to fix that.
    Well, the advice you've been given so far isn't exactly terrible. You should follow Mutant John's suggestion, and set the first values you read to be the max and min. After you do that, the code that searches for the min and max should work. It is particularly important that the min and max have a valid number to start with, because you will be comparing input numbers against it. C++ and C do not initialize variables for you. Additionally, it is absolutely possible to find the min and max in the same loop.

    In addition to that, I think the easiest thing is to change the way that you read the file. If you look at the input, 0 is a sentinel value that means the end of the file.

    Using a for is only correct in this situation if you know how many lines there are, and you programmatically exclude the line that starts with 0.

    In C and C++, we can use the fscanf function in a loop to read files with a particular format, checking the return value to make sure that all of the conversions (the fields starting with %) took place. We can tweak the idiom to handle your specific situation:
    Code:
    	n = 1;
    	total = average = min = max = 0.0;
    
    	/* Note that the first line with 2 values will be treated differently: */
    	if (fscanf(ifp, "%f %f", &startWeek, &endWeek) == 2 && startWeek != 0.0f)
    	{
    		value = startWeek - endWeek;
    		min = max = value;
    		/* The first two values are still part of the average! */
    		total += value;
    		average = total / n;
    		++n;
    	}
    	else
    	{
    		fprintf(stderr, "Error: reading stopped abruptly\n");
    	}
    
    	while (fscanf(ifp, "%f %f", &startWeek, &endWeek) == 2 && startWeek != 0.0f)
    	{
    		value = startWeek - endWeek;
    		total += value;
    		average = total / n;
    		++n;
    
    		if (value > max)
    			max = value;
    
    		if (value < min)
    			min = value;
    	}
    
    	if (startWeek != 0.0f)
    	{
    		fprintf(stderr, "Error: reading stopped abruptly after %f\n", startWeek);
    	}
    Now, zero isn't treated as normal input and in fact signals normal termination of the loop that reads the file.
    Last edited by whiteflags; 06-19-2015 at 08:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  2. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  3. find more than one minimum
    By saudi-vip in forum C Programming
    Replies: 3
    Last Post: 11-14-2008, 02:57 AM
  4. Minimum Macro
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2003, 10:16 AM
  5. Replies: 0
    Last Post: 03-28-2003, 08:20 AM

Tags for this Thread