Here is a code fragment which appears in a valid main int nu

Here is a code fragment which appears in a valid main(): int numGifts; double costPerGift; // numGifts gets a value somehow in code that is not shown. then ... if ( numGifts <= 10 ) costPerGift = 3.0; else if ( numGifts <= 20 ) costPerGift = 2.75; else costPerGift = 2.5; For which values of numGifts will costPerGift be 2.75 at the end of the fragment? (Check all boxes that apply - there will be more than one.)

numGifts = 11

numGifts = 20

numGifts = -1

numGifts = 8

Solution

the code fragment

if(numGifts<=10)

costPerGift=3.0;

else if(numGifts<=20)

costPerGift=2.75)

else

costPerGift=2.5

decides what value is assigned to costPerGift depending on the value of numGifts

if (numGifts<=10)

costPerGift=3.0;

//implies any value of numGifts that is less than or equal to 10 gives the costPerGift the value 3.0 numGifts

else if (numGifts<=20)

costPerGift=2.75;

/*implies any value of numGift that is less than or equal to 20 but more than 10 (beacause of the else <=10 values have already been considered and are not to be considered again) gives the costPerGift the value of 2.75(this is what asked )*/

else

costPerGift=2.5;

/*implies all the values more than 20 or say not taken care by the previous if else statements then costPerGift is given 2.5*/

here is asked all those values of numGifts for which costPerGift=2.75

so 2nd else if statement is considered

/*implies any value of numGift that is less than or equal to 20 but more than 10 (beacause of the else <=10 values have already been considered and are not to be considered again) gives the costPerGift the value of 2.75(this is what asked )*/

so,only

numGifts=11 and numGifts=20 gives costPerGift the value 2.75

Here is a code fragment which appears in a valid main(): int numGifts; double costPerGift; // numGifts gets a value somehow in code that is not shown. then ...

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site