solve assignment for programming C++

profileomaralekhmimi
firstname_lastname_cpsc120_seclab5.doc

CPSC 120

Spring 2014

Lab 5

Name _____________________________

Practice Objectives of this Lab:

1. Relational Operators

2. The if Statement

3. The if/else Statement

4. The if/else if Statement

5. Menu-Driven Programs

6. Nested if Statements

7. Logical Operators

Grading:

1. 5.1-5.4 15 points each,

5.5-5.6 20 points each

100 points totally

2. Your final complete solution report is due before your lab next week.

To begin

· Log on to your system and create a folder named Lab5 in your work space.

· Start the C++ IDE (Visual Studio) and create a project named Lab5.

Part I (60 points)

LAB 5.1 (15 points) – Using Boolean Variables and Branching Logic

Step 1: Add the tryIt5B.cpp program in your Lab5 folder to the project. Here is a copy of the int main() source code.

1 // Lab 5 tryIt5B

8 int main() 9 { 10 bool hungry = true, 11 sleepy = false, 12 happy = true, 13 lazy = false; 14 15 cout << hungry << " " << sleepy

<< endl;

16 17 if (hungry == true) 18 cout << "I'm hungry. \n"; 19 20 if (sleepy == true) 21 cout << "I'm sleepy. \n"; 22 23 if (hungry) 24 cout << "I'm still hungry. \n"; 25 else 26 cout << "I'm not hungry. \n"; 27 28 if (sleepy) 29 cout << "I'm still sleepy. \n"; 30 else 31 cout << "I'm not sleepy. \n";

32 33 if (sleepy) 34 cout << "I'm sleepy. \n"; 35 else if (lazy) 36 cout << "I'm lazy. \n"; 37 else if (happy) 38 cout << "I'm happy. \n"; 39 else if (hungry) 40 cout << "I'm hungry. \n"; 41 42 return 0; 43 }

Expected Output

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

Observed Output

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

_________________

Step 2: Read the source code, paying special attention to the expressions that control the branching statements. Then complete the “Expected Output” column above, writing down what output you think each cout statement will produce. If no output will be produced, leave the line blank.

Step 3: Now compile and run the tryIt5B.cpp program, and look at the output it creates. If the actual output from a cout statement matches what you wrote down, just place a checkmark in the “Observed Output” column. If it is not the same, write down the actual output.

LAB 5.2 (15 points) – Working with the if and if/else Statements

Step 1: Remove tryIt5B.cpp from the project and add the testNum.cpp program in your Lab5 folder to the project. Here is a copy of the source code.

1 // Lab 4 testNum.cpp 2 // This program checks to see if a test score is equal to 100. 3 // It currently contains a logic error. 4 // PUT YOUR NAME HERE. 5 #include <iostream> 6 using namespace std; 7 8 int main() 9 { 10 int score = 65; // Initialize student's test score 11 12 if (score = 100) 13 cout << "You made a perfect score.\n"; 14 15 if (score != 100) 16 cout << "You needed " << 100 - score 17 << " more points for a perfect score.\n"; 18 19 return 0; 20 }

Step 2: What do you think the program will print when it is run?

___________________________________________________________________________________

Compile and run the program. Did it produce the output you expected? ___________

Find the logic error in the program and fix it so that the program produces the correct output. Write down the correct output it now displays. _______________________________________________________

Step 3: Replace the two if statement in the program with a single if/else statement. Then recompile and rerun it. You should get the same correct output you wrote down above.

Step 4: Print a copy of the source code and make a screenshot for the output.

LAB 5.3 (15 points) – Working with if/else if Statements

Step 1: Remove testNum.cpp from the project and add the color.cpp program in your Lab4 folder to the project. Here is a copy of the source code.

1 // Lab 4 color.cpp 2 // This program lets the user select a primary color from a menu. 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 #include <string> 6 using namespace std; 7 8 int main() 9 { 10 int choice; // Menu choice should be 1, 2, or 3 11 12 // Display the menu of choices 13 cout << "Choose a primary color by entering its number. \n\n"; 14 cout << "1 Red \n" << "2 Blue \n" << "3 Yellow \n"; 15 16 // Get the user's choice 17 cin >> choice; 18 19 // Tell the user what he or she picked 20 if (choice == 1) 21 cout << "\nYou picked red.\n"; 22 else if (choice == 2) 23 cout << "\nYou picked blue.\n"; 24 else 25 cout << "\nYou picked yellow.\n"; 26 27 return 0; 28 }

Step 2: Compile the program and then run it 5 times. For each run enter the menu choice shown in the table below and write down the current output the program displays.

Run Menu choice Current output New output

1 1 ___________________ ___________________

2 2 ___________________ ___________________

3 3 ___________________ ___________________

4 0 ___________________ ___________________

5 99 ___________________ ___________________

Step 3: Improve the program so that only 1, 2, and 3 are accepted as valid choices. Make line 24 check for a choice of 3 before printing the message on line 25. Then add a trailing else that prints a descriptive error message whenever anything other than 1, 2, or 3 is entered.

Step 4: Recompile the program and run it 5 more times, using the same 5 menu choices shown above. For each run, write down the new output the program now displays.

LAB 5.4(15 points) – Working with Nested if/else Statements

Step 1: Remove color.cpp from the project and add the petTag.cpp program in your Lab5 folder to the project. Here is a copy of the source code.

1 // Lab 4 petTag.cpp 2 // This program determines the fee for a cat or dog pet tag. 3 // It uses nested if/else statements. 4 // PUT YOUR NAME HERE. 5 #include <iostream> 6 #include <string> 7 using namespace std; 8 9 int main() 10 { 11 string pet; // "cat" or "dog" 12 char spayed; // 'y' or 'n' 13 14 // Get pet type and spaying information 15 cout << "Enter the pet type (cat or dog): "; 16 cin >> pet; 17 cout << "Has the pet been spayed or neutered (y/n)? "; 18 cin >> spayed; 19 20 // Determine the pet tag fee 21 if (pet == "cat") 22 { if (spayed == 'y') 23 cout << "Fee is $4.00 \n"; 24 else 25 cout << "Fee is $8.00 \n"; 26 } 27 else if (pet == "dog") 28 { if (spayed == 'y') 29 cout << "Fee is $6.00 \n"; 30 else 31 cout << "Fee is $12.00 \n"; 32 } 33 else 34 cout << "Only cats and dogs need pet tags. \n"; 35 36 return 0; 37 }

Step 2: Compile the program and then run it 7 times. For each run use the input test data shown in the table below and write down the fee information that is displayed. Indicate whether or not the displayed information seems to be correct or not.

Run Input data Fee Information Correct?

1 cat y ___________________ ________

2 cat n ___________________ ________

3 cat Y ___________________ ________

4 dog y ___________________ ________

5 dog n ___________________ ________

6 dog Y ___________________ ________

7 hamster n ___________________ ________

Step 3: Improve the program in the following two ways.

· Use a logical OR on lines 22 and 28 so that either a lowercase ‘y’ or an uppercase ‘Y’ is accepted.

· Currently when an animal other than a cat or dog is entered (such as a hamster), the program asks if it is spayed or neutered before displaying the message that only cats and dogs need pet tags. Find a way to make the program only execute the spay/neuter prompt and input when the pet type is cat or dog.

Step 4: Recompile your revised program and test it with the same 7 test cases given in the previous table. Make sure it works correctly for all 7 cases.

Step 5: Copy your final source code and make 7 screenshots for the 7 testing cases.

Part II (40 points)

Please do Programming Challenges in the following way.

1) Pseudocode of your program (20% grading);

2) Your C++ source code (include your name, section number, CSUF email, lab number and date (60% grading);

3) The results gotten (3 Screen Shots) using any 3 testing cases that are reasonable

(20% grading)

LAB 5. 5(20 points) –Programming Challenges of Chapter 4 # 8 Book Club Points (Page 236)image1.png

LAB 5. 6 (20 points) –Programming Challenges of Chapter 4 # 9 Software Sales (Page 237)

image2.png

1