### All possible 3 numbers which when squared and summed are equal to your chosen number.

....
Last edited on
The amount of square numbers less than a number (n) is no greater than the sqrt(n). So create an array of size `(int)sqrt((double)n);`. This array is to hold all of the square numbers.

It is then your job to go through every combination of this array and see if the sums equal n. If they do, print the line.

If you haven't learned about making a dynamically allocated array, perhaps just make a array that is large enough that your teacher says "okay". Perhaps use a vector instead of array. Such things depend on how much you have learned in class.

 Because 2^2 + 2^2 + 4^2 = 25
That's incorrect.
...
Last edited on
Outputting an array can be done with a simple for loop where you compare the variable in the initialization expression to the max size of the array then use that to cout each element of the array. While that sounds confusing it is simple once you see it in code:

 ``1234`` ``````for (int i = 0; i < arraysize; i++) { cout << array[i]; }``````

Obviously you have to replace "arraysize" and "array" with whatever you used to initially create your array. In the example you just gave, that would look like:

 ``1234`` ``````for (int i = 0; i < sq; i++) { cout << a[i]; }``````
Last edited on
I got this finally working , but I don't know what happened but, it now stopped responding for some reason...

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include using namespace std; int main () { int n; int* a; int ok; do { do { cout <<"Enter a natural number ... "; cin >> n; }while(n<=0); int sq=floor(sqrt(n)+1); a=new int[sq]; for(int i=0;i<=sq;i++) { a[i]=pow(i,2); } for(int i = 0; i < sq; i++) { cout << a[i] << " - "; } int total; for (int i=0; i>ok; }while(ok==1); return 0; }``````
Last edited on
line 28:
`for(int i=0;i<sq;i++)`

line 30:
`a[i]=pow(i + 1,2);`

line 49:
`cout << i + 1 <<" / "<< j + 1 <<" / "<< k + 1 <<endl;`

line 56:
`delete a;`

line 61:
`}while(ok==0);`
Also, I just remembered another for loop that was added with the new C++11 standard, so it might not be supported by your compiler. The range-based for loop. It would look like this in your code:

 ``12`` ``````for (int x : a) std::cout << x``````

Basically, int x represents the first element of the array a. It then outputs it, and will increment x to the 2nd element and so on until the end of the array is reached.
Last edited on
Don't quite understand, but Im still randomly getting "tast.exe has stopped working" error right after i enter n.

I enter 3 - prints out 1 / 1 / 1
I try to repeat the program -
I enter 45 "Not responding"

Or right after i enter the number.
Last edited on
Why are you deleting your posts?

Edit: After adding casts so it compiles for me and fixing line 28 your code runs fine for me, tested with 3, 24, 25, 45 and 99.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include #include using namespace std; int main () { double n; int* a; int ok; do { do { cout <<"Enter a natural number ... "; cin >> n; }while(n<=0); int sq=floor((double)sqrt(n)+1); a=new int[sq]; for(int i=0;i>ok; }while(ok==1); return 0; }``````
Last edited on
Thank you very much!

I deleted them because I wanted to leave only the latest update.
You can edit the posts to alert to updated code (though I dont see it nessescary) but deleting will make it harder for others to solve a similar issue.
Topic archived. No new replies allowed.