filling an array

This is a discussion on filling an array within the C Programming forums, part of the General Programming Boards category; I would like to fill my array with the 40 numbers I generated randomly, but it doesn't seem to be ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    12

    filling an array with 40 rand #'s

    I would like to fill my array with the 40 numbers I generated randomly, but it doesn't seem to be working right. Here is the code:
    #include <stdlib.h>
    #include <stdio.h>

    int main()
    {
    int i, r, x, ref[39];
    unsigned seed;

    printf("Enter seed: ");
    scanf("%u", &seed);
    srand(seed);

    for( i=0;i<=39;i++){
    printf("%10d", 1 + (rand() % 20));
    r= 1 + (rand() % 20);
    ref[i]=r;

    }
    printf("\nref[4] = %d", (ref[4]));
    printf("\nThis is the complete ref string\n");
    scanf("%d", &i);

    return 0;
    }

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Without actually running your code, I'll take a guess that the problem happens at that last random number.

    You say you want to hold 40 random numbers, but you only allocate enough room for 39 in the ref array. Later on, you loop through the array adding random numbers. Unfortunately, there are only 39 places to put these values (0 through 38), but on the last time through you attempt to put a value in rval[39], which does not exist.

    Change your declaration of rval to hold 40 integers instead of 39 to give yourself enough room, and modify your for statement to loop through as long as 'i < 40' so that you only hit the valid subscripts of your array.

    HTH
    Jason Deckard

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    12

    filling an array

    I tried your solution and the program acts the same. My problem lies where I try to initialize the array to my random # output. So I guess I need to know how to initialize the array to the numbers being generated randomly.

    #include <stdlib.h>
    #include <stdio.h>

    int main()
    {
    int i, r, x, ref[39];
    unsigned seed;

    printf("Enter seed: ");
    scanf("%u", &seed);
    srand(seed);

    for( i=0;i<40;i++){
    printf("%10d", 1 + (rand() % 20));

    /* THIS IS WHERE I HAVE A PROBLEM
    /* HOW DO I SET THE ARRAY TO MATCH MY RANDOM LOOP

    r = 1 + (rand() % 20);
    r=ref[i];
    ref[i]++;

    }
    printf("%10d", (1+ref[0]%20));
    printf("\nThis is the complete ref string\n");
    scanf("%d", &i);

    return 0;
    }

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    for( i=0;i<40;i++){
    r = 1 + (rand() % 20);
    printf("%10d", r));
    ref[i] = r;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    for ( i = 0; i < 40; i++ ) {
      ref[i] = 1 + ( rand() % 20 );
      printf ( "%d ", ref[i] );
    }
    -Prelude
    Last edited by Prelude; 02-28-2002 at 09:27 AM.
    My best code is written with the delete key.

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Bit of concurrency...

    > int rand;

    ?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Oops, I started with one solution and changed my mind without changing the first part of the code

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest

    Smile

    Don't forget to change:

    int i, r, x, ref[39];

    to

    int i, r, x, ref[40];

    now the <40 will work correctly and you'll do 40 instead of 39 random values

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. filling an array from a file using fscanf...
    By stodd04 in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 07:55 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Filling a 2d Array cause program to crash
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2003, 07:00 AM

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