• 05-04-2012
abhishekcoder
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; }```
• 05-04-2012
stahta01
Quote:

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.
• 05-05-2012
abhishekcoder
Thanks for your reply. The input is 9, the expected output is 4 and the incorrect output is 3.

Quote:

Originally Posted by stahta01
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.

• 05-05-2012
Salem
Post your latest code, showing how you used a while loop.
• 05-05-2012
abhishekcoder
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
Post your latest code, showing how you used a while loop.

• 05-05-2012
ZuK
Your algorythm doesn't work if you use integer calculation.
divided by 2 gives 2.
2 squared = 4 and that is already lower then 5.
Kurt
• 05-05-2012
stahta01
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.
• 05-05-2012
stahta01
Quote:

Originally Posted by ZuK
Your algorythm doesn't work if you use integer calculation.
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.
• 05-05-2012
ZuK
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
• 05-05-2012
abhishekcoder
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
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

• 05-05-2012
ZuK
Let's put it that way. If you loop while m*m > input is the same as looping until m*m <= input :)
• 05-05-2012
abhishekcoder
:). I would like to read book that enables me to think algorithms...any suggestions?
• 05-05-2012
manasij7479
Quote:

Originally Posted by abhishekcoder
:). 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.
• 05-05-2012
abhishekcoder
Thanks :)