-2

So I'm not sure what is going on here.

I have a function which is supposed to solve this problem, except that while typing out the elements of an array in main() I get different results:

  • 0 if I cout it in the same line with the function
  • the correct result if i write it out below

Here's the code:

#include <bits/stdc++.h>

using namespace std;

int a[100100],mx=0,bg;

int collatz(int n){
    a[1] = 1;
    for(int i=2; i<=n; i++){
        int p = 0;
        int j=i;
        while(j!=1 && j>=i){
            if(j%2==0) j/=2, p++;
            else if(j%2==1) j=3*j+1, 
        }
        a[i] = p + a[j];
        if(mx<a[i]) mx = a[i], bg = i;
    }
    return mx;
}

int main()
{
    for(int i=1; i<=10000; i++) a[i] = 0;
    cout << collatz(1000) << " " << a[1] << " " << a[2] << " " << a[3]<< "\n";
    cout << a[1] << " " << a[2] << " " << a[3] << " ";
    return 0;
}

And here's what I get:

enter image description here

The compiler I use is CodeBlocks, though I've tested it on others and it's the same issue!

Thanks

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
Sciencephile
  • 71
  • 1
  • 6
  • 1
    There are a few things [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) will taught you that an online judge or competition site will not. Among them that array indexes start at zero, and that order of evaluation when outputting using `< – Some programmer dude Jun 04 '18 at 20:52
  • 1
    I also suggest you take some time to read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) And learning that [`using namespace std` is bad](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Some programmer dude Jun 04 '18 at 20:53
  • btw the word ["inline"](https://en.cppreference.com/w/cpp/language/inline) has a particular meaning (which is not relevant here). I edited your question, because it took me a while to get what you really mean ;) – 463035818_is_not_a_number Jun 04 '18 at 21:01
  • Yep I'm aware of how array indexing works. This particular problem would have been a mess if I had started with 0 though. Thanks for the answer! – Sciencephile Jun 04 '18 at 21:03
  • 1
    CodeBlocks is an IDE, not a compiler. You can use any compiler with it; it seems you are using an old compiler – M.M Jun 04 '18 at 21:27

1 Answers1

2

On the line:

 cout << collatz(1000) << " " << a[1] << " " << a[2] << " " << a[3]<< "\n";

Before the C++17 standard, the compiler was allowed to evaluate the operands in any order, so it could look up a[1] etc. and save the result, then call collatz(1000), and then display the saved results in the right order.

To fix this, either compile in C++17 mode (use -std=c++17 compiler switch) or change the code to be:

cout << collatz(1000);
cout << " " << a[1] << " " << a[2] << " " << a[3]<< "\n";
M.M
  • 130,300
  • 18
  • 171
  • 314
  • Thanks for the actual answer instead of a passive aggressive comment! Is there a reason why the C++17 compiler isn't set as default for new projects? – Sciencephile Jun 05 '18 at 08:51
  • You would have to inquire with Code::Blocks developers about that one. – M.M Jun 05 '18 at 12:59