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. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Question 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. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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;
    }
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Quote Originally Posted by Cat View Post
    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

    Quote Originally Posted by King Mir View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-06-2012, 06:24 AM
  2. array to hexadecimal variable
    By TheDeveloper in forum C Programming
    Replies: 7
    Last Post: 08-13-2009, 09:37 AM
  3. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 11:02 AM
  4. variable for array ?
    By black in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2004, 06:03 AM
  5. My own variable - the array
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 03-03-2002, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21