By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,660 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,660 IT Pros & Developers. It's quick & easy.

HELP! storing values into an array

P: 5
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?
Nov 15 '06 #1
Share this Question
Share on Google+
3 Replies


sivadhas2006
100+
P: 142
Hi,

Here is some way to initialize the array.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define  ARRAY_SIZE       25
  6.  
  7. void main()
  8. {
  9.    int 
  10.       nArray1[ARRAY_SIZE] = {1, 2, 3, 3};        
  11.  
  12.    // or 
  13.  
  14.    int 
  15.       nArray2[ARRAY_SIZE];        
  16.  
  17.    nArray2[0] = 1;
  18.    nArray2[1] = 2;
  19.    nArray2[2] = 3;
  20.  
  21.    // ...
  22.    // ...
  23.  
  24.    // or
  25.  
  26.    int
  27.       nIndex = 0;
  28.    int 
  29.       nArray3[ARRAY_SIZE];        
  30.  
  31.    srand((unsigned)time(NULL));
  32.    for(nIndex = 0; nIndex < ARRAY_SIZE; nIndex++)
  33.    {
  34.       nArray3[nIndex] = rand();
  35.    }
  36.  
  37. }
  38.  
  39.  
Regards,
M.Sivadhas.
Nov 15 '06 #2

P: 5
Thank you...that helpes
Nov 15 '06 #3

sivadhas2006
100+
P: 142
Hi

You are always welcome to
http://www.thescripts.com

Regards,
M.Sivadhas.
Nov 16 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.