jeudi 9 avril 2020

Why this code works for 8 different test cases except 1 in C++?

So I wrote this function in C++ which basically counts the maximum number in an array and then prints out the number of maximum numbers in the array. Here's the code of the function:

int Number_of_maxNum(vector<int> ar) {
      int max=0;
      int Number_of_Maxnum=0;
      int d = ar.size();

      for(int i=0;i<=d;i++){
          if(ar[i]>max){
          max=ar[i];
          }
      }

      for(int j=0;j<=d;j++){
          if(ar[j]==max){
             Number_of_Maxnum++;
          }
      }

      return Number_of_Maxnum;

}

Now this code however doesn't work for the following array as input: {44, 53, 31, 27, 77, 60, 66, 77, 26, 36} It should print out 2, but print out 1

If someone could please explain what's actually going on with that input that's giving 1 as an input, It would

Aucun commentaire:

Enregistrer un commentaire