Thread: Multidimensional Arrays

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    Multidimensional Arrays

    I have an algorithm regarding the motion of a particle. It can only hop one step to the right or remain still. I want to construct a square board(N*N dimensional array) that shows the correlation between time and distance.

    so it'll look something like

    Code:
    D\T 0  1  2  3  4  5
      0      a 
      1      x  a
      2      x  x  a
      3      x  x  x  a
      4      x  x  x  x  a
      5      x  x  x  x  x  a
    *this table shows that if the particle a hops every time, at time 5 it would be at Distance = 5*

    Code:
    D\T 0  1  2  3  4  5
      0      a  a
      1      x  x  a
      2      x  x  x  a
      3      x  x  x  x  a
      4      x  x  x  x  x a
      5      x  x  x  x  x  x
    *this table shows that if the particle a hops every time except the firts time, at time 5 it would be at Distance = 4*

    Assume the particle starts at zero, if it hops one step after 1 second, then a would move to [1][1], because that represents at T = 1, the particle is at D = 1. else, it would move to [1][0], because it did not hop at T = 1, and thus particle is at D = 0.

    I have not had much experience with multidimensional arrays. I understand you have to allocate the memory for it first.

    Code:
       c = (double *) malloc (N * N * sizeof *c); /* to allocated a flexible 
                                                     multidimensional array so N can 
                                                     vary upon command line input */
    I'm having trouble with placing the particle in the corresponding [][]. Using two for-loops, I think I have set the each tile of the square into an empty state:

    Code:
      for(i = 0; i < t; i++)
        {
          for(j = 0; j < t; j++)
    	{
    	  c[i*t+j] = 0.0;
    	}
        }
    I'm not sure how I would go about inputing values into the square. How would the for-loop be constructed?

    *edit* Also, how are multi-dimensional arrays printed? right now I have no way to check my code to see if it is what I want it to do becuase idk how to print it :P.
    Last edited by Dave_Sinkula; 10-05-2006 at 05:47 PM. Reason: Added [code][/code] tags to preserve whitespace, and wrapped long comment.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    To print.

    Code:
      for(i = 0; i < t; i++)
        {
          for(j = 0; j < t; j++)
    	{
    	  printf("%f ", c[i*t+j]);
    	}
             printf("\n");
        }
    To set a value in it, you input a row and a column and a value

    Code:
    scanf("%d %d %f", &row, &col, &val);
    Or however, and then

    Code:
    c[col* t + row] = val
    t being equal to N. You should figure out how to make the particle hop around, and modify the for loop to print a table like you made in your post.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
      for(i = 0; i < t; i++)
        {
          for(j = 0; j < t; j++)
    	{
    	  (double) c[i * t + j] = 0.0; /*line 47*/
          	  printf("%f", c[i*t+j]); /*line 48*/
    	}
          printf("\n");
        }
    how come it says:
    hop.c:47: error: array subscript is not an integer
    hop.c:48: error: array subscript is not an integer

    *c is defined as a double

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What are i, j, and t defined as? They should probably be int's (doesn't make sense to have fractional indices or array dimensions, so just int).

    Code:
    (double) c[i * t + j] = 0.0;
    No need for the cast there.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote Originally Posted by Tonto
    What are i, j, and t defined as? They should probably be int's (doesn't make sense to have fractional indices or array dimensions, so just int).

    Code:
    (double) c[i * t + j] = 0.0;
    No need for the cast there.
    I just decided to switch t to int so this part can be fixed later.

    My problem is, I don't want to manually enter the values, but to use an algorithm to place them into corresponding boxes on the table. Basically I'm having my algorithm returning two values, 1 or 0. 1 means hop, 0 means stay at the same place. To my understanding...
    Code:
    scanf("%d %d %d", &t, &t, &val);
    involves entering value by hand?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note, vutek0328, that even if your code compiled it probably wouldn't do what you want, because you don't print a space after each floating point number. Instead of printing "12 13.6 3.77" it would look something like "1213.63.77". Tonto put a space in for a reason.

    BTW, if you just want to intialize an array to all zeros, use memset or better yet
    Code:
    double array[SIZE] = {0.0};
    I have not had much experience with multidimensional arrays. I understand you have to allocate the memory for it first.
    No, you don't have to, although you can; this works as well
    Code:
    int array[X][Y];
    To access an element of a multidimentional array you usually use two array indecies:
    Code:
    printf("%i\n", array[x][y]);
    Unless you have it set up like a 2D array like the video memory for mode 13h, which you apparently do. If you want a "true" multidimentional array, you'd need to use a pointer to a pointer.

    There's a tutorial on multidimentional arrays (I think) here somewhere: http://cprogramming.com/tutorial.html

    BTW, comments start with /* and end with */, not \* and *\.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yes. But basically the point is you need to figure out what value to set (depending on these hops or something), to what row, col, and then use this to do it:

    Code:
    c[col * t + row] = val
    From what I see, you could be like

    Code:
    while col < t 
    {
        if hop then hop row 
        c[col * t + row] = val;
    }

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote Originally Posted by dwks

    To access an element of a multidimentional array you usually use two array indecies:
    Code:
    printf("%i\n", array[x][y]);
    Unless you have it set up like a 2D array like the video memory for mode 13h, which you apparently do. If you want a "true" multidimentional array, you'd need to use a pointer to a pointer.
    Fixed the printing error and the comment error

    So now I have an initialized table of d * t all with values 0. Now I will run an if loop through it, if x(randomly generated number) is > Constant, then the particle hops (in order to indicate that on the table, I need to input a value in the correct corresponding element...this is where i'm confused on how I can do that). If particle hops, value = a, if particle remains still, value = b.

    so for example, after 1 second, the particle hops one, and after another second, particle remains still (a represents the location of the particle)

    Code:
    D\T   0   1   2   3 
    0     a   
         
    1     x   a   a
    
    2     x    x   x
    
    3     x    x   x
    edit: OH, if it hops, then it's [i+1][j+1] = a, if it does not hop, then it's [i][j+1] = a.
    Last edited by vutek0328; 10-05-2006 at 05:21 PM.

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I can't see why it would 'remain' if you overwrite the row, col coordinate you are setting the value with (unless you did not). May we see some code.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
     for(i = 0; i < to; i++)
        {
          for(j = 0; j < to; j++)
    	{
    	  c[i * to + j] = 0;
    	  double u = gsl_ran_flat (r, 0.0, 1.0);
    	  a[i] = u;
    	  if(a[i] > exp(-t/tao))
    	    {
    	      c[(i+1)*to + (j+1)] = 1;
    	    }
    	  else
    	    {
    	      c[(i*to) + (j+1)] = 1;
    	    }
          	  printf("%f ", c[i*to+j]);
    	}
          printf("\n");
        }
    This is what I have so far, but it doesn't give me any results, but at least it is giving me a better mental understanding of what it should be...since if it hops, then it's [i+1][j+1] = a, if it does not hop, then it's [i][j+1] = a.

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I don't know if that's how it should be. I was thinking of it more like pos vs time chart, tell me if that's incorrect for the program though. But if it is, time would be the columns.

    Code:
    time = 0, distance = 0;
    
    while(time < t)
    {
         	c[time * t + distance] = someval; // like 'a'
    
    	double u = gsl_ran_flat (r, 0.0, 1.0);
    	a[i] = u;
    
    	if(a[i] > exp(-t/tao))
    	{
    		// hop
    		distance++;
    	}
    	time++; // step forward in time, distance will be plotted one over on the next time.
    }
    So in a grid like

    Code:
    D\T   0   1   2   3 
    0     a                   // starts at 0, 0
         
    1     x   a   a           // hopped (t = 1, d = 1), then didn't hop (t = 2, d = 1)
    
    2     x   x   x   a       // hopped (t = 3, d = 2)
    
    3     x   x   x

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    The code you proposed seems to be both correct and efficient, however, I still encounter the error that:

    Code:
    array[t * to + dis] = 1.0;
    error: array subscript is not an integer.

    t, to, dis and *array are all defined as doubles...thus clearly the subscripts are not integers, nor do I want the elements to be integers.


  13. #13
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, instead of making a grid and setting grid to somevalue at position, maybe you should have like an array of times filled in with the distance the object is at. Well, okay, maybe that goes against the assignment. Can you explain to me what t, to, and dis are?

    t = array dimension?
    to = time?
    dis = distance?

    All these things should be whole numbers in the context of your problem dealio. It doesn't make sense to have fractional indexes.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want to use doubles for indecies, you could round the double to the nearest integer:
    Code:
    array[ (int) (doubleindex + .5) ] = something;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote Originally Posted by Tonto
    Well, instead of making a grid and setting grid to somevalue at position, maybe you should have like an array of times filled in with the distance the object is at. Well, okay, maybe that goes against the assignment. Can you explain to me what t, to, and dis are?

    t = array dimension?
    to = time?
    dis = distance?

    All these things should be whole numbers in the context of your problem dealio. It doesn't make sense to have fractional indexes.
    you are correct with the above statements, but for variation purposes, all variables are supposed to be defined as doubles :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM