Thread: Possible off by 1 error

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    33

    Possible off by 1 error

    So i'm assigned to enter a list of real numbers and to enter -1 if I want to end the list. After that I must find the average of the numbers entered. Here is my code:

    Code:
    main()
    {
    	float num, sum = 0.0, i = 0.0;
    
    	printf("Enter a list of real numbers. \n");
    	printf("Enter -1 to terminate the list.\n");
    	
    	while (num != -1)
    	{
    		scanf("%f", &num);
    		sum += num;
    		++i;
    	}
    
    	printf("The average is %.2f\n", sum / i);
    
    }
    Code:
    Enter a list of real numbers.
    Enter -1 to terminate the list.
    1
    2
    3
    -1
    The average is 1.25
    As you can see this is obviously not the average of 1 + 2 + 3 = 6 / 3 = 2.00 not 1.25.

    I tried ++i as well as setting i = 1.0 instead of 0.0 but still no logical equivalence.

    Is there an explanation as to why?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    One problem is that you did not initialise num, so the first comparison of num != -1 compares garbage to -1. The problem that you are observing is that when -1 is entered, you add -1 to sum instead of ignoring it.

    A simple way to fix this is to repeat the scanf call: once before the loop, and once at the end of the loop body.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    main() should be int main().
    Now, as for the issue, ponder what happens on the last loop when you enter -1. Remember to keep track of what values sum and i are! You will soon realize what's wrong.

    Quote Originally Posted by laserlight View Post
    A simple way to fix this is to repeat the scanf call: once before the loop, and once at the end of the loop body.
    I would say an easy solution is to initialize sum to something non -1, or use a do ... while loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    Quote Originally Posted by Elysia
    I would say an easy solution is to initialize sum to something non -1, or use a do ... while loop.
    sum has already been correctly initialised to 0.0. An alternative solution to what I suggested would be to initialise num to 0 and then just shift the scanf to the end, but I think that that might be more difficult to see as correct at a glance (in both cases the initial value of i has to be modified). A do while loop can work, but again avoiding adding -1 is not so easy without uglier duplication.
    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

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah sorry, I meant num, not sum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    On second thought, with my suggestion, the initial value of i should not be modified, i.e., it would be correct to keep it initialised to 0.
    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

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Just use for loop!

    Code:
    int n;
    float num,sum;
    sum = 0;
    for(n = 0; scanf("%f",&num) == 1 && num != -1.0; n++) {
         sum += num;
    }

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    33
    figured it out...thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM