user1219733
user1219733

Reputation: 23

Cant make a nested loop

This loop works as long as l is 1 and h can be any number. But i need it to work from different ranges such as l = 20 h = 40? Can anyone tell me how to do it? I would greatly appreciate it.

#include <iostream>
#include <vector>
#include <list>
#include <math.h>
#include <algorithm>


using namespace std;

int main(void)
{
    int a, b, c, i = 0;
    unsigned long l = 1; 
    unsigned long h = 25;
    int array[3];

    for ( a = l; l <= a && a <= h; ++a )
        for ( b = a; l <= b && b <= h; ++b )
            for ( c = b; l < c && c <= h; ++c )
                if ( a * a + b * b == c * c )
                {
                    array[0] = a;
                    array[1] = b;
                    array[2] = c;

                    if (array[0]+array[1]+array[2] <= h)
                        cout << array[0] << " " << array[1] << " " << array[2] <<endl;
                    else
                        break;
                }
    return 0;
}

Upvotes: 0

Views: 268

Answers (4)

Dmitriy Kachko
Dmitriy Kachko

Reputation: 2914

If I understand you right, you're trying to bruteforce Diophant's system

  • a^2 + b^2 = c^2
  • a + b + c < h

this is the solution

#include <iostream>

using namespace std;

int main() {
    const int l = 1;
    const int h = 25;
    for ( int a = l; a <= h; ++a )
        for ( int b = l; b <= h; ++b )
            for ( int c = l; c <= h; ++c )
                if ((a * a + b * b == c * c ) &&
                    (a + b + c  <= h)) 
                        cout << a << " " << b << " " << c <<endl;

    return 0;
}

the output is:

3 4 5
4 3 5
6 8 10
8 6 10

if you don't need to distinguish a and b, second cycle could be

for ( int b = a; b <= h; ++b )

so you will get this:

3 4 5
6 8 10

Upvotes: 3

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39728

There is something funny with this line, I can't tell what you are trying to do...

for ( a = l; l <= a && a <= h; ++a )

So, on the first round a=l, a++, and the condition is checked l<=a. a will be l+1, which means that l<=l+1, so you are going to exit your loop after the first time. I suspect that is not the behavior you want, but I really don't know what you do want. I could speculate that you want something like this:

for ( a = 0; l <= a && a <= h; ++a )

EDIT: From your comments, I can see what you are trying to do, and this should work better. Basically, you don't need to have the conditional for the lower value, it is the root of your problems. Also, I don't see why you bother to put the values into an array, which is written over each time, so I removed that.

for ( a = l; a <= h; ++a ) {
    for ( b = a;  b <= h; ++b ) {
        for ( c = b; c <= h; ++c ) {
            if ( a * a + b * b == c * c ) {
                if (a+b+c <= h) {
                    cout << a << " " << b << " " << c <<endl;
                }
                else {
                    break;
                }
            }
        }
    }
}

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 755010

I think one problem is in your third loop, which is slightly different from the other two:

for ( c = b; l < c && c <= h; ++c ) {

On the first pass, a == 1 and b == 1 and l == 1 so c is set to 1, and l < c evaluates to false, so the inner loop does not execute.

You really don't need to test your lower bounds (the l < c, or l <= b etc in earlier loops) because you know from the way you set them up that the condition should be true, except when you make a typo in the condition.

The canonical form for a for loop in C++ is:

 for (int i = lo; i < hi; ++i)

for a suitable type (int here), index variable (i) going from a lower bound lo up to but not including an upper bound hi. This works in C99 too, but not C89. If you need the loop index's value after the loop completes, you might declare the variable at larger scope than just the loop as shown, but you usually avoid doing that. (I used i++ in a comment because I'm an unreformed C programmer, but the pre-increment is better in C++ in general.)

Upvotes: 1

Mostafa Maklad
Mostafa Maklad

Reputation: 347

at the first loop the condition will be false from the first time because l>a

Upvotes: 0

Related Questions