d. Assumecalories is already declared as an integer. Code a fall-through switchstatement by dividing calories by 100 in the controlling expression of the switch header, so the following messages print: When caloriesare 1200 through 1800: “Diet is on target.” When caloriesare 2000 through 2550: “Calorie intake ok if active.” When caloriesare any other value: “Calorie intake is either insufficient or too much!” e. Re-code 1d using double-selection ifs. You’ll use a conditional logical operator to join the sets of relational conditions (choose the right one). f. Code a forloop with the switch structure from 1d and allow 3 attempts. Prompt the user to enter calories. When the number entered is in the correct range exit the loop (not the program) after one of the messages in 1d is printed. Print the message “No more attempts left!” when it is the last attempt.
//4th question using switch case
switch(cal/100) // integer type cal
{
case 12 : // as cal is integer type , the division of integer to a integer would be always
case 13: // an integer ie. float or decimal value would be discarded
case 14: // and one thing the switch case cases can only be of two datatypes
case 15: // that are char type and int type respectively any other type may result in
case 16: //errors . Here from 1200 to 1800 was the limit so 1200/100=12
case 17: //for any case the value will go to case 18 in the specified range
case 18: cout<<"Diet is on target";
break; // always remember to place the break in switch cases to prevent dangling
case 20: //case 19 was not specified in ques. so it will go to default case
case 21:
case 22:
case 23:
case 24:
case 25: cout<<"cal is good is active lifestyle"; // we can't take float values hence case 25 not 25.5 in switch
break;
default: cout<<"the cal is either too low or high";
}
//5th ques .... personally i suggest to use if else for these cases because using the switch case results in O(n) time complexity
// as O(1) for every case so if there are n cases time complexity will reach O(n) as going to every case which is not even its
//plus in if else you can use float type values giving higher precision
if((cal<=1800)&&(cal>=1200) //&& logical and operator for range satisfying both conditions
cout<<"diet is on target;
else
{
if((cal<=2550)&&(cal>=2000)) //complexity of the code reduced by n ie. it is n times faster than
cout<<"ohk is active lifestyle"; // previous one
else
cout<<"calories either too high or low";
}
//6th question
// for these types you should use the do while loops as at least you'll run the loop once and on while you can specify no. of test cases ie. T such that do{........}while(T--); so the loop will run till T=1 as in while(T--) means the expression inside will be true if there is any value except 0 and false if there is 0 . Hence as it becomes zero the loop will stop.
int i;
for(i=0;i<3;i++) //for loop after initialization of "i" it will check condition after wards it will update
{ //the value of i then check the condition
cout<<"enter the cal";
cin>>cal;
switch(cal/100)
{
case 12 :
case 13:
case 14:
case 15:
case 16:
case 17:
case 18: cout<<"Diet is on target";
break; //to get out of switch case
case 20:
case 21:
case 22:
case 23:
case 24:
case 25: cout<<"cal is good is active lifestyle";
break; //to get out of switch case
default: continue; // to go to next iteration immediately with updated value of iterator i
}
break; //to exit loop
}
if(i==3) //because after the complete loop run value of i would be 3
cout<<"no more choices left";
d. Assumecalories is already declared as an integer. Code a fall-through switchstatement by dividing calories by...
d. Assumecalories is already declared as an integer. Code a fall-through switchstatement by dividing calories by 100 in the controlling expression of the switch header, so the following messages print: When caloriesare 1200 through 1800: “Diet is on target.” When caloriesare 2000 through 2550: “Calorie intake ok if active.” When caloriesare any other value: “Calorie intake is either insufficient or too much!” e. Re-code 1d using double-selection ifs. You’ll use a conditional logical operator to join the sets of relational conditions (choose the right one). ...