A variable array?

This is a discussion on A variable array? within the C Programming forums, part of the General Programming Boards category; Hey everyone! (first post Woohoo!) So we're having an assignment for school where you code a Roulette game which simulates ...

1. A variable array?

Hey everyone! (first post Woohoo!)

So we're having an assignment for school where you code a Roulette game which simulates you playing and you can choose how many times you "play" to check how many times you'd win (if that makes any sense, im german :P)

Anyways, the problem is how do i store all the random generated numbers in an array while NOT doing this (it's just a snip):

Code:
```void randomzahl(void){
int i;
int zahl5[5];
int zahl10[10];
int zahl100[100];
int zahl1000[1000];
int zahl10000[10000];

if(runden == 5)
{
for(i=0; i<5; ++i)
{
zahl5[i] = rand()%37;
if(input==zahl5[i])
g++;
}
}

else if(runden == 10)
{
for(i=0;i<10;++i)
{
zahl10[i] = rand()%37;
if(input==zahl10[i])
g++;
}
}

else if(runden == 100)
{
for(i=0;i<100;++i)
{
zahl100[i] = rand()%37;
if(input==zahl100[i])
g++;
}
}

else if(runden == 1000)
{
for(i=0;i<1000;++i)
{
zahl1000[i] = rand()%37;
if(input==zahl1000[i])
g++;
}
}

else if(runden == 10000)
{
for(i=0;i<10000;++i)
{
zahl10000[i] = rand()%37;
if(input==zahl10000[i])
g++;
}
}
else
puts("Sie haben keine G\201ltige Option gew\204lt");

}```

2. Why do you need to save the results of the random function? You only use the result once and then never read it again.

Why not code like this:

Code:
```int calculateWins(int input, int attempts)
{
int i, wins;
const int wheelSpaces = 37;
for (i = 0; i < attempts; i++)
{
if (input == rand() % wheelSpaces) { wins++; }
}
return wins;
}```

3. You could simply do this:
Code:
```int i;
int zahl[10000];
for(i=0;i<runden;++i)
{
zahl[i] = rand()%37;
if(input==zahl[i])
g++;
}
}```
Also, you should remove the magic literal constants from the code and replace them with #defines.

4. Originally Posted by Cat
Why do you need to save the results of the random function? You only use the result once and then never read it again.

Why not code like this:

Code:
```int calculateWins(int input, int attempts)
{
int i, wins;
const int wheelSpaces = 37;
for (i = 0; i < attempts; i++)
{
if (input == rand() % wheelSpaces) { wins++; }
}
return wins;
}```
Hey, thanks for the fast reply
I didn't know you could do this when i created that snip :P
BUT actually I want/have to save the numbers because i want to
add betting on red/black and the collums and things so ill have to move the evaluation out of that :P

Originally Posted by King Mir
You could simply do this:
Code:
```int i;
int zahl[10000];
for(i=0;i<runden;++i)
{
zahl[i] = rand()%37;
if(input==zahl[i])
g++;
}
}```
Also, you should remove the magic literal constants from the code and replace them with #defines.
Won't the extra space do something funny if I don't fill it up? I don't want my code to produce a MissingNo or something :P