350,531 views
24 votes
24 votes
Goal: The campus squirrels are throwing a party! Do not ask. They need you to write a method that will determine whether it will be successfull (returns true) or not (returns false). The decision will be based on int values of the two parameters "squirrels" and "walnuts" representing respectively the number of squirrels attending the party and the numbers of walnuts available to them. The rules for making that determination are detailed below. Variables declarations & initialization: Boolean variable named "result", initialized to false. Steps for the code: First, if less than 1 squirrel attends, then "result" is assigned false. Else, we consider the following conditions: If we have less than, or exactly, 10 squirrels attending: Then: If we have less than 15 walnuts, Then: "result" is set to false. Else: if we have less than, or exactly, 30 walnuts Then: "result" is set to true. Else: "result" is set to false. Else: If less than, or exactly, 30 squirrels are attending Then: If the number of walnuts is more or equal to twice the number of squirrels attending Then: "result" is set to true Else: "result" is set to false Else: If the number of walnuts is equal to 60 plus the number of squirrels attending minus 30 Then: "result" is set to true Else: "result" is set to false

User Safrazik
by
2.7k points

1 Answer

21 votes
21 votes

Answer:

Following are the code to the given question:

#include<iostream>// header file

using namespace std;

int main()//main function

{

int squirrels, walnuts;//defining integer variable

bool result = false; //defining a bool variable that holds a false value

cout<<"Enter the number of squirrels"<<endl;//print message

cin>>squirrels; // input integer value

cout<<"Enter the number of walnuts"<<endl;//print message

cin>>walnuts; // input integer value

if(squirrels < 1)//use if variable that checks squirrels value less than 1

{

result = false;//use result variable that holds false value

}

else//defining else block

{

if(squirrels <= 10)//use if variable that checks squirrels value less than equal to 10

{

if(walnuts < 15) //use if variable that checks walnuts value less than 15

result = false;//use result variable that holds false value

else if(walnuts <= 30)//use elseif block that checks walnuts less than equal to 30

result = true; //use result variable that holds true value

else //defining else block

result = false;//use result variable that holds false value

}

else if(squirrels <= 30)//use elseif that checks squirrels value less than equal to 30

{

if(walnuts >= 2*squirrels) //use if block to check walnuts value greater than equal to 2 times of squirrels

result = true; //use result variable that holds true value

else//defining else block

result = false;//use result variable that holds false value

}

else if(walnuts == 60 + squirrels - 30)//using elseif that checks walnuts value equal to squirrels

result = true;//use result variable that holds true value

else //defining else block

result = false;//use result variable that holds false value

}

if(result)//use if to check result

cout<<"True";//print True as a message

else //defining else block

cout<<"False";//print False as a message

return 0;

}

Output:

Enter the number of squirrels

10

Enter the number of walnuts

30

True

Explanation:

In this code, two integer variable "squirrels and walnuts" and one bool variable "result" is declared, in which the integer variable use that input the value from the user-end, and inside this the multiple conditional statements is used that checks integer variable value and use the bool variable to assign value as per given condition and at the last, it uses if block to check the bool variable value and print its value.

User Brendosthoughts
by
2.8k points