#include <iostream>
#include <sstream>
#include <vector>
#define MAX 4000000
/*
Each new term in the Fibonacci sequence is
generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the
Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.
*/
void SetVector(std::vector<unsignedlong>List);
int main()
{
std::vector<unsignedlong>List;
List.push_back(1);
List.push_back(2);
SetVector(List);
}
void SetVector(std::vector<unsignedlong>List)
{
unsignedlong sum = 0;
for(long i = 2; List[i] < MAX;i++)
{
std::cout << List[i-2] << " + " << List[i-1] << " = ";
unsignedlong temp = List[i - 2] + List[i - 1];
List.push_back(temp);
std::cout << List[i] << "\n";
if(i % 2 == 0)
{
sum += temp;
std::cout << "sum: " << sum << "\n";
}
}
std::cout << sum;
}
EDIT: even when running this program from the flash drive it still returns 0, could it be the operating system im using? i was programming this on an Xp system(worked fine). Im now at home on a windows 7 and it no longer works
The operating system your using shouldn't play any role in the code you have. It could be the age of the compilers you are using, unless you have them the same on both systems.
I would pass the vector by reference because the copy of the two vectors might be causing problems in the implementation between compilers, if they are not the same. Although the standard library should be maintained for both with a long legacy.
I expect it's the old "passing a pointer by value is kind of like a longhand way of passing what it points to by reference" interpretation. If we're lucky, it'll spark a huge hissyfit in someone and a little flamewar will start here about whether passing a pointer should be considered pass-by-value or pass-by-reference. :)
Arrays are passed by reference by default in C++ now. Since arrays are really just pointers it makes sense for the arrays to be passed by reference. But on topic, code still not working!!!
This seems a bit fishy: List[i] < MAX
If I read correctly, List[i] shouldn't exist yet at the point of comparison. List[i-1] should work.
(P.S.: How does pass-by-reference have anything to do with this? He's pass-by-value-ing a vector of two elements, but it's never used outside the function anyway, so who cares.)
[Grammatically speaking you are (of course) correct, but in easy-speak using "who cares" as a statement signifies a rhetorical question implictly saying that nobody cares.}