How to store Intergers in ARRAY

This is a discussion on How to store Intergers in ARRAY within the C Programming forums, part of the General Programming Boards category; How do you take the output from rand() and put it into an ARRAY? This is part of code if ...

  1. #1
    Registered User cgn38's Avatar
    Join Date
    Jan 2004
    Posts
    4

    How to store Intergers in ARRAY

    How do you take the output from rand() and put it into an ARRAY?
    This is part of code if you need more I can post it.

    Code:
    printf("Maximum digits in the numerator? \n"); //limit a to 3 digits max
    	scanf("%i",&a);
    		
    	if (a==2)
    	{
    		LIMIT=99;
    	}
    	if (a==3)
    	{
    		LIMIT=999;
    	}
    		
    srand((unsigned)time(NULL));  	//need 1 a for every c
    
    	for (j = 0; j < c; j++)
    	{
     	int i = rand() % LIMIT;
    	printf("The random number is %d\n", i );
    	}

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    with LIMIT in all caps you infer that it is a macro like defined like this
    #define LIMIT blah
    it shouldn't be in caps.
    to store it just get the random number then go
    Code:
    x=array[j];
    assuming the array is an integer array
    Code:
    int array[BUFSIZ];
    do you mean a character array?

  3. #3
    Registered User cgn38's Avatar
    Join Date
    Jan 2004
    Posts
    4
    Yes it is an Interger ARRAY
    Here is more of the code.
    I changed the LIMIT to limit, it was just a carry over from when it was #defined.

    Code:
    #define SIZE 10
    int num [SIZE];
    int dem [SIZE];
    
    
    	int main(void)
    {
    	int a;//digits in numerator
    	int b;//digits in demoninator
    	int c;//number of problems
    	int j;
    	int limit;
    
    printf("All answers must be reduced to its simplest form.\n");
    	start:
    	printf("\n");
    	printf("Maximun Number of Problems is 10 \n");
    	printf("\n");
    	printf("How many Problems would you like? \n");   
    		scanf("%i",&c);
    		if (c>10)				//limit c to 10 problems
    		goto start;		
    	
    	
    printf("Maximum digits in the numerator? \n"); 	//limit a to 3 digits max
    		scanf("%i",&a);
    		
    		if (a==2)
    		{
    			limit=99;
    		}
    		if (a==3)
    		{
    			limit=999;
    		}
    		
    srand((unsigned)time(NULL));  	//need 1 a for every c
    		for (j = 0; j < c; j++)
    		{
    	 	int i = rand() % limit;
    		printf("The random number is %d\n", i );
    		}

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An array has three requirements:

    1) an array type.
    2) an array name.
    3) an array size.

    Like so:
    Code:
    arraytype arrayname [ arraysize ];
    Thus:
    Code:
    int somedata[10];
    The array type is of type int.
    The array name is 'somedata'.
    The array size is 10.

    You can access the members of an array by referring to elements 0 through 'arraysize-1'.

    Thus in our example, these are valid:
    Code:
    somedata[0]
    somedata[9]
    And these would be invalid:
    Code:
    somedata[-1]
    somedata[10]
    somedata[400]
    To put a value into an array, by direct assignment, you'd use:
    Code:
    somedata[0] = somevalue;
    To get what's stored in the array at a given cell, you use the reverse:
    Code:
    somevariable = somedata[0];
    There's a few other items regarding arrays, but the above should suffice.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Array of Intergers
    By _Cl0wn_ in forum C Programming
    Replies: 10
    Last Post: 02-22-2003, 11:03 PM

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