Code not working

So i made this program which displays the number of ways that the target number can be achieved by the sum of 2 numbers raised on 2 but the outputs are wrong.At the start i made a code that would check all possible sums but that took a lot of time and it would count twice some sums like 0^2+1^2 and 1^2+0^2,so i decided to try making it check less sums.You may be able to understand better while looking my code,here it is:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>

using namespace std;

vector<int> squares;

int square_pair(int target)
{
    int low = 0 , high = floor(sqrt(target)) , count = 0 , sqlow = squares[low] , sqhigh = squares[high];

    while( low <= high )
    {

        if ( pow(sqlow,2) + pow(sqhigh,2) < target )
        {
            low++;
        }
        if ( pow(sqlow,2) + pow(sqhigh,2) > target )
        {
            high--;
        }
        if ( pow(sqlow,2) + pow(sqhigh,2) == target )
        {
            count++;
        }

    }


}

int main()
{

    int t , x , *nums , max = 0 , temp;
    ifstream input("test.txt");

    input >> t;  //t is the number of numbers that have to be added to nums[]
    nums = new int[t];

    for( int i = 0 ; i < t ; i++)
    {
        input >> temp;
        nums[i] = temp;

        if( temp > max )
        {
            max = temp;
        }

    }
    input.close();

    int maxroot = (int) floor(sqrt(max));

    for ( int i = 0; i <= maxroot; i++ )
    {

        x = i * i;
        squares.push_back(x);

    }


    for ( int  i = 0; i < t; i++)
    {

        int sq = square_pair(nums[i]);
        cout << sq << endl;

    }


    return 0;

}

My guess is that there is a problem at square_pair(),what do you think?
Last edited on
Your guess is correct. There is a big problem in square_pair(). The function does not return anything. It is an int function, int function has to return an integer.
I want it to return the number of the count,so I have to add something like return count; At the end of the function?
Topic archived. No new replies allowed.