assign elements to a multi-d array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #11
Mar 5th, 2007
oh, and why are we defining elements to 100?
Last edited by boujibabe; Mar 5th, 2007 at 11:10 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #12
Mar 5th, 2007
Originally Posted by boujibabe View Post
oh, and why are we defining elements to 100?
  1. multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/

This will initialize the first and second row's first element's to the value of 0.

Since it IS necessary you use a two dimensional array, then the following might give you an idea of how it might work:

  1. #define rows 7
  2. #define columns 10
  3. #define max_total 12
  4. #define id 1
  5.  
  6. void array1( int (*multi_d)[columns])
  7. {
  8.  
  9. int x, y;
  10.  
  11. for (int i=0;i<rows;i++)
  12. for (int j=0;j<columns;j++)
  13. multi_d[i][j] = 0;
  14.  
  15. srand(time(NULL)*rand());
  16. for (int i=0;i<max_total;i++) {
  17. x= rand() % rows;
  18. y = rand() % columns;
  19. multi_d[x][y] = id;
  20. }
  21. }

I hope the above helps you out.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 11:41 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 272
Lerner Lerner is offline Offline
Posting Virtuoso

Re: assign elements to a multi-d array

 
0
  #13
Mar 5th, 2007
>>why are we defining elements

By that I assume you mean why intialize elements to some default value. Assuming that is correct, the answer is: when you declare an array none of the elements are initialized, just like when you declare a single element. Therefore, if you try to access any given element of an unitialized array to check it's value there is nothing you can check it against. So you initialize all the elements with a default value. Then when you go to enter the actual data you check to see if the value of any given element is the default value or not. If it is the default, then the space is open. It it isn't the default then the space is already occupied. If you have 12 elements assigned to random spots in the array there is always the possibility that you could end up overwriting one value with another if the same x and y values came up which would mean you have less than the desired elements actually occupied.

If you have a large array, then loops are a much easier way to initialize t he array, rather than initializing each element manually.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #14
Mar 5th, 2007
Thanks, can I also ask and why is it necessary to use the time function along with rand()? and using LamaBot's method don't I wanna check it the location is equal to zero using something like

  1.  
  2. for (int i=0;i<max_total;i++) {
  3.  
  4. x= rand() % rows;
  5.  
  6. y = rand() % columns;
  7.  
  8. if(multi_d[x][y]=={{0},{0}})//or some form of check//
  9.  
  10. multi_d[x][y] = id;
  11.  
  12. }
Last edited by boujibabe; Mar 5th, 2007 at 5:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #15
Mar 5th, 2007
Originally Posted by boujibabe View Post
Thanks, can I also ask and why is it necessary to use the time function along with rand()? ...
  1.  
  2. for (int i=0;i<max_total;i++) {
  3. x= rand() % rows;
  4. y = rand() % columns;
  5. if(multi_d[x][y]=={{0},{0}})//or some form of check//
  6. multi_d[x][y] = id;
  7. }
You don't use it with rand, you use it with srand - which is used to seed rand. If you use just use rand without seeding rand, every function call it'll produce the same values for x and y, that is why I use time(NULL). In that sense, certain applications of rand, well, aren't really random.

Originally Posted by boujibabe View Post
and using LamaBot's method don't I wanna check it the location is equal to zero...
Are you implying that you don't want to use the whole multidimensional array? Let me know.

Good luck, LamaBot
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #16
Mar 5th, 2007
Originally Posted by Lerner View Post
>>why are we defining elements

By that I assume you mean why intialize elements to some default value. Assuming that is correct, the answer is: when you declare an array none of the elements are initialized, just like when you declare a single element....
In his sample code it had a constant using MAX in its identifier. I took it as it had a maximun but not a minimum, which is why I specifically wrote it this way as to indirectly have the number of populate positions, perhaps, random as well.

In the later case here is the code modified:
  1. #define rows 7
  2. #define columns 10
  3. #define max_total 12
  4. #define id 1
  5.  
  6. void array1( int (*multi_d)[columns])
  7. {
  8.  
  9. int x, y;
  10.  
  11. for (int i=0;i<rows;i++)
  12. for (int j=0;j<columns;j++)
  13. multi_d[i][j] = 0;
  14.  
  15. srand(time(NULL)*rand());
  16. for (int i=0;i<max_total;i++) {
  17. x= rand() % rows;
  18. y = rand() % columns;
  19. if (mutli_d[x][y] != 0)
  20. multi_d[x][y] = id;
  21. }
  22. }

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 8:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #17
Mar 5th, 2007
Thanks a bundle one last thing say I had to guess at a location and i guessed at the row and column how would i check that specific row and column to see if it contained the id? Here what i came up with
  1.  
  2. printf("Guess the row location ");
  3. scanf(rowLoc)
  4. printf("Guess the column location ");
  5. scanf(colLoc);
  6.  
  7. for (int i=0;i<row;i++)
  8. for (int j=0;j<col;j++)
  9. if( multi-d[rowLoc][colLoc]==id)
  10. printf ("You found a location");
Last edited by boujibabe; Mar 5th, 2007 at 10:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #18
Mar 5th, 2007
Originally Posted by boujibabe View Post
I want to use it but i want to know how i should check if the location is free before I add to it as mentioned before this will ensure i don't overwrite anything.
As mentioned above, I thought you wanted a random number with a maximum of 12 objects in the array. My original method uses the fact that it is possible to overwite the values whose coordinates are dependent on the rand generated number, therefore making the number of objects from 0 - 12 a random number as well.

I posted the modified program that does exactly what you want, I believe. Every time it loops it'll only write to the array if the value at x and y is equal to 0, which indicates it hasn't been written too already. Let me know if you'd like a more thorough explaination

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 10:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #19
Mar 5th, 2007
Here is the modified code:

  1. #define rows 7
  2. #define columns 10
  3. #define max_total 12
  4. #define id 1
  5.  
  6. void array1( int (*multi_d)[columns])
  7. {
  8.  
  9. int x, y;
  10.  
  11. // Loop through to initialize all array elements to 0
  12. for (int i=0;i<rows;i++)
  13. for (int j=0;j<columns;j++)
  14. multi_d[i][j] = 0;
  15.  
  16. srand(time(NULL)*rand()); // Seed the random
  17. for (int i=0;i<max_total;i++) {
  18.  
  19. // Generate random coordinates
  20. x= rand() % rows;
  21. y = rand() % columns;
  22.  
  23. // Make sure you don't overwrite a value
  24. if (!mutli_d[x][y])
  25. multi_d[x][y] = id;
  26. }
  27. }


Good luck, LamaBot
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: assign elements to a multi-d array

 
0
  #20
Mar 6th, 2007
LamaBot, please watch your formatting. We are trying to show by example. Poorly formatted examples tells the poster it's OK to be sloppy...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC