Dominion Strategy Forum

Please login or register.

Login with username, password and session length
Pages: [1]

Author Topic: Natural square root in C++ [Solved]  (Read 1462 times)

0 Members and 1 Guest are viewing this topic.

SirClemens

  • Navigator
  • ****
  • Offline Offline
  • Posts: 73
  • A very creative personal text
  • Respect: +484
    • View Profile
Natural square root in C++ [Solved]
« on: November 07, 2015, 04:35:12 am »
0

Hello f.ds,

I need to write a small program for school that calculates the nearest thing to a natural square root of a natural number. Unfortunately, no if is allowed and I have no Idea how to make it work without it. You can see my code and further restrictions below:
Code: [Select]
int main()
{
int input;
int result;
int addition;
int helper;

/* The sum of n successive odd numbers is a squared number. For example 1 + 3 = 4, 1 + 3 +5 = 9.
The purpose of this program is to show the nearest naturel square root of "input". For example if the input is 17 the result should be 4.
Restrictions: Only one while loop is allowed. No If. Only addition, !=, ==, <, >, >= and <= are allowed
*/


addition = 1; // To get odd numers, the addition has to start at 1
helper = 1; // Since the addition starts at 1, this has to start at 1 aswell
result = 1; // Same reason as above

cout << "Enter a number X >= 1: ";
cin >> input;

while(input > helper) // This ensures that the while loop ends on time
{
addition = addition + 2; //This creates the next odd number
helper = helper + addition; //This creates the next squared number
result++; // This increases the result by one, because the helper now represents the next squared number
}

cout << "The ";
cout << result;
cout << " is the natural square root of ";
cout << input << endl;
}

My problem is that if I type in the 17 for example, it gives out the 5, but it should give out the 4.
Any Ideas how to fix it?

Thank you in advance!

EDIT: I solved the problem, maybe i will fix the wrong code here later. Thank you all for your help!
« Last Edit: November 10, 2015, 11:23:45 am by SirClemens »
Logged
Too bad Dominion doesn't have Gunpowder.

Titandrake

  • Mountebank
  • *****
  • Online Online
  • Posts: 2211
  • Respect: +2858
    • View Profile
Re: Natural square root in C++
« Reply #1 on: November 07, 2015, 04:44:04 am »
0

Your code computes the largest square bigger than the given number, so it fails when 25 > 17. Not sure how to do it with all those restrictions, they feel arbitrary and not very helpful.
Logged
I have a blog! It's called Sorta Insightful. Check it out?

qmech

  • Torturer
  • *****
  • Offline Offline
  • Posts: 1918
  • Shuffle iT Username: qmech
  • What year is it?
  • Respect: +2320
    • View Profile
Re: Natural square root in C++
« Reply #2 on: November 07, 2015, 05:58:59 am »
+2

Finding the closest integer to the root is the same as working out which interval [0.5, 1.5), [1.5, 2.5), ... contains the root, so you can step through the squares of the half-integers until one of them exceeds your target.
Logged

Grujah

  • Mountebank
  • *****
  • Offline Offline
  • Posts: 2237
  • Respect: +1177
    • View Profile
Re: Natural square root in C++
« Reply #3 on: November 07, 2015, 06:11:05 am »
+2

Glancing over it,
while (input >= (helper + addition)) should work.

I'll re-think later if this doesn't work.

But whomever is teacher the class isn't doing a good job.
Why is C++ written like C, why random arbitrary restriction (like, no substractions, no ifs.. jeez).
Logged

SirClemens

  • Navigator
  • ****
  • Offline Offline
  • Posts: 73
  • A very creative personal text
  • Respect: +484
    • View Profile
Re: Natural square root in C++
« Reply #4 on: November 07, 2015, 06:27:38 am »
0


while (input >= (helper + addition)) should work.


That really helped, thank you!
But there is still a problem, according to the program the square root of 2 and 3 is 2.
Logged
Too bad Dominion doesn't have Gunpowder.
Pages: [1]
 

Page created in 0.085 seconds with 18 queries.