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.
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;
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 :(
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' ;
}
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.
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?
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.
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.