Thread: passing random values into array

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    7

    passing random values into array

    So here is my issue,

    the range is 1 to 49, inclusive in an array.
    You must input how many integers you wish to show on the output. (lets say 12 is your input).

    The expected random output should be for example
    ------------------------
    1
    3
    47
    3
    1
    3
    47
    44
    26
    47
    49
    26

    1 appears 2 times
    2 appears 0 times
    3 appears 3 times
    .....
    26 appears 2 times
    ....
    44 appears 1 times
    ...
    47 appears 3 times
    48 appears 0 times
    49 appears 1 times
    ----------------------------------
    For a total of 12 times

    Here is what i have so far.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main(void)
    {
    	int random[49];
    	int value;
    	int i;
    	int numtogen2;
    	
    	
    printf("Enter the number of random integers to generate.\n");
    	scanf("%d",& numtogen2);
    
    for(i=0;i<numtogen2;i++)
    	{
    		value=(rand()%49)+1;
    		printf("%d\n",value);
    
    		printf("%d is shown %d times\n", value, random);
    		
    	}
    
    
    }
    My main issue is passing the output values into the array, I know im missing some code, just wondering what that would be or how it would look like.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    To get or set a number from an array you generally use brackets:
    Code:
    int random[49];
    //set some values
    random[0] = 5;
    random[1] = 0;
    random[1] += 1;
    
    //print them
    printf("random[0]=%d\n random[1]=%d\n", random[0], random[1]);
    This should get you in the right direction.
    Consider this post signed

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I believe there's a FAQ on generating random numbers.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    You need to seed the random number generator. Most common way to do this is to #include <time.h> and call

    srand(time(NULL));

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    scanf("%d",& numtogen2);
    No space after the '&'.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonoob View Post
    Code:
    scanf("%d",& numtogen2);
    No space after the '&'.
    Whitespace doesn't matter there.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Thanks, quzah. I stand corrected. I expected hideous compiler blow-ups which the OP would have noticed sooner or later since "boolean and" is missing a left argument.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Whitespace in most cases doesn't matter. Also, that & there isn't the "boolean and" in this case, it's the "address of" operator. Used to get the address of a variable (for functions expecting pointers as arguments and the like).
    Code:
    int x, *p;
    
    x = 5;
    p = &x; /* assign the address of x to the pointer p */

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Also, that & there isn't the "boolean and" in this case, it's the "address of" operator.
    I think the point was that he thought the compiler would recognize & as the boolean and instead of address of since there was a space and boolean and is usually written with the space there, as opposed to address of which is usually written without a space between the variable and operator. Of course since whitespace doesn't matter at all in C this is not the case.

    Actually I wonder if it is even possible to get a
    "boolean and" is missing a left argument.
    error. As far as I know the compiler would assume it's address of if that ever happened.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array of strings to a function.
    By dazman19 in forum C Programming
    Replies: 10
    Last Post: 09-16-2009, 06:32 PM
  2. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  3. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  4. Storing values from an array pointer
    By newatc in forum C Programming
    Replies: 21
    Last Post: 01-07-2008, 06:46 AM
  5. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 12:40 PM