User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,912 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,662 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 7259 | Replies: 49 | Solved
Reply
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

assign elements to a multi-d array

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: assign elements to a multi-d array

  #2  
Mar 4th, 2007
OK. I'll bet sourceforge.net has something you can use.
Last edited by WaltP : Mar 5th, 2007 at 12:36 am.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

  #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  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

  #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.
#define rows 7
#define columns 10
#define max_total 12


void array1( int multi-d[rows][columns], int rows, int columns)
{
          multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/

         /* Populate array???*/


}
Last edited by boujibabe : Mar 4th, 2007 at 10:28 pm.
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

  #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.
 
#define rows 7
#define columns 10
#define max_total 12
 
 
void array1( int multi-d[rows][columns], int rows, int columns)
{
          multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/
 
         /* Populate array???*/
 
 
}


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 10:46 pm.
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: assign elements to a multi-d array

  #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.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

  #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.
 
#define rows 7
#define columns 10
#define max_total 12
 
 
void array1( int multi-d[rows][columns], int rows, int columns)
{
          multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/
 
         /* Populate array???*/
 
 
}


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  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: assign elements to a multi-d array

  #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...
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

  #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  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

  #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?

byard[row][col] = {{0},{0}};
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 7:47 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC