944,150 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 12116
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2007
0

assign elements to a multi-d array

Expand 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Mar 4th, 2007
0

Re: assign elements to a multi-d array

OK. I'll bet sourceforge.net has something you can use.
Last edited by WaltP; Mar 5th, 2007 at 1:36 am.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Mar 4th, 2007
0

Re: assign elements to a multi-d array

Click to Expand / Collapse  Quote originally posted by boujibabe ...
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
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 4th, 2007
0

Re: assign elements to a multi-d array

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Mar 4th, 2007
0

Re: assign elements to a multi-d array

Click to Expand / Collapse  Quote originally posted by boujibabe ...
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.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 5th, 2007
0

Re: assign elements to a multi-d array

Click to Expand / Collapse  Quote originally posted by boujibabe ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Mar 5th, 2007
0

Re: assign elements to a multi-d array

Click to Expand / Collapse  Quote originally posted by boujibabe ...
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
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 5th, 2007
0

Re: assign elements to a multi-d array

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...
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Mar 5th, 2007
0

Re: assign elements to a multi-d array

Click to Expand / Collapse  Quote originally posted by WaltP ...
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.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 5th, 2007
0

Re: assign elements to a multi-d array

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}};
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Unresolved External Errors
Next Thread in C Forum Timeline: can anyone help me with writing a magic square program?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC