Like Tree1Likes
  • 1 Post By ZuK

Erroneous answer in algorithm problem

This is a discussion on Erroneous answer in algorithm problem within the C Programming forums, part of the General Programming Boards category; Dear All, I have written a program that finds the integer whose square is closest to but greater than the ...

  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
    2,745
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    ZuK
    ZuK is offline
    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
    stahta01 likes this.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,745
    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 02:09 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,745
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    ZuK
    ZuK is offline
    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 02: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 02:42 PM.

  11. #11
    ZuK
    ZuK is offline
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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, 04:11 PM
  2. stupid problem. answer = 0.0
    By spydrvnm in forum C Programming
    Replies: 6
    Last Post: 09-26-2004, 11:53 AM
  3. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 10:22 AM
  4. problem with answer going to default?
    By Patrick1234 in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 10:11 AM
  5. slight problem just can't see the answer
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 09:45 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21