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:

1
2
3
4
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:

1
2
3
4
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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <math.h>

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<sq; i++)
            {
            for (int j=0; j<sq; j++)
                {
                for (int k=0;k<sq; k++)
                    {
                    total = a[i] + a[j] + a[k];
                    if (total == n)
                        {
                        cout<< sqrt(a[i])<<" / "<< sqrt(a[j]) <<" / "<< sqrt(a[k]) <<endl;
                        }
                    }
                }
            }


        delete a;

        cout<< "End (0), Continue (1)"<< endl;
        cin >>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:

1
2
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cmath>

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<sq;i++)
            {
            a[i]=pow((double)i,2);
            }

            for(int i = 0; i < sq; i++)
            {
            cout << a[i] << " - ";
            }
            cout<<'\n';//fix output

            int total;

            for (int i=0; i<sq; i++)
            {
            for (int j=0; j<sq; j++)
                {
                for (int k=0;k<sq; k++)
                    {
                    total = a[i] + a[j] + a[k];
                    if (total == n)
                        {
                        cout<< sqrt((double)a[i])<<" / "<< sqrt((double)a[j]) <<" / "<< sqrt((double)a[k]) <<endl;
                        }
                    }
                }
            }


        delete a;

        cout<< "End (0), Continue (1)"<< endl;
        cin >>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.