Thread: How to create a two dimensinal array of random integers?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20

    How to create a two dimensinal array of random integers?

    Hello, My program is to generate a two dimensional array of random integers between 0 and 40, each row of this array is X, Y, and Z of a plane. I dont have a clue how to go about this program. This is what i have so far. Can someone let me know if i'm doing this correctly? If not, how should I go about doing it?

    Code:
     
    double array[][][5]; 
    
    int main(void)
    {
      double x,y,z;
      double array[2][4][5]; 
        
        srand(time(NULL));
        for(x=0;x<40;x++)
        {
          for(y=0;y<40;y++)
          {
            for(z=0;z<40;z++)
            {
              amarry[x][y][z] = (x+1)+(y+1)+(z+1);
            }
          }
        }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you trying to make the array have a random number of elements for the dimensions, or are you trying to populate the elements with random values? If the latter, there is a FAQ on generating random numbers (hint: you aren't calling rand()).

    If the former, then you should search the forum for something like 'dynamic multi dimensional arrays' or something similar.


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

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20
    Thank i will look in the forum. I'm am trying to populate the elements with random values.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by timbean View Post
    Hello, My program is to generate a two dimensional array of random integers between 0 and 40, each row of this array is X, Y, and Z of a plane. I dont have a clue how to go about this program. This is what i have so far. Can someone let me know if i'm doing this correctly? If not, how should I go about doing it?


    Code:
     
    #include <time.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    double array[][][5];  <-- remove this 
    
    int main(void)
    {
      double x,y,z;         <-- counters should be type int 
      double array[2][4][5]; 
        
        srand(time(NULL));
        for(x=0;x<40;x++)  <-- This should be x <2 
        {
          for(y=0;y<40;y++)  <-- this should be y < 4 
          {
            for(z=0;z<40;z++)   <-- this should be z < 5 
            {
              array[x][y][z] =  <--- call rand() here         
            }
          }
        }
    When writing C code always be aware of the sizes of your arrays. If you overrun the boundaries bad --undefined-- stuff is very likely to happen.
    Last edited by CommonTater; 06-18-2011 at 09:40 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20
    No luck! I through the forum and nothing and found nothing.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    scroll up...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20
    Did not work.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
     double amarry[][][5]; 
    
    int main(void)
    {
      double x,y,z;
      double amarry[2][4][5]; 
        
        
        for(x=0;x<40;x++)
        {
          for(y=0;y<40;y++)
          {
            for(z=0;z<40;z++)
            {
              amarry[x][y][z] =rand()%40+1;
            }
          }
        }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Now go back and take a look at post #4 in this thread... and follow the advice given.

    Your code as it is now has horrific array bounds overruns that will cause it to crash.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Location
    NewYork
    Posts
    20
    thank for your help

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    For a 2D array, there are too many dimensions in that 'code'.

    It's clear that you still don't know what each line of your program does.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a No-repeat random array
    By cosio55 in forum C Programming
    Replies: 8
    Last Post: 10-28-2010, 02:11 AM
  2. Making a random array of integers
    By Vidak in forum C# Programming
    Replies: 2
    Last Post: 11-09-2007, 06:00 AM
  3. Create random array
    By Hunter_wow in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 05:29 AM
  4. arrays of random integers & time
    By krazykid18 in forum C++ Programming
    Replies: 22
    Last Post: 12-12-2004, 03:12 PM
  5. random integers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 07:55 PM