Question

In: Computer Science

Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is...

Problem set

  1. Rewrite the following if statement as an equivalent switch statement. The variable digit is of type int.

if (digit == 0)

value = 3;

else if (digit == 1)

value = 3;

else if (digit == 2)

value = 6;

else if (digit == 3)

    value = 9;

  1. The decision table below shows fines imposed for speeding violations. Write a code segment that assigns the correct fine to type double variable fine based on the value of type int variable speed.

    Speed (mph) Fine ($)

    65 or less   0

    66-70        15.00

    71-75        30.00

    76-80        75.00

    over 80 100.00

  1. Rewrite the switch statement below as a multiple-alternative if statement.

    switch (jersey) {

    case 11:

         printf("I. Thomas\n");

         break;

    case 23:

         printf("M. Jordan\n");

         break;

    case 33:

         printf("S. Pippen\n");

         break;

    default:

         printf("Player unknown\n");

    }

  1. What is the output of the following program?

         count = 5;

         while (count > 0) {

           print(“Woot! ”);

           count -= 1;

         }

Solutions

Expert Solution

Program:

#include<stdio.h>
int main() {
int digit=2;// Here initailizing the value of digit to 2
if (digit == 0)
printf("value = 3");
else if (digit == 1)
printf("value = 3");
else if (digit == 2)
printf("value = 6");
else if (digit == 3)
printf("value = 9");
}
Output:

Using Equivalent switch statement

Program:

#include<stdio.h>
int main() {
int digit=2;// Here initailizing the value of digit to 2
switch (digit) {
case 0:
printf("value = 3\n");
break;
case 1:
printf("value = 3\n");
break;
case 2:
printf("value = 6\n");
break;
case 3:
printf("value = 9\n");
break;
default:
printf("NULL\n");
}
}

Output:

Program:

#include<stdio.h>
int main() {
int Speed;
double Fine;// Here initailizing the value of digit to 2
printf("Enter Speed in kmph: ");
scanf("%d",&Speed);
if (Speed <= 65)
printf("Fine = 0");
else if (Speed >=66 &&Speed <= 70)
printf("Fine = 15.00");
else if (Speed >=71 &&Speed <= 75)
printf("Fine = 30.00");
else if (Speed >=76 &&Speed <= 80)
printf("Fine = 75.00");
else if (Speed >80)
printf("Fine = 100.00");
}

Output:

Program:

#include<stdio.h>
int main() {
int jersey=11;// Here initailizing the value of jersey to 11
switch (jersey) {
case 11:
printf("I. Thomas\n");
break;
case 23:
printf("M. Jordan\n");
break;
case 33:
printf("S. Pippen\n");
break;
default:
printf("Player unknown\n");
}
}

Output:

Using multiple-alternative if statement.

Program:

#include<stdio.h>
int main() {
int jersey=11;// Here initailizing the value of jersey to 11
if(jersey == 11)
{
printf("I. Thomas\n");
}
else if (jersey == 23)
{
printf("M. Jordan\n");
}
else if (jersey == 33)
{
printf("S. Pippen\n");
}
else
{
printf("Player unknown\n");
}
}

Output:


  

Program:

#include<stdio.h>
int main() { // Start of main()
int count = 5;// Here initailizing the value of count
while (count > 0) { // Here checking the condition count > 0
printf("Woot! "); // Here this statement is printed if above condition is True
count -= 1; // Here decrementing the value of count by 1
} // En of while() loop
} // End of main()

Output:



Related Solutions

Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit...
Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit number from 31 to 99. (You do not have to check the numbers just assume the user will enter a valid number so long as you tell them to). Store an extra copy of this number for later 3.   Add 82 to the number. 4.    Remove the hundreds place from your number 5.    Add 1 to the number. 6.    Subtract this from your...
Rewrite the following SELECT statement. In the rewrite change the join operator style on the Facility...
Rewrite the following SELECT statement. In the rewrite change the join operator style on the Facility table to a Type I nested query. (10 points) SELECT eventplan.planno, eventrequest.eventno, workdate,activity FROM eventrequest, eventplan, Facility WHERE eventplan.workdate BETWEEN '2018-12-01' AND '2018-12-31' AND eventrequest.eventno = eventplan.eventno AND EventRequest.FacNo = Facility.FacNo AND facname = 'Basketball arena'; Rewrite the following SELECT statement. In the rewrite change the join operator style on the Facility and Employee tables to a Type I nested queries. (15 points) SELECT...
The following is a set of tree-set test programs that show the following outputs: Switch to...
The following is a set of tree-set test programs that show the following outputs: Switch to ArrayList, LinkedList, Vector, TreeMap, and HashMap to display similar output results. Results: Tree set example! Treeset data: 12 34 45 63 Treeset Size: 4 First data: 12 Last Data: 63 Removing data from a tree set Current tree set elements: 12 34 63 Current tree set size :3 Tree set empty. Example code import java.util.Iterator; import java.util.TreeSet; public class TreeDemo2 { public static void...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variable s is specified to be assumed to be one of three values ​​of [0, 2], and a different action for each case becomes: case 0: a = a - 1; break; case 1: a = a + 2; break; case 2: b = 3 * b; break; Shows how such statements can be compiled into MiniMIPS assembly instructions.
A switch/case statement allows multiway branching based on the value of an integer variable. In the...
A switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variables can assume one of the three values in [0, 2] and a different action is specified for each case. switch (s) {    case 0: a=a+1; break;    case 1: a=a-1; break;    case 2: b=2*b; break; } Show how such a statement can be compiled into MiniMIPS assembly instructions.
Fill in the blanks to rewrite the following statement: There is a positive integer that is...
Fill in the blanks to rewrite the following statement: There is a positive integer that is less than or equal to every positive integer. (a) There is a positive integer m such that m is . (b) There is a such that every positive integer. (c) There is a positive integer m which satisfies the property that given any positive integer n , m is
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter)...
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter) { case 1: Enter = -4; case 2: Enter = -6; case 4: break; case 6: Enter = -8; break; default: Enter = -1; } What would the value of Enter be after execution of this code if the value read for Enter were 4? -4,-6,-8 or none
For the following plots, rewrite the equivalent code in ggplot2 grammar. qplot(displ, hwy, data = mpg,...
For the following plots, rewrite the equivalent code in ggplot2 grammar. qplot(displ, hwy, data = mpg, geom = c("point", "smooth")) qplot(color, data = diamonds, fill = cut, position = "identity")
Answer whether the following statement is TRUE or FALSE. If FALSE, rewrite the sentence to be...
Answer whether the following statement is TRUE or FALSE. If FALSE, rewrite the sentence to be TRUE. (15 pt) A. Reproducible set of DNA fragments will be produced every time a restriction endonuclease digests a known piece of DNA. B. Restriction nucleases recognize and cut specific sequences on single-stranded DNA. C. The larger the genome of the organism from which a library is derived, the larger the fragments inserted into the vector will tend to be. D. Ligation reactions by...
Question 4: Set P to the largest digit in your 5 digit number. For example, if...
Question 4: Set P to the largest digit in your 5 digit number. For example, if your 5 digit number is 88412, then P should be set to 8. Calculate and print the value of (P+1) multiplied by itself (P+3) times. As an example, if your 5 digit number is 88412, you should print the result of multiplying 9 with itself 12 times -- this is also known as 9 to the power of 12. HINT: you are free to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT