-1
#include <iostream>
#include <cmath>
#include <windows.h>

using namespace std;
int n_before = 5;

int main()
{

    int numberofwritten = 3; 
    int numberofwritten_before = 0;

    __int64 data[999];
    data[0] = 0;
    data[1] = 1; 
    data[2] = 1;
    data[3] = 1;
    __int64 three = 1;

    for (__int64 i = 1; i <= n_before; i++)
    {
    
        numberofwritten++;
        
        three*=3;

        data[numberofwritten] = three;
        numberofwritten_before = numberofwritten;

        for (__int64 j = 1; j < numberofwritten_before; j++)
        {
            numberofwritten++;
            data[numberofwritten] = data[j]; 
           
        }

    }
    for (int i = 0; i <= 900; i++)
    {
        cout << i << " equals " << data[i] << endl;
    }
}

When you compile this, after 128 you get max value of int64__ (-3689348814741910324). What is wrong?

Further, when I compile this in CodeBlocks (instead of Visual Studio), I get different numbers after 128 (but still wrong).

The code is meant to make a sequence of 1113111 then 111311191113111 and so on; basically same 111 (3^1) after that 111 (thing that was before) and after that (3^2) and what was before and so on (exponent increments with every iteration).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0xMax
  • 21
  • 2
  • sorry for code not being in english, should i change it? – 0xMax Dec 05 '21 at 11:37
  • 4
    there's no rules against code being in English, but you will certainly increase your chance of getting a good answer if everyone can read and understand it. – user438383 Dec 05 '21 at 11:39
  • 2
    At least an explanation of what the code is supposed to and the expected and actual output would help (a [mre]) – Alan Birtles Dec 05 '21 at 11:44
  • 2
    You don't need `pow` at all, rather `trzy *= 3 ` on each iteration, with the initial value of `trzy` set to `1`. – Bathsheba Dec 05 '21 at 11:45
  • Please explain why you feel the need to init the first 4 array entries, but do not feel the need to init all of them. – Yunnosch Dec 05 '21 at 11:49
  • `data[numberofwritten] = pow(three, i);` -- The program is broken due to the usage of `pow` for an integer-based problem. As soon as a program introduces a floating point function like `pow` to solve integer-based problems, it's over. If you want proof of this [take a look here](https://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os). – PaulMcKenzie Dec 05 '21 at 11:53
  • Could you check if initializing array with 0s while defining solves the problem; like so `int64_t data[999] = {0};` There might be some random values left in the newly allocated memory location. – Meto Dec 05 '21 at 12:00
  • @Meto Cheked and after changing that, every number after 128 equals 0 :/ – 0xMax Dec 05 '21 at 12:03
  • @PaulMcKenzie edited the code to Bathsheba's proposition but still broken :( – 0xMax Dec 05 '21 at 12:08
  • Because I'm an old cat, please could you tell me what the first 5 terms are? My hunch is down to your overflowing a type - I'll know for sure once I know what the series is. – Bathsheba Dec 05 '21 at 12:09
  • @Yunnosch because without that the program wouldn't work – 0xMax Dec 05 '21 at 12:09
  • @Bathsheba english is not my primary language, and i don't know what exactly you mean by that, i'm also 15 and not really that deep into c++. – 0xMax Dec 05 '21 at 12:12
  • At 15 years old you're probably more experienced than most! What are the first 5 lines output by your program? – Bathsheba Dec 05 '21 at 12:14
  • ohhh sorry just realised that my loop ends at (n_before) when i change that every thing looks alright – 0xMax Dec 05 '21 at 12:15
  • Thanks for everyone for helping, and sorry for disturbing you all. – 0xMax Dec 05 '21 at 12:16
  • @0xMax please share the solution as an answer and accept it as the valid answer so that the question would be resolved. – Meto Dec 05 '21 at 12:21
  • So you tried with non-initialised array and it did not work. Then you initialised a few members but not all and now it ... still does not work.... Again, why the first four but not the rest? Or lets rephrase: What happens if you init all array entries? – Yunnosch Dec 05 '21 at 12:26
  • @Meto changing n_before to 10 (for example) – 0xMax Dec 05 '21 at 12:27

1 Answers1

1

The initial problem was due to random variables residing within the memory allocated for data[] and also the main for loop had to loop for 3 more cycles. Therefore the correct version of code would be like so;

#include <iostream>
#include <cmath>
#include <cstdint>

using namespace std;
int n_before = 8;

int main()
{

    int numberofwritten = 3; 
    int numberofwritten_before = 0;

    int64_t data[999] = {0};
    data[0] = 0;
    data[1] = 1; 
    data[2] = 1;
    data[3] = 1;
    int64_t three = 1;

    for (int64_t i = 1; i <= n_before; i++)
    {
        numberofwritten++;
        
        three*=3;

        data[numberofwritten] = three;
        numberofwritten_before = numberofwritten;

        for (int64_t j = 1; j < numberofwritten_before; j++)
        {
            numberofwritten++;
            // check if we stay within the boundaries of data[]
            if(numberofwritten > 998)
                break;
            data[numberofwritten] = data[j];
        }

    }
    for (int i = 0; i <= 998; i++)
    {
        cout << i << " equals " << data[i] << endl;
    }
}
Meto
  • 638
  • 7
  • 18
  • 2
    Small thing but these days we can write `int64_t data[999] = {};` Just looks better and is perhaps a better expression of intent. But upvoted anyway for stripping away `pow`. – Bathsheba Dec 05 '21 at 12:38
  • @Bathsheba hey, it was your idea and I liked it, it would be a shame not to use it here. (also, take my upvotes :D) – Meto Dec 05 '21 at 12:41