1

The context of the program is a game involving pegs and discs. The user inputs the amount of pegs (max of 20) and the amount of discs on each peg (max of 10). Two players go back and forth removing any amount of discs on a single peg each turn, given that there are enough discs to remove on that peg. The player to remove the last disc loses.

The number of discs are stored in an array, where the index of the array corresponds the peg number. I have a boolean function that checks whether or not the pegs are empty of discs, implying someone has won. There is some logical error in my code but I can't figure out what it is:

bool checkPegs(int array[], int size)
{
  int checker(0);
  for (int i = 0; i < size; i++)
  {
      if(array[i] = 0)
      {
        return true;
      }
      else
      {
        return false;
      }
  }
}
Harsh Patel
  • 31
  • 1
  • 1
  • 2
  • 2
    `if(array[i] == 0)` instead of `if(array[i] = 0)`? – Simon Kraemer Nov 13 '15 at 15:38
  • 2
    `=` is assignment, and you want to keep looping until either the first non-zero value or the end. – molbdnilo Nov 13 '15 at 15:38
  • 1
    Possible duplicate of [Can I check in C(++) if an array is all 0 (or false)?](https://stackoverflow.com/questions/4044590/can-i-check-in-c-if-an-array-is-all-0-or-false) – phuclv Mar 07 '19 at 08:03

5 Answers5

3
bool checkPegs(int array[], int size)
{
  for (int i = 0; i < size; i++)
  {
      if(array[i] != 0)
      {
        return false;
      }
  }
  return true;
}
Tefek
  • 162
  • 1
  • 10
3

try memcmp instead of having separate function with for loop:

int zeros[sizeof(yourArray)];
if(memcmp(yourArray,zeros,sizeof(yourArray))==0)
//do things
else
//do things
0
if(array[i] = 0)

That doesn't compare array[i] with 0, it assigns 0 to array[i]. You want array[i] == 0.

if(array[i] == 0)
{
    return true;
}
else
{
    return false;
}

The second issue is that you only check the first element, then return based on that. You should check every element to ensure they are non-zero:

for (int i = 0; i < size; i++)
{
    if(array[i] != 0) {
        return false;
    }
}

Finally, you don't handle the case that size is 0. In that case, you should return true.

bool checkPegs(int array[], int size)
{
    for (int i = 0; i < size; i++)
    {
        if(array[i] != 0) {
            return false;
        }
    }

    return true;
}
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
0

there are two errors here

bool checkPegs(int array[], int size)
{
  int checker(0);
  for (int i = 0; i < size; i++)
  {
      if(array[i] = 0) // the first one use '==' instead of '='
      {
        return true; // the second one, you are testing the first element only
      }
      else
      {
        return false;
      }
  }
}

here how it should be

bool checkPegs(int array[], int size)
{

  for (int i = 0; i < size; i++)
  {
      if(array[i] )
          return false; // return false at the first found

  }
  return true; //all elements checked
}
milevyo
  • 2,165
  • 1
  • 13
  • 18
0

The way you wrote your code cannot work, for you are actually considering only the first element because of the two return statements in the if/else. Moreover, you use an assignment statement instead of a comparison.

It follows a reviewed example:

bool checkPegs(int *array, int size) {
    for (int i = 0; i < size; i++) {
        if(array[i] != 0) { return false; }
    }
    return true;
}

Keep in mind that it can be optimized and you can do the same using standard utilities, but I assume that you are learning to code and so it's worth to write it for yourself.

skypjack
  • 49,335
  • 19
  • 95
  • 187