program crashes

The program crashes I think on line 42. Anyone know whats up?

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
#include <iostream>
#include <vector>
#include <cmath>
#include <math.h>
#include <limits>
#include <stdlib.h>
using namespace std;
int main()
{
    cout << "h" << endl;

    // insert code here...
    int x;
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 0;
    int total;
    vector<int>va;
    vector<int>vb;
    vector<int>vc;

for (; a < 400; a++)
{
    if (a*a + b*b == c*c)
        vc.push_back(c); vb.push_back(b); va.push_back(a);

    for (; b < 400; b++)
    {
        if (a*a + b*b == c*c)
        vc.push_back(c); vb.push_back(b); va.push_back(a);
            for (; c < 400; c++)
            {
                if (a*a + b*b == c*c)
                vc.push_back(c); vb.push_back(b); va.push_back(a);
            }
    }
}
cout << va.size() << endl;
for (int i = 0; i < 1200; i++)
{
    if (va[i] + vb[i] + vc[i] == 1000)
    {cout << va[i] * vb[i] * vc[i] << endl;
    break;}
}
    return 0;
}
use iterators to access vector elements. This prevents segmentation fault errors like the one you are most likely experiencing.

Line 43. The elements you are multiplying should be in brackets

Also the way you have set up your for-loops is time wasting seeing as you are only incrementing each value to no more than 400, you can just have one for-loop to do what all 3 of them are doing.

This brings me to the seg fault. Since your arrays only contain 400 elements, trying to access up to the 1200th element (line 40) is what is causing problems
Can you explain what you are saying please? How can I use iterators? Isn't the for loop an iterator? On line 43 do you mean I should put parentheses around the elements? How would I use just 1 for loop? And the reason I access up to 1200 elements is because that is the number of elements in the vectors.

This is Project Euler number 9 by the way.
closed account (Dy7SLyTq)
no, the for loop is a loop. an iterator (in this case) would be (well actually this is how you do it with std::list but i think its the same with std::vector)
std::vector::iterator step;

for(step = vector.begin(); step != vector.end(); step++)
//perform magic

edit:
i get this nagging feeling im missing something when declaring the iterator
Last edited on
1
2
    if (a*a + b*b == c*c)
        vc.push_back(c); vb.push_back(b); va.push_back(a);


This is equivalent to:

1
2
3
4
5
    if (a*a + b*b == c*c)
        vc.push_back(c);
 
    vb.push_back(b); 
    va.push_back(a);



Consequently the size of vc is going to be smaller than either vb or va and the vectors will not be parallel.

Why is the limit for the for loop the magic number 1200 on line 40?




The loop goes up to 1200 because that is the size of the vector I found. Nice catch with the if loop. I added brackets to them, and now nothing gets pushed back :(
closed account (Dy7SLyTq)
if is not a loop
After the first iteration of the outer loop, the inner loops will never be executed again. That's why we normally include the initialization of the variable controlling the loop in the for loop.


1
2
3
4
5
6
7
8
for ( unsigned a = 1; a < 1000 ; ++a )
    for ( unsigned b= a+1; b < 1000; ++b )
        for ( unsigned c = b+1; c < 1000; ++c )
        {
            if ( a+b+c == 1000 && (a*a + b*b == c*c) )
                  std::cout << a*b*c << '\n'  ;
        }


Thanks. How come the loops don't get executed again?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    int a = 1;
    int b = 2;
    int c = 3;

    // ...

    for (; a < 400; a++)
    {
        if (a*a + b*b == c*c)
            vc.push_back(c); vb.push_back(b); va.push_back(a);

        for (; b < 400; b++)   // After the first time this loop finishes, b is 400 
        {
            if (a*a + b*b == c*c)
                vc.push_back(c); vb.push_back(b); va.push_back(a);
            for (; c < 400; c++)    // After the first time this loop finishes, c is 400 
            {
                if (a*a + b*b == c*c)
                    vc.push_back(c); vb.push_back(b); va.push_back(a);
            }
        }
    }


So for subsequent iterations of the outer loop, the variables controlling the inner loops are already 400.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for (; a < 400; a++)
{
    if (a*a + b*b == c*c && a + b + c == 1000)
       {
          cout << a * b * c; return 0;
       }

    for (b = a + 1; b < 400; b++)
    {
        if (a*a + b*b == c*c && a + b + c == 1000)
       {
          cout << a * b * c; return 0;
       }
            for (c = b + 1; c < 400; c++)
            {
                if (a*a + b*b == c*c && a + b + c == 1000)
                {
          cout << a * b * c; return 0;
                }
            }
    }
}
This still doesn't work. I know yours is much simpler, but I just don't know why this doesn't work. Shouldn't b and c reset now after each iteration of a?
Shouldn't b and c reset now after each iteration of a?


They do. There isn't much point in checking the conditions outside of the innermost loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    for (a=1; a < 400; a++)                             //after the first iteration of this loop
    {
        if (a*a + b*b == c*c && a + b + c == 1000)      // b and c will always be 400 here.
        {                                               
            cout << a * b * c; return 0;
        }

        for (b = a + 1; b < 400; b++)                   // after the first iteration of this loop,
        {                                           
            if (a*a + b*b == c*c && a + b + c == 1000)  // c will always be 400 here. 
            {
                cout << a * b * c; return 0;
            }
            for (c = b + 1; c < 400; c++)
            {
                if (a*a + b*b == c*c && a + b + c == 1000)
                {
                    cout << a * b * c; return 0;
                }
            }
        }
    }


But, the reason this code doesn't work is because one of the numbers must be greater than 400.
Last edited on
Thanks. I don't know what I was thinking when I set that limit. And now I understand that because c is always 400 when I check it at the b loop, I only have to check the numbers during the c loop.

Thanks so much.
Topic archived. No new replies allowed.