HELP! storing values into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ag203
    New Member
    • Nov 2006
    • 5

    HELP! storing values into an array

    I need to store 25 different values (1, 5, 10, 25 etc. theres no pattern) in an array and then when the user picks a number q through 25 one of those values (randomly not the same one every time) needs to be chosen.

    I know that I need to Create a new array, initialize them all to a sentinel value, get a random number between 0 and 24, and move the first to that value. If it's not the sentinel, it's been set, get another random value(and check it again...), and to this 24 times, and move the 25th value to the only open spot.

    but how do I store the 25 values into an array before the program starts and before the randomizing?
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    Here is some way to initialize the array.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define  ARRAY_SIZE	   25
    
    void main()
    {
       int 
          nArray1[ARRAY_SIZE] = {1, 2, 3, 3};		
       
       // or 
    
       int 
          nArray2[ARRAY_SIZE];		
    
       nArray2[0] = 1;
       nArray2[1] = 2;
       nArray2[2] = 3;
    
       // ...
       // ...
    
       // or
    
       int
          nIndex = 0;
       int 
          nArray3[ARRAY_SIZE];		
    
       srand((unsigned)time(NULL));
       for(nIndex = 0; nIndex < ARRAY_SIZE; nIndex++)
       {
          nArray3[nIndex] = rand();
       }
    
    }
    Regards,
    M.Sivadhas.

    Comment

    • ag203
      New Member
      • Nov 2006
      • 5

      #3
      Thank you...that helpes

      Comment

      • sivadhas2006
        New Member
        • Nov 2006
        • 142

        #4
        Hi

        You are always welcome to
        Join Bytes, the community with 420,000+ software developers and IT professionals. Master the latest tools, share data expertise, and grow together.


        Regards,
        M.Sivadhas.

        Comment

        Working...