> for (int i=0;i<row_size;i++)
This is C, not C++

You must declare
int i;
at the start of the block, and then
for (i=0;i<row_size;i++)

> attempts++;
Since the preceding if and else both contain continue, how is this line ever executed?

Using a few more braces would help as well.

Member Avatar for rkamin1

Don't get discouraged now, it seems you're chugging right along

Your errors look like they are the result of missing braces "{ }" For each for loop that is more than one line, you will need to have the following formatting:

for (int i = 0; i < rows; i++) {
     //do something
     //do something else
 }

Another point, in your guessing function, it is not entirely necessary for you to transverse your array if you are taking a row and column location from the user. You can simply just check that location for the correct value. By including those two for loops, you are just checking each element instead of the one that the user input. I'm sure this is probably adding some logic errors to your program.

Let me know if this helps at all.

-Robbie

nope, I declared i and j before as suggested and now the error sits on the initializer line. I was wondering how I could search just the user provided location instead of going through the whole array can that be done?

Member Avatar for rkamin1

I was wondering how I could search just the user provided location instead of going through the whole array can that be done?

That's what I was trying to get at with my last post. You don't even need the for loops at all, but just reference the location using the indices for row and column you collect from the user. Just use your IF statement.

> scanf("%d",colLoc);
Here's something else wrong - you forgot the &
as in
scanf("%d",&colLoc);

Post your latest code and latest error messages.

Yeah, so its running now after i made some changes.

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

	do{	
		while(num_found<=max_location){

	printf("Guess the row location of a Bone");
    scanf("%d",&rowLoc);
	printf("Guess the column location of a Bone");
	scanf("%d",&colLoc);
	
	  if( array3[rowLoc][colLoc]==id)
	  {
		  printf ("Congratulations ,You found a Location!");
		  num_found++;
		  
	  }
	  else
	  { 
          printf("Sorry. Try Again");
		  
	  }
	      attempts++;
	}/*endwhile*/

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

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

Good news is I'm starting to understand 2d arrays. Bad news is made initial populate array either isn't populating or my array that is supposed to count the locations isn't counting

here is my initialize and populate function

void populate(int array[][col], int row_size, int col_size)
{
	
	int x, y;

	for (int i=0;i<row_size;i++)
		for (int j=0;j<col_size;j++)

		array[i][j] = 0;

	srand(time(NULL)*rand());

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

	x= rand() % row_size;
	y = rand() % col_size;

	if(array[x][y]!=0)

	array[x][y] = id;

        }/*endfor*/
   

}

And here is my count locations function

int countLoc(const int array2[][col], int row_size, int col_size )
{
	int count = 0;		//Stores the number of locations found

	for (int i=0;i<row_size;i++)
		for (int j=0;j<col_size;j++)
		 if(array2[i][j] ==id)
		 {
			 count++;
		 }

	return count;
}
Member Avatar for rkamin1

Your count looks good. What is the variable Max_total initialized to?

Also, I can't emphasize enough the need for parentheses around for loops, especially when they are nested...

Without comments it's difficult to say for sure what you intended. I have commented on the code you posted based on what I think you are trying to do. If I am correct, then there is a logic error involved. Ask yourself how many bones will be placed in the array using this code?

//initially all elements of array have a default value of 0;
for (int i=0;i<row_size;i++)
{
  for (int j=0;j<col_size;j++)
  {
     array[i][j] = 0;
  }
}
	
for (int i=0;i<max_total;i++) 
{
   //obtain random values for row and col
   x= rand() % row_size;
   y = rand() % col_size;

   //if current array[x][y] is not the default value
   if(array[x][y] != 0)
     //place bone at current element
     array[x][y] = id;
}

The max_total is 20 I initially had a while loop to ensure that 20 positions got filled but once it didn't work i took it out.

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

	x= rand() % row_size;
	y = rand() % col_size;

	if(yard[x][y]!=0)

	yard[x][y] = id;
		
        }/*endfor*/
	 count++;
	}/*endwhile*/

}

Yeah, so its running now after i made some changes....

void populate(int array[][col], int row_size, int col_size)
{
    
    int x, y;

    for (int i=0;i<row_size;i++)
        for (int j=0;j<col_size;j++)
            array[i][j] = 0;

    srand(time(NULL)*rand());
    for (int i=0;i<max_total;i++) {
         x= rand() % row_size;
         y = rand() % col_size;
         if(array[x][y]!=0)
            array[x][y] = id;
        }/*endfor*/
}

And here is my count locations function

One last thing, I don't think you wan the following:

if(array[x][y]!=0)....

Reason; because you want to populate positions in the array aren't already occupied therefore you'd want to change it to the following:

if(!array[x][y])

This works because id is 1 and notting 1 is 0 which is the value that indicates it is not occupied. And I too think the count looks good!!!!

Good luck, LamaBot

That works! But is there a way I can ensure that the 20 locations always get filled? It seems to only get around 18

int countLoc(const int array2[][col], int row_size, int col_size )
{
    int count = 0;        //Stores the number of locations found

    for (int i=0;i<row_size;i++)
        for (int j=0;j<col_size;j++)
         if(array2[i][j] ==id)
         {
             count++;
         }

    return count;
}

When you cout the locations, try the following to see if it works then:

for (int i=0;i<=row_size;i++) {
  for (int j=0;j<=col_size;i++) {....

This might be the reason you can only see around 18 and not an exact 20. If you sometimes, randomly, get 20 locations but never less than 18 locations, this is probably the problem.

Good luck, LamaBot

nope,now I'm seeing 17 and sometimes 20

nope,now I'm seeing 17 and sometimes 20

Ok, keep the code I just said to changed, changed and try this.

void populate(int array[][col], int row_size, int col_size) {          
         int x, y;    
         for (int i=0;i<=row_size;i++)        
           for (int j=0;j<=col_size;j++)         
                 array[i][j] = 0;      
        srand(time(NULL)*rand());               
        for (int i=0;i<=max_total;i++) {      
           x= rand() % row_size;    
           y = rand() % col_size;      
          if(array[x][y]!=0)     
                  array[x][y] = id;         
 }/*endfor*/      
}

In the above code, I changed the for loops to have a "<=" that
may fixed the problem. Keep the coutLoc for loop "<=" suggestion
set along with this one, and let me know.

LamaBot.

Jeez! Stop shooting in the dark and think about the problem. First, format the code properly with braces and spaces so it can be read, then:

void populate(int array[][col], int row_size, int col_size)
{
    int x, y;
    for (int i=0; i <= row_size; i++)  // This loop just blew your array   
                                       //    boundaries. Take it back to <
    {
        for (int j=0; j <= col_size; j++)  // Same here
        {
            array[i][j] = 0;
        }
    }            
//  srand(time(NULL) * rand());  // don't multiply by rand -- does nothing
    srand(time(NULL));
    for (int i=0; i < max_total; i++) 
    {
        do              // start a loop
        {
            x= rand() % row_size;
            y = rand() % col_size;
        } while (array[x][y] != 0); // keep looping (get new coordinates) 
                                    //   if the location is already filled
        array[x][y] = id;
    }/*endfor*/
}

In the above code, I changed the for loops to have a "<=" that may fixed the problem. Keep the coutLoc for loop "<=" suggestion set along with this one, and let me know.

Looping 1 extra time is not a solution for data collisions. What if you had 2 collisions before the extra loop? You'll be missing one value. What if you had no collisions before the extra loop? You'd have too many values.

Problem solved by Walt's code.

LamaBot

Excellent! That works, thanks everyone 2d arrays are my new best friends :)

Excellent! That works, thanks everyone 2d arrays are my new best friends :)

I hope I don't "burn my bridges" by adding this but here it is. I'd like to say one thing, in reference to Walt's solution code to another. Consider the following which would do the exact same thing as Walt's for loop:

for (int x=0;x<max_value;x++) {
  x = rand() % row;
  y = rand() % col;
  if (array[x][y] == 0)
    array[x][y] = id;
  else
    x--;
}

The probability of a 0 character being encountered lessens each loop. Walt's corresponding code will loop 20 * (20 - the probability factor for each successive loop) as will the above for loop, however, what makes Walt's code better is that it executes less statements when it tries to find an element that is == 0. example; say there is an array of 5 characters like this "[1][1][1][0][1]". Using Walt's code the number of statemetns executed is 3 * 4. The above for loop is 5 * 4 which is obviously more statements. This is just to emphasize on why Walt's code would be bette than the above. I add this because it is helpful to think about this too when trying to make something work.

Best regards, LamaBot

>Consider the following which would do the exact same thing as Walt's for loop
Are you so sure about that? You're using one of the array subscript variables 'x', as a loop index. That's certain death when you start assigning it a random value inside the loop.

Well all be.... you're right Joe! Here is the oh so massive change ;)

for (int i=0;i<max_value;i++) {
  x = rand() % row;
  y = rand() % col;
  if (array[x][y] == 0)
    array[x][y] = id;
  else
    i--;
}
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.