How to check if all the values of an array are equal to 0?

后端 未结 5 1458
感情败类
感情败类 2021-01-16 14:02

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

5条回答
  •  鱼传尺愫
    2021-01-16 14:43

    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
    }
    

提交回复
热议问题