Thread: Erroneous answer in algorithm problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Erroneous answer in algorithm problem

    Dear All,

    I have written a program that finds the integer whose square is closest to but greater than the integer number input as data. But it is giving me an erroneous answer. My algorithm is:

    1. Select a number which is half the input number.
    2. Square it and check whether it satisfies our condition.
    3. Subtract one and square it and check whether it satisfies our condition.
    4. Repeat 2 and 3 iteratively until a solution is obtained.

    Below is my code for the above problem.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        int n,m,input,powm,pown;
        printf("Enter a number");
        scanf("%d",&input);
        m=input/2;
        n=m-1;
        powm=pow(m,2);
        pown=pow(n,2);
        if (powm>input)
        {
              if(pown>input)
              {
              m=m-1;
              n=n-1;
              powm=pow(m,2);
              pown=pow(n,2);
              }
        }
        
        printf("The integer whose square is closest to but greater than the input number input is %d",n);
        
        system("pause");
        return 0;
    }
    Thank you for your reply.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    4. Repeat 2 and 3 iteratively until a solution is obtained.
    Step 4 implies the use of a loop; likely a do/while or while loop.

    Suggestion: State the input, the expected output, and the incorrect output from your program.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thanks for your reply. The input is 9, the expected output is 4 and the incorrect output is 3.

    Quote Originally Posted by stahta01 View Post
    Step 4 implies the use of a loop; likely a do/while or while loop.

    Suggestion: State the input, the expected output, and the incorrect output from your program.

    Tim S.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest code, showing how you used a while loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Below is my code...I am encountering an unexpected error. My code doesn't give any output after I enter 9 as input.

    Code:
    /*program finds an integer whose square is closest to but greater than the integer number input as data*/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        int n,m,input,powm,pown;
        printf("Enter a number");
        scanf("%d",&input);
        m=input/2;
        n=m-1;
        powm=pow(m,2);
        pown=pow(n,2);
        while (powm>input)
        {
              while(pown>input)
              {
              m=m-1;
              n=n-1;
              powm=pow(m,2);
              pown=pow(n,2);
              }
        }
        
        printf("The integer whose square is closest to but greater than the input number input is %d",m);
        
        system("pause");
        return 0;
    }
    Quote Originally Posted by Salem View Post
    Post your latest code, showing how you used a while loop.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your algorythm doesn't work if you use integer calculation.
    Say your input is 5
    divided by 2 gives 2.
    2 squared = 4 and that is already lower then 5.
    Kurt

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest only having a single value to test instead of two as you do with "m" and "n".

    Edit: I also suggest use "x*x" instead of "pow(x,2)".

    Tim S.
    Last edited by stahta01; 05-05-2012 at 01:09 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ZuK View Post
    Your algorythm doesn't work if you use integer calculation.
    Say your input is 5
    divided by 2 gives 2.
    2 squared = 4 and that is already lower then 5.
    Kurt
    It almost works, he needs to add 1 to all the results.
    I would say the algorithm is OK (but lacking detail); And, the implementation is flawed.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I tried it. If the startvalue is ( input / 2) +1 the algo gives correct output for input > 1.
    Only one loop is needed.
    Kurt
    EDIT: True if you loop until m*m < input and add 1 to the result it's correct even for 1
    Last edited by ZuK; 05-05-2012 at 01:29 PM.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Hi Zuk,

    Thank you for your reply. The problem is solved with your edits...except for one minor edit, instead of m*m<input, there should be m*m>input, then the algorithm gives proper results.

    Below is the modified code:

    Code:
    /*program finds an integer whose square is closest to but greater than the integer number input as data*/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        int m,input,powm;
        printf("Enter a number");
        scanf("%d",&input);
        m=(input/2)+1;
    
        powm=pow(m,2);
    
        while (powm>input)
        {
            
              m=m-1;
            
              powm=pow(m,2);
       
        }
        
        printf("The integer whose square is closest to but greater than the input number is %d",m+1);
        
        system("pause");
        return 0;
    }

    Quote Originally Posted by ZuK View Post
    I tried it. If the startvalue is ( input / 2) +1 the algo gives correct output for input > 1.
    Only one loop is needed.
    Kurt
    EDIT: True if you loop until m*m < input and add 1 to the result it's correct even for 1
    Last edited by abhishekcoder; 05-05-2012 at 01:42 PM.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Let's put it that way. If you loop while m*m > input is the same as looping until m*m <= input

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    . I would like to read book that enables me to think algorithms...any suggestions?

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by abhishekcoder View Post
    . I would like to read book that enables me to think algorithms...any suggestions?
    "Programming Pearls" come recommended.
    I read it very little at a time, in my spare time.
    If you're mathematically inclined, you can always try the Knuth Books.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. an answer raises another problem...
    By Dorky King in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 03:11 PM
  2. stupid problem. answer = 0.0
    By spydrvnm in forum C Programming
    Replies: 6
    Last Post: 09-26-2004, 10:53 AM
  3. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 09:22 AM
  4. problem with answer going to default?
    By Patrick1234 in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 09:11 AM
  5. slight problem just can't see the answer
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 08:45 AM

Tags for this Thread