Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. An electronics store is having a sale. Items from the audio department (dept

ID: 3890228 • Letter: 8

Question

 8. An electronics store is having a sale. Items from the audio department    (dept code 310) are 10% off. Items from the video department (dept    code 438) are 12% off. Items from the computer department (dept    code 284) are 8% off, and items from the communications department    (dept code 652) are 15% off. Items from other departments are 5%    off. Write the statements to read in the regular price of an item and    the dept code, and calculate the sale price. Print the regular price    and the sale price. Use a nested if statement.   9. Rewrite your code from problem 8 using a switch statement. 

Explanation / Answer

// C++ code , problem 8, using if else statements
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
double regular_price;
cout << "Enter regular price: ";
cin >> regular_price;

int code;
cout << "Enter code: ";
cin >> code;

double sale_price = 0;
if(code == 310 || code == 438 || code== 284 || code == 652)
{
if(code == 310)
sale_price = regular_price - regular_price*0.1;
else if(code == 438)
sale_price = regular_price - regular_price*0.12;
else if(code == 284)
sale_price = regular_price - regular_price*0.08;
else if(code == 652)
sale_price = regular_price - regular_price*0.15;
}
else
{

sale_price = regular_price - regular_price*0.05;
}


cout << "Regular price: " << regular_price << endl << "Sale price: " << sale_price << endl;
return 0;

}


/*
output:

Enter regular price: 345
Enter code 284
Regular price: 345
Sale price: 317.4

*/





// C++ code , problem 9, using switch statements
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
double regular_price;
cout << "Enter regular price: ";
cin >> regular_price;

int code;
cout << "Enter code: ";
cin >> code;

double sale_price = 0;
switch(code)
{
case 310:
sale_price = regular_price - regular_price*0.1;
break;
case 438:
sale_price = regular_price - regular_price*0.12;
break;
case 284:
sale_price = regular_price - regular_price*0.08;
break;
case 652:
sale_price = regular_price - regular_price*0.15;
break;
default:
sale_price = regular_price - regular_price*0.05;
break;
}


cout << "Regular price: " << regular_price << endl << "Sale price: " << sale_price << endl;
return 0;

}


/*
output:

Enter regular price: 345
Enter code 284
Regular price: 345
Sale price: 317.4

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote