Comp Sci Next Perfect Squares for a Given Number

AI Thread Summary
The discussion revolves around creating sequences to find the next largest and smallest perfect squares for a given number. The user encounters an issue with their code where the second loop for finding the smallest perfect square is incorrectly structured, causing it to not function as intended. Suggestions include using a do-while loop to iterate until a perfect square less than the user’s input is found. Additionally, there is a side conversation about creating a custom square root function that allows for specified decimal points, acknowledging that C++ has a built-in sqrt function. Overall, the thread focuses on troubleshooting loop logic and exploring custom function implementations in C++.
mr.me
Messages
49
Reaction score
0
Heres my problem, step one I've solved. Step two only ever returns the original newRoot*newRoo and I don't understand how to correct itTo demonstrate event controlled loops use arithmetic to create:
1.Create a sequence to determine the next largest perfect square
2.Create another sequence to determine the next smallest perfect square for a value less than 1000

My code:

Code:
#include <iostream>
#include <math.h>
#include <iomanip>          
using std::cout;
using std::cin;
using std::endl;

main()
{
   cout << "Enter a number for which to find the next largest perfect square: ";
   int number;
   cin >> number;
   int sqRoot = 1;            
   while (sqRoot*sqRoot < number)  // sqRoot is too small
      sqRoot++;                 //  try the next number
   cout << sqRoot*sqRoot << endl;
   
    cout<< "Enter a number for which to find the next smallest perfect square: ";
    int newNumber;
    cin>>newNumber;
    int newRoot =1000;
    while (newRoot*newRoot < 1000 )
        newRoot-+newRoot;
    cout<< newRoot*newRoot<<endl;
  
}
 
Last edited:
Physics news on Phys.org
I think it is because your while loop is never actually done because 1000*1000>1000

not sure what your trying to do here
newRoot-+newRoot
 
I guess that was an obvious mistake :-p

Still I am not sure how to construct my loop for the problem in question

With the second loop I wanted to check each iteration until a got a value less-than the user entered value...

So if I entered 6 I wanted it to return a 4 because that is the first perfect square less than 6

Im not sure how to do this on paper or with the while loop.
 
hmm... I think you might to try a do while loop and increase the number by 1 each time and check again
 
x=1
y=0
while(y<input)
{
if(x*x>input)
break
y=x*x;
x++
}

cout <<y

I think that might do ityou could say "using namespace std" instead of saying all the using at the top
 
Last edited:
Thank-you, I see what I was doing wrong.

We are required to use each std:: for our work, instead of loading the whole namespace.

Thanks again :smile:
 
As an aside, i.e. not homework, what if I wanted to write a square root function, that we allow you to set the given decimal points for stuff like the square of 7 or the square of 10.?

I know C++ already has a sqrt function but could I use my code, clumsy though it may be or would I have to do something entirely different?
 

Similar threads

Replies
3
Views
1K
Replies
2
Views
3K
Replies
7
Views
2K
Replies
3
Views
2K
Replies
8
Views
1K
Replies
9
Views
3K
Replies
6
Views
3K
Back
Top