-1

Quick explanation: I want to assign random numbers to words, in a way so every letter has different number. for convenience purposes I have used arrays instead of separate one letter variables; this way I know that "abc[1]" is letter 'b' from word "abc". The first number of a word is using different range to avoid having numbers like "075"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int abc[2];
int def[2];

void setRandom()
{
    abc[0] = rand() %9+1;
    do {abc[1] = rand() %10;} while (abc[1] == abc[0]);
    do {abc[2] = rand() %10;} while (abc[2] == abc[1] || abc[2] == abc[0]);

    printf("RANDOM abc %d %d %d", abc[0], abc[1], abc[2]); //printed just fine.

    do {def[0] = rand() % 9 +1; } while (def[0] == abc[2] || def[0] == abc[1] || def[0] == abc[0]); //code seems to be stuck here
    do {def[1] = rand() %10;} while (def[1] == def[0] || def[1] == abc[2] || def[1] == abc[1] || def[1] == abc[0]);
    do {def[2] = rand() %10;} while (def[2] == def[1] || def[2] == def[0] || def[2] == abc[2] || def[2] == abc[1] || def[2] == abc[0]);

    printf("RANDOM def %d %d %d", def[0], def[1], def[2]); //THIS CODE IS NEVER REACHED. WHY?
}

int main ()
{
    setRandom();
    printf("RANDOM SET");
}

I am aware that the numbers are not truly random. I am aware that the code is messy. I am aware it's innefficient to brute-force numbers. I am aware that this way of setting numbers is bad, but I couldn't think of anything better. This code will do the job for my purposes.

The problem is that the above code gets stuck while generating number for def[0]; it will infinitely generate new random numbers, it seems to be completely ignoring 'while' part of the loop.

You may either suggest a better method of doing what I'm trying to do, or simply a solution to the problem, either will do just fine since the project isn't anything big, it's just used as puzzle solver.

  • Have you tried inspecting the values of `def[0]` vs `abc[0]`, `abc[1]`, and `abc[2]`? – Ben Hocking Jul 03 '16 at 14:56
  • You may try using a single array and doing the comparison checks using perhaps another function. – Paul Stelian Jul 03 '16 at 14:57
  • 1
    Your code is C99, not (genuine) C++11 (so tag it as C). Then, compile it with all warnings and debug info (if using [GCC](http://gcc.gnu.org/) as your compiler, than means with `gcc -Wall -g`) and improve it till you got no warnings. Then **use the debugger** (e.g. `gdb`). Your fix-my-code request is off-topic here. – Basile Starynkevitch Jul 03 '16 at 15:27

2 Answers2

2

The problem is that you've chosen the wrong size for your arrays. def[0] and abc[2] most likely point to the same location.

Specifically, you're creating arrays of size 2, and then accessing 3 elements of them. Change:

int abc[2];
int def[2];

to:

int abc[3];
int def[3];

See also this answer as to a better way to do what you're trying to do.

Community
  • 1
  • 1
Ben Hocking
  • 7,790
  • 5
  • 37
  • 52
1

Arrays in C/C++ are zero-based. creating abc[2] give you only 0 and 1 to legitimately access. Anything beyond the end of the array is undefined behaviour - in this case it's probable that abc[2] (the third, not second element) points to def[0].

Fix your code so that both declarations allocate 3 elements instead of 2.

Olipro
  • 3,489
  • 19
  • 25