assign elements to a multi-d array

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

assign elements to a multi-d array

 
0
  #1
Mar 4th, 2007
I need a function that will populate a multidimensional array with a fixed amount of elements. So that only some of the locations contain a distinguishing number for identification.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
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
  #2
Mar 4th, 2007
OK. I'll bet sourceforge.net has something you can use.
Last edited by WaltP; Mar 5th, 2007 at 1:36 am.
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  
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
  #3
Mar 4th, 2007
Originally Posted by boujibabe View Post
I need a function that will populate a multidimensional array with a fixed amount of elements. So that only some of the locations contain a distinguishing number for identification.
If the multi-dim array is of fixed length, then most of the work is done for you. Simply declare a mutli-dimensional array then assign the locations the distinguishing numbers. Although if you're using numbers as identification, you don't necessarily need a multi-dimensional array but I'll give an example of both:
  1. #define ELEMENTS 20
  2. #define ARRAY 5
  3.  
  4. /* MULTI-DIMENSIONAL */
  5. int multi_ID[ARRAY][ELEMENTS];
  6. ...
  7.  
  8. /* REGULAR ARRAY */
  9. int multi_ID[ELEMENTS];

If you have any questions let me know.

Good luck, LamaBot
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
  #4
Mar 4th, 2007
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
  1.  
  2. #define rows 7
  3. #define columns 10
  4. #define max_total 12
  5.  
  6.  
  7. void array1( int multi-d[rows][columns], int rows, int columns)
  8. {
  9. multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/
  10.  
  11. /* Populate array???*/
  12.  
  13.  
  14. }
Last edited by boujibabe; Mar 4th, 2007 at 11:28 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
  #5
Mar 4th, 2007
Originally Posted by boujibabe View Post
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
  1.  
  2. #define rows 7
  3. #define columns 10
  4. #define max_total 12
  5.  
  6.  
  7. void array1( int multi-d[rows][columns], int rows, int columns)
  8. {
  9. multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/
  10.  
  11. /* Populate array???*/
  12.  
  13.  
  14. }
Cool! Ok I understand. Thanks for clear that up:
  1. #define elements 100
  2. #define ID 10
  3.  
  4. void array1(int *multi_d) {
  5. int position;
  6.  
  7. /* Put initialization code here (i.e. array = all zeros) */
  8. srand(time(NULL)*rand());
  9. position = rand() % elements;
  10. multi_d[position] = ID;
  11. }

Make sure you include "time.h" and "stdlib.h" header files. Let me know if you have any problems.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 4th, 2007 at 11:46 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
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
  #6
Mar 5th, 2007
Originally Posted by boujibabe View Post
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
First define your array, kinda like you did:
int multi-d[rows][columns]

Then fill it with 0's with for loops.

Then loop from 1 to 12 and use LamaBot's idea with rand() to get a location in the array. Check if the location is zero. If so, load the loop value. If not, get another location.
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  
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
  #7
Mar 5th, 2007
Originally Posted by boujibabe View Post
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
  1.  
  2. #define rows 7
  3. #define columns 10
  4. #define max_total 12
  5.  
  6.  
  7. void array1( int multi-d[rows][columns], int rows, int columns)
  8. {
  9. multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/
  10.  
  11. /* Populate array???*/
  12.  
  13.  
  14. }
Is an absolute requirement that you use a multidimensional array of integers? It'd make the program a little easier if you just used a single dimension array, especially when you transverse the array. Just for closure, you don't need to pass the rows and columns as they're declared as global constants.

Good luck, LamaBot
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
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
  #8
Mar 5th, 2007
You know, LamaBot, you don't have to quote every post in their entirety if you aren't going to reference any part of it. And you can edit the quote down to only show the relevant portions you wish to comment on. That would help by not having a 200 line quote with a 3 line reply that has little to do with the quote. Just a thought...
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  
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
  #9
Mar 5th, 2007
Originally Posted by WaltP View Post
You know, LamaBot, you don't have to quote every post in their entirety if you aren't going to reference any part of it. And you can edit the quote down to only show the relevant portions you wish to comment on. That would help by not having a 200 line quote with a 3 line reply that has little to do with the quote. Just a thought...
You're right. I'll keep that in mind. Darn my lack of common sense.
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
  #10
Mar 5th, 2007
Thanks for the help, First of it hast to be a multi-d array since the rows and columns are predefined.

Second what is wrong with my attempt at initialization to zeros?

  1. byard[row][col] = {{0},{0}};
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