954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

assign elements to a multi-d array

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.

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

OK. I'll bet sourceforge.net has something you can use.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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:

#define ELEMENTS 20
#define ARRAY 5
 
/* MULTI-DIMENSIONAL */
int multi_ID[ARRAY][ELEMENTS];
...
 
/* REGULAR ARRAY */
int multi_ID[ELEMENTS];


If you have any questions let me know.

Good luck, LamaBot

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

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???*/


}
boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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:

#define elements 100
#define ID 10
 
void array1(int *multi_d) {
  int position;
 
  /* Put initialization code here (i.e. array = all zeros) */
  srand(time(NULL)*rand());
  position = rand() % elements;
  multi_d[position] = ID;
}


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

Good luck, LamaBot

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 
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 useLamaBot'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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

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... ;)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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.

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

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

oh, and why are we defining elements to 100?

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
oh, and why are we defining elements to 100?
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:

#define rows 7
#define columns 10
#define max_total 12
#define id 1
 
void array1( int (*multi_d)[columns])
{
 
int x, y;
 
for (int i=0;i<rows;i++) 
    for (int j=0;j<columns;j++) 
     multi_d[i][j] = 0;
 
srand(time(NULL)*rand());
for (int i=0;i<max_total;i++) {
x= rand() % rows;
y = rand() % columns;
multi_d[x][y] = id;
}
}


I hope the above helps you out.

Good luck, LamaBot

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

>>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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

for (int i=0;i<max_total;i++) {

x= rand() % rows;

y = rand() % columns;

if(multi_d[x][y]=={{0},{0}})//or some form of check//

multi_d[x][y] = id;

}
boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Thanks, can I also ask and why is it necessary to use the time function along with rand()? ...

for (int i=0;i<max_total;i++) {
x= rand() % rows;
y = rand() % columns;
if(multi_d[x][y]=={{0},{0}})//or some form of check//
multi_d[x][y] = id;
}



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.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

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 
>>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:

#define rows 7
#define columns 10
#define max_total 12
#define id 1
 
void array1( int (*multi_d)[columns])
{
 
int x, y;
 
for (int i=0;i<rows;i++) 
    for (int j=0;j<columns;j++) 
     multi_d[i][j] = 0;
 
srand(time(NULL)*rand());
for (int i=0;i<max_total;i++) {
x= rand() % rows;
y = rand() % columns;
  if (mutli_d[x][y] != 0) 
     multi_d[x][y] = id;
}
}


Good luck, LamaBot

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

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

printf("Guess the row location ");
       scanf(rowLoc)
	printf("Guess the column location ");
	scanf(colLoc);
	
      for (int i=0;i<row;i++)
		  for (int j=0;j<col;j++)
	               if( multi-d[rowLoc][colLoc]==id)
		         printf ("You found a location");
boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
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

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

Here is the modified code:

#define rows 7
#define columns 10
#define max_total 12
#define id 1
 
void array1( int (*multi_d)[columns])
{
 
int x, y;
 
// Loop through to initialize all array elements to 0
for (int i=0;i<rows;i++) 
    for (int j=0;j<columns;j++) 
     multi_d[i][j] = 0;
 
srand(time(NULL)*rand());      // Seed the random
for (int i=0;i<max_total;i++) {
 
// Generate random coordinates
x= rand() % rows;      
y = rand() % columns;
 
  // Make sure you don't overwrite a value
  if (!mutli_d[x][y]) 
     multi_d[x][y] = id;
}
}



Good luck, LamaBot

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

LamaBot, please watch your formatting . We are trying to show by example. Poorly formatted examples tells the poster it's OK to be sloppy...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You