Author Topic: C::B goes into endless loop / other online compilers do not  (Read 3049 times)

Offline d.thomas.3411

  • Single posting newcomer
  • *
  • Posts: 3
As a new C++ programmer, CODE::BLOCKS was recommended. after install I scavenged enough info to write 'my version' of code to display a series of random numbers.
What I needed at the time was from 1 to 4 inclusive. To my surprise I actually got it to work. After doing so I decided to "get fancy" and change the code to get more numbers with multiple lists at the same time.
Well, I got up to 7 random numbers, 10 times. when I tried 8 - C::B appeared to do nothing, which after much outputs of many variable I finally found 'nothing' to be an ENDLESS LOOP.
After MANY hours of tweaking, with no improvement, I decided to try an on line compiler where it worked flawlessly. As my code was not the problem I am trying to report a possible BUG.
See attached code in text format.
Thanks, Don

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: C::B goes into endless loop / other online compilers do not
« Reply #1 on: July 12, 2022, 12:12:58 am »
Hello, and welcome in the forum.
First of all i would suggest you format your code a bit more readable. Codeblocks can help you here: select all your code text->right click->Format astyle. Identation is not forced by c/c++ but it helps a lot other and yourself when reading code.

Second, if your program compiles then the work of codeblocks is done. Everything what happens when the black console window pops up has nothing to do with codeblocks but only with your code.

I have absolute no idea what your code does, but some notes:
1) You are accessing an array with user selectable index without range check:
Code
cin>> y;

for (x=0;x<3;x++)                        //OUTPUT REPEAT IS 'x' TIMES
{
//  ==========================  OUTER LOOP START  =====================================
for (i=1;i<=y;i++)
{
num = rand()% y+1;                       // RANDOM NUMBER IS --  FROM 1 TO "y"
a[i] = num;
the variable y can be > 15 but your array is only 16 elements wide. This will lead to invalid memory access...

Code
for (i=1;i<=y;i++)
{
num = rand()% y+1;                       // RANDOM NUMBER IS --  FROM 1 TO "y"
a[i] = num;

/**** other code ****////
if (a[i] == a[j-1])
(i--) ;
here you decrement the loop index (i) that you inceremtn in the for. This looks suspicious like an endless loop like you are experiencing. I would never change the loop index backwards...

You are calling rand and then check if this value is already in the list. Remember: An array full of 1 can be also random (obligatory xkcd: https://xkcd.com/221/ )

Arrays start with index 0 not 1
Code
for (i=1;i<=y;i++)
{
num = rand()% y+1;                       // RANDOM NUMBER IS --  FROM 1 TO "y"
a[i] = num;

And last but not least, and i probably think the reason of your problem:
the array a[16] is not initialized, it contains random numbers when you run your application.
When this array now contains all numbers from 1 to 8 then your loop will never terminate, because it can not find a non dublicate number...
I woudl recommend to inizialize your array with
Code
for (i = 0; i < 16; ++i)
        a[i] = -1;
this would also explain why your code runs in an online container.  It would be possible that they initialuze the memory before they call your code...

Offline d.thomas.3411

  • Single posting newcomer
  • *
  • Posts: 3
Re: C::B goes into endless loop / other online compilers do not
« Reply #2 on: July 14, 2022, 03:16:51 am »
Hi BlurHazzard
Thanks for the speedy return
As a complete newbie to c++ programming I will try to follow your advice and clean up my coding.
The whole idea of this was to get 4 random numbers with no duplicates and I got carried away.
Thanks again, Don

Offline d.thomas.3411

  • Single posting newcomer
  • *
  • Posts: 3
Re: C::B goes into endless loop / other online compilers do not
« Reply #3 on: July 16, 2022, 04:39:39 am »
To 'BlueHazzard'
Thanks for the advice, which I hope I followed correctly.
I did as suggested and re-formatted my code - you are right - much easier to read - well for me, anyway.
I also started my loops at '0' as you suggested.
I realize that my variable 'y' can be more than 15, but as I am just trying to get it to work, I will code in some error checking later.
I have added some remarks that "MAY" be of some help to explain what I am trying to accomplish.
As far as decrementing the loop index (i) to "reverse by one and do it again" I really can't think of a correct way of a 'do over' for a generated random number that is a duplicate.
As for initializing the array, I looked up a 'how to' and did it the hard way, although your suggestion is much more 'pro' .
The "NEW" code attached does work now.
Thanks for your expert advice and time for a new comer.
Don