• Counting consecutive numbers in an array C++

    CarltonBirch Member

    For my computer science homework I have to create a game. I pretty much have all of it finished but I am having trouble creating the code necessary to check for straights. I plan on calling a function called sequence with the parameter of the dice rolled. This function will pretty much just return the highest amount of consecutive numbers there are in the array but it is not working. Can someone give me some feedback or recommend a better solution please? !

    Here is my code so far:

    
    int sequence(int dice[])
    {
       int seq;
       int maxS;
    
       seq = 1;
       maxS = 1;
    
       for (int i = 1; i < SIZE; i++)
       {
          if (dice[i] > 0)
             {
                if(i < 6)
                {
                   if(dice[i+1] > 0) //There is a value in the next number
                   {
                      seq++;   //Add to the sequence for sure
                   }
    
                   else
                   {                  
                      if (maxS <= seq)
                         {
                         maxS = seq;
                         }
                      seq = 1; //Else reset the counter and move on
                   }
                }
          }
          else
          {
             if (maxS <= seq)
                      {
                      maxS = seq;
                      }
                seq = 1;
          }
    
       }
       if (seq > maxS)
          return seq;
       else
          return maxS;
    }
    

    It’s a bit confusing but the concept is pretty simple. The array of dice is just an array with 6 elements, and I only use elements 1-6 for the count of dice. So if the value of element 3 was 1, a single dice would have the value of 3.

Viewing 0 reply threads
  • You must be logged in to reply to this topic.