简体   繁体   中英

Program doesn't work properly

so my program is supposed to work like this:

  1. Input integers n and m (for example n = 1; m = 10)
  2. Input all the values from n to m (for example 1;2;3;4;5;6;7;8;9;10) into a dynamic array
  3. Then with bool function you have to search the dynamic array for 2 numbers who are squared and summed together and make some kind of 3rd number (for example 1*1 + 1*1 = 2; 2*2 + 2*2 = 8 etc etc)

The program for some reason either only outprints the very first combination or doesn't outprint anything at all (for example, if n = 1 and m = 10, it only outprints 1*1 + 1*1 = 2, but it should outprint 2*2 + 1*1 = 5; 2*2 + 2*2 = 8 etc etc, if n = 4 and m = 200 it doesn't even outprint anything). Where could the problem be? I've been stuck for hours and in my mind the program should work but it doesn't. Thanks a lot.

    #include <iostream>

using namespace std;

bool isSquared (int i){
   // bool result = false;

    //int div;
    //int *squares = new int [i];

    for (int j=1;j<=i;j++){
        for (int k=1;k<=i;k++){
            if (k*k + j*j == i) return true;
            else return false;
        }
    }
}


int main()
{
    int n,m,i,size;

    cin >>n;
    cin >>m;

    size = m - n;
    int *real = new int [m - n];

    for (int q=0, j=n; q<size, j<=m; q++, j++){
        real[q] = j;
    }

    for (int q=0; q<=size; q++){
        cout <<real[q] <<" | ";
    }

    cout <<endl;

    for (int i=n; i<=m; i++){
        if (isSquared(i) == true){
            for (int j=0; j<=size; j++){
                for (int k=0; k<=size; k++){
                    if (real[j]*real[j] + real[k]*real[k] == i){
                        cout <<i <<"=" <<real[j] <<"*" <<real[j] <<"+" <<real[k] <<"*" <<real[k] <<endl;
                    }
                }

            }


        }
    }

    return 0;
}

You need to spend more time breaking down the logic of your task. The way you've worded it is confusing, and I think it's contributing to the way you've written your code.

  1. Input integers n and m (for example n = 1; m = 10)
  2. Input all the values from n to m (for example 1;2;3;4;5;6;7;8;9;10) into a dynamic array
  3. Then with bool function you have to search the dynamic array for 2 numbers who are squared and summed together and make some kind of 3rd number (for example 1*1 + 1*1 = 2; 2*2 + 2*2 = 8 etc etc)

I see no (logical) problem with how you've written part 1, but for clarity's sake, I'm going to rename them to start and end .

//I'm also getting rid of superfluous variables we don't need.
int start, end;

cin >> start;
cin >> end;

Part 2 is where we start hitting problems. For starters, you've made your program needlessly complicated by involving manual management of dynamic memory. A std::vector is perfectly suited for our task (for more than one reason, as you'll soon see...)

std::vector<int> data;

We're going to load up our values into data . Based on your prompt, the inputs for start and end are an inclusive range, so we'll write it like so:

for(int i = start; i <= end; i++) {
    data.emplace_back(i);
}

In your version of the code, this is just confusing:

for (int q=0, j=n; q<size, j<=m; q++, j++){
    real[q] = j;
}

You've defined size to be m - n , which will be 9, but the prompt wants all the numbers between 1 and 10, which means there should be 10 numbers; so off the bat, you're going to have errors. The code I've suggested will work more reliably.

Finally, let's consider Step 3. It can (and should) be broken down into several steps:

  • For each number in data ( z ),
    • iterate through each pair of numbers that preceded it (which, based on your examples, may include duplicates), and for each pair ( x and y )
    • determine if x*x + y*y == z , and if so, print those variables to the user.

So let's write that code.

//Note we're using data.size(), to ensure we stay in valid memory
for(int i = 0; i < data.size(); i++) {
    int z = data[i];
    //You can change the checks for j and k to "j <= i" and "k <= i" respectively
    //if you want solutions where z*z + z*z = z, which can happen if z == 0.
    for(int j = 0; j < i; j++) {
        //We don't need to repeat identical pairs, so we're starting k at j.
        for(int k = j; k < i; k++) {
            int x = data[j];
            int y = data[k];
            if(x*x + y*y == z) {
                std::cout 
                << x << "*" << x 
                << " + "
                << y << "*" << y
                << " = "
                << z
                << std::endl;
            }
        }
    }
}

For an input of 1 10 , I get the following output:

1*1 + 1*1 = 2
1*1 + 2*2 = 5
2*2 + 2*2 = 8
1*1 + 3*3 = 10

And for an input of 4 200 I get the following output:

4*4 + 4*4 = 32
4*4 + 5*5 = 41
5*5 + 5*5 = 50
4*4 + 6*6 = 52
5*5 + 6*6 = 61
4*4 + 7*7 = 65
6*6 + 6*6 = 72
5*5 + 7*7 = 74
4*4 + 8*8 = 80
6*6 + 7*7 = 85
5*5 + 8*8 = 89
4*4 + 9*9 = 97
7*7 + 7*7 = 98
6*6 + 8*8 = 100
5*5 + 9*9 = 106
7*7 + 8*8 = 113
4*4 + 10*10 = 116
6*6 + 9*9 = 117
5*5 + 10*10 = 125
8*8 + 8*8 = 128
7*7 + 9*9 = 130
6*6 + 10*10 = 136
4*4 + 11*11 = 137
8*8 + 9*9 = 145
5*5 + 11*11 = 146
7*7 + 10*10 = 149
6*6 + 11*11 = 157
4*4 + 12*12 = 160
9*9 + 9*9 = 162
8*8 + 10*10 = 164
5*5 + 12*12 = 169
7*7 + 11*11 = 170
6*6 + 12*12 = 180
9*9 + 10*10 = 181
4*4 + 13*13 = 185
8*8 + 11*11 = 185
7*7 + 12*12 = 193
5*5 + 13*13 = 194
10*10 + 10*10 = 200

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM