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.

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

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

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


}

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

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.

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

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

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}};

oh, and why are we defining elements to 100?

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

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

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;

}

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

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

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

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

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

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

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

Your examples are good, we just need to model good practices.

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

Oh thanks i understand thats why I edited the post but i was asking something else but I guess you had already seen the first post.I'll repost so you'll know what I'm talking about.
If 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");

You've basically got it. Did you try it? What happened when you did?

I got a more than a little errors most of them say something like

error C2143: syntax error : missing ';' before 'type'

I wonder if this is because The function is located in a separate C file

I got a more than a little errors most of them say something like

error C2143: syntax error : missing ';' before 'type'

I wonder if this is because The function is located in a separate C file

Yeah that is probably the problem. An easier way to confirm this is just declare i and j as integers before executing the for loop. Sometimes when I'm writing a reply post, another post gets posted before I actually post mine. Please don't interpret my post sequence as incompetency. :) Also one last suggestion, if you're using 1 for you ID then you really don't have to write the "== 1' part.

Good luck, LamaBot

I got a more than a little errors most of them say something like

error C2143: syntax error : missing ';' before 'type'

I wonder if this is because The function is located in a separate C file

No, it's because your syntax is wrong.

Steps to correct:
#1) Look at the error
#2) Figure out what it's telling you
#3) Look at the line number it mentions
#4) Look at the actual line in the code, and a couple lines above it
#5) Figure out what your code says based on the error

And if you can't quite see the problem, post the exact error here and about 5 lines above and below the line in error -- with a comment on the line that's wrong at least mentioning the line number, or "error here".

The reason I mention exact is if you don't understand the error, you can't very well paraphrase the error

Walt, when I'm using for loops, my C++ compiler allows me to declare and initialize a variable within a for loop.

In C++, this is allowed:

int main(void)
{
    int i;
    i = 23;
    int j;
    j = i * 3;
    for (int k=1; k < 10; k+=2)
    {
        int newval;
 . . .

In C the above is not and should be changed to

int main(void)
{
    int i;
    int j;
    int k;

    i = 23;
    j = i * 3;
    for (k=1; k < 10; k+=2)
    {
        int newval;
 . . .

The variable declarations must be made before any executable code. This includes function calls.

ok I have no idea what is going on so I'll post the entire fuction NB. the function is supposed to return the user score based on attempts and found locations

float game(const int yard[][col], int row_size, int col_size,int max_location)
{
	int attempts;
	int rowLoc;
	int colLoc;
	int num_found;
	int sensitivity;
	char quit={0};

	do{	
		while(num_found<=max_location)){

	printf("Guess the row location ");
    scanf("%d",rowLoc);
	printf("Guess the column location");
	scanf("%d",colLoc);
	
      for (int i=0;i<row_size;i++)/*Error occurs on tis line*/
		  for (int j=0;j<col_size;j++)
	  if( yard[rowLoc][colLoc]==id)
	  {
		  printf ("Congratulations ,You found a location!");
		  num_found++;
		  continue;
	  }
	  else
	  { 
          printf("Sorry  Try Again");
		  continue;
	  }
	      attempts++;
	}/*endwhile*/

          printf("\n\n");
		  printf("Enter Q to quit, C to continue >");
          scanf(" %c", &quit);
	
	
}while ((quit != 'Q') && (quit != 'q'));

  sensitivity=(num_found/attempts)*100;
  return sensitivity;
}

: error C2143: syntax error : missing ';' before 'type'
: error C2143: syntax error : missing ';' before 'type'
: error C2143: syntax error : missing ')' before 'type'
: error C2143: syntax error : missing ';' before 'type'
: error C2065: 'i' : undeclared identifier
: warning C4552: '<' : operator has no effect; expected operator with side-effect
: error C2059: syntax error : ')'

hello

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.