Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Whenever I run this piece of code and it gets a 0 value it executes the first and third if statements C++

0 votes
asked Apr 9, 2018 by SavageMango (310 points)
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
    

int bowl()
{
   srand(time(NULL));
   int arr[37]={0,0,0,0,1,1,2,2,3,3,4,4,4,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10};
   int RandIndex = rand() % 37 ;
    if (9 >= arr[RandIndex] >= 1)
    {
     cout<<"You knocked down "<< arr[RandIndex]<< " pins!"<<endl;
    }
   
    if (arr[RandIndex]== 10)
    {
      cout<<"Strike!"<<endl;
    }
    
    if (arr[RandIndex]== 0)
    {
     cout<<"You threw a gutterball :("<<endl;
    }
    
  
    I only want it to execute the last if statement
    
}

int main()
    {
      bowl();
    }

1 Answer

0 votes
answered Apr 11, 2018 by anonymous
Since "RandIndex" is initialized with random value % 37, it is uncertain that which if condition will get execute.
If you want to be certain that every time last if condition is executed, initialize RandIndex with rand()%4
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...