1

I'm trying to solve project Euler questions and on the 14th question when i compile it the exe keeps on crashing. Here is my code

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int counter_array[1000000];
    int array_key=0;
    for(int x=1;x<=1000000;x++)
    {
        int y=x;
        int z=1;
        int counter=0;
        while(z==1)
        {
            if((y%2)==0 && y>1)
            {
                y=y/2;
            }
            else
            {
                if((y%2)!=0 && y>1)
                {
                    y=(3*y)+1;
                }
                else
                {
                    z=2;
                }
            }
            counter++;
            counter_array[array_key]={counter};
            array_key++;
        }
    }
    int temp=0;
    int pos=0;
    for(int i=0; i<1000000;i++)
    {
        if(counter_array[i]>temp)
        {
            temp=counter_array[i];
            pos=i;
        }
    }
    cout << pos << "----->"<<temp << endl;
}

I don't know what went wrong. Please forgive me for my mistakes I'm really new at this .

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
maniteja
  • 687
  • 2
  • 16
  • 32
  • Might be that that stack allocation's too big (eg. http://stackoverflow.com/questions/15136992/why-does-my-program-crash-when-using-fread-in-the-constructor) – Mat Dec 27 '13 at 21:28
  • Where does it crash? Provide more detail than "it crashed..." – smac89 Dec 27 '13 at 21:28
  • i use code blocks to learn c++ and when i run this program it says program.exe stopped working – maniteja Dec 27 '13 at 21:31
  • It isn't causing your program to crash, but consider making `z` a `bool z=true` instead of `int`, then your loop can be `while (z)`. You'd have to change `z=2` to `z=false`. You could give it a more fitting/descriptive name, and it saves a few bytes of memory. – Trojan Dec 27 '13 at 21:33
  • @trojansdestroy In a program that needs 1000000 ints, you're going to worry about saving three bytes? –  Dec 27 '13 at 21:40
  • his not worried about saving 3 bytes, he is worried about people using bools when they mean a bool. – RichardPlunkett Dec 27 '13 at 21:43
  • @hvd More concerned about the otherwise-unused `int` being a loop condition. The memory savings are merely a positive side effect. – Trojan Dec 27 '13 at 21:44
  • Of course, no objection to addressing readability issues. :) –  Dec 27 '13 at 21:49
  • On most systems (probably including yours) `int` won't be sufficiently large to store the sort of numbers you'll have to deal with. –  Dec 27 '13 at 22:07

1 Answers1

4

your going out of bounds by 1 on your array, but i doubt that the real issue, I think this line is the problem:

int counter_array[1000000];

Many system wont let you allocated an array on the stack like that.

ok, you have a second major problem with:

    counter_array[array_key]=   counter;
    array_key++;

given it places in the inner loop, arraykey will get incremented many times per iteration of the for loop, and will thus exceed the size of the array easily.

I am guessing here, but move the array decl into global space. Then move the above two lines to after the while loop (which will be safer, but may not do what you want I haven't checked).

RichardPlunkett
  • 2,998
  • 14
  • 14
  • im really sorry i check my code again and didnt unterstand it will int counter_array[1000001]; solve my problem? – maniteja Dec 27 '13 at 21:40
  • i understood the second part thank you sir but the first part i still cant get the mistake – maniteja Dec 27 '13 at 21:47
  • 1
    ok, my g++ on cygwin crashes when I declare an array of 1,000,000 entries in local scope. It just wont let me put that big an object on the stack. If your compiler lets you, that's fine. Still, I got around this by moving the array declaration to outside of main, making it a static storage declaration, rather than automatic storage. compilers will let you statically declare much much larger things then you can get away with locally. (PS yes you need to add one to the size as well, but that wasn't actually what I was talking about) – RichardPlunkett Dec 27 '13 at 21:52