mardi 28 juillet 2020

Why would a mod return zero when it's restricted to <=1?

Card* createCard()
{

  /*This function dynamically allocates a new Card struct object and returns a 
  pointer to that struct object which will later be used to insert into a 
  linked list. There are three types of cards ATTACK, DEFEND, and RUN. 
  ATTACK and DEFEND cards also have a value. 
  You will assign a card type based on these random chances:
    40% - ATTACK: the value is a random number between 1 and 5 inclusive.
    50% - DEFEND: the value is a random number between 3 and 8 inclusive.
    10% - RUN: the value is a random number between 1 and 8 inclusive. 
    The value of a RUN card is only used for sorting purposes.*/
    
    Card* createdCard;
    int n;
    int v; 
    createdCard = (Card*) malloc(sizeof(Card));
    
    n = (rand()%10)+1;
    
    
    if(n == 1)
    {
        createdCard->cardType = RUN;
        v = (rand() % 8)+1;
        createdCard->value = v;
    }
    else if(n>1 && n<7)
    {
        createdCard->cardType = DEFEND;
        v = (rand() % 6)+3;
        createdCard->value = v;
    }
    else if(n>6 && n<10)
    {
        createdCard->cardType = ATTACK;
        v = ( rand() %5)+1;
        createdCard->value = v;
    }
    
    
    
     createdCard->next = NULL;
    
    return createdCard;

}

Output with A0 and should be 1 through 5




Aucun commentaire:

Enregistrer un commentaire