Question

In: Computer Science

in c++ codeblocks Convert the following code into a switch structure. Make the switch structure as...

in c++ codeblocks

Convert the following code into a switch structure. Make the switch structure as short as possible (do not repeat code). if (i == 3 || i == 5) { n++; tryagain = 0; } else if (i == 4) || i == 10) { n = 5; } else if (i == 6) { n = 6; } else { n = 0; tryagain = 1; }

Write an if statement that will give 5 extra credit points to students who answer Question A correctly, and 10 extra points if they answer Question A and Question B correctly. Name the variable intBonus.

Solutions

Expert Solution

So, here we want to convert a nested loop to switch:

Steps to follow:

  1. Pass the argument which we wan to check to the switch statement.
  2. Now, for every condition, use a case.
  3. For OR ("||") operations, we can use two switch statements together.

Please refer to the comments of the program for more clarity.

    // Starting of the switch block
    switch(i)
    {
       case 3:    // For (i == 3 || i == 5)
       case 5:
        {
            n++; tryagain = 0;
            break;
        }

       case 4:    // For (i == 4 || i == 10)
       case 10:
        {
            n = 5;
            break;
        }
       case 6:   // For (i == 6)
        {
            n = 6;
            break;
        }
       default:  // For others
        {
            n = 0; tryagain = 1;
            break;
        }
    }  // End of the switch block

CodeBlock Screenshot:

Now, as asked lets see an example of if statement, using variable, intBonus:

In the below code, we care finding the intBonus using the if-statements,

I have applied if conditions on variable "workExperience" and calculated intBonus. You can interchange the variables according to your requirement.

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int intBonus = 0;  // Initializing intBonus as 0
    int workExperience = 0; // Initializing workExperience as 0
    cout << "Enter your work experience: ";
    cin >> workExperience;  // Taking input

    // calculating the bonus using the if statements
    if(workExperience >= 2 && workExperience < 5)
    {
        intBonus = 1000;
    }
    else if(workExperience >= 5)
    {
        intBonus = 5000;
    }
    else
    {
        intBonus = 500;
    }
    cout << "\n\nYour Bonus: " << intBonus << endl;  // Printing the output
    return 0;
}

Code-run/Input-Output:

CodeBlock Screenshot:

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of characters using Caesar cipher please attach samples run of the code #include <iostream> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string> #define MAX_SIZE 200 void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string. void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string. using namespace std; int main() { char input_str[MAX_SIZE];...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Question:   Can you please convert this program into switch case program using c++. Output should be...
Question:   Can you please convert this program into switch case program using c++. Output should be same int main() {     bool weekend = false;     unsigned char day = 0;     bool isDay = true;     cout << " Enter a char for a day of week:";     cin >> day ;     if (day == 'M' || day == 'm')         cout << " This is Monday" << endl ;     else if (day == 'T' || day...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT