SlideShare a Scribd company logo
1 of 12
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.
nn";
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)
LAB 5. 6 (20 points) –Programming Challenges of Chapter 4 #
9 Software Sales (Page 237)
1

More Related Content

Similar to CPSC 120Spring 2014Lab 5Name _____________________.docx

Comp 122 lab 4 lab report and source code
Comp 122 lab 4 lab report and source codeComp 122 lab 4 lab report and source code
Comp 122 lab 4 lab report and source codepradesigali1
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docxrafbolet0
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsshyaminfo04
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsash52393
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comStephenson22
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comWilliamsTaylorza48
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.comHarrisGeorg12
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comrobertledwes38
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG Greg.Helton
 
Cis 170 ilab 1 of 7
Cis 170 ilab 1 of 7Cis 170 ilab 1 of 7
Cis 170 ilab 1 of 7comp274
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxAASTHA76
 
Md university cmis 102 week 5 hands
Md university cmis 102 week 5 handsMd university cmis 102 week 5 hands
Md university cmis 102 week 5 handseyavagal
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitAmr E. Mohamed
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxjacksnathalie
 
An introduction to Machine Learning
An introduction to Machine LearningAn introduction to Machine Learning
An introduction to Machine LearningValéry BERNARD
 
Cis 170 c ilab 5 of 7 arrays and strings
Cis 170 c ilab 5 of 7 arrays and stringsCis 170 c ilab 5 of 7 arrays and strings
Cis 170 c ilab 5 of 7 arrays and stringsCIS321
 
Cis 170 i lab 1 of 7
Cis 170 i lab 1 of 7Cis 170 i lab 1 of 7
Cis 170 i lab 1 of 7helpido9
 

Similar to CPSC 120Spring 2014Lab 5Name _____________________.docx (20)

Comp 122 lab 4 lab report and source code
Comp 122 lab 4 lab report and source codeComp 122 lab 4 lab report and source code
Comp 122 lab 4 lab report and source code
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and strings
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and strings
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.com
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.com
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.com
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.com
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG
 
Cis 170 ilab 1 of 7
Cis 170 ilab 1 of 7Cis 170 ilab 1 of 7
Cis 170 ilab 1 of 7
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
 
Md university cmis 102 week 5 hands
Md university cmis 102 week 5 handsMd university cmis 102 week 5 hands
Md university cmis 102 week 5 hands
 
Testing in Django
Testing in DjangoTesting in Django
Testing in Django
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
 
An introduction to Machine Learning
An introduction to Machine LearningAn introduction to Machine Learning
An introduction to Machine Learning
 
Cis 170 c ilab 5 of 7 arrays and strings
Cis 170 c ilab 5 of 7 arrays and stringsCis 170 c ilab 5 of 7 arrays and strings
Cis 170 c ilab 5 of 7 arrays and strings
 
Aguirre.Module2.Problemset.pdf
Aguirre.Module2.Problemset.pdfAguirre.Module2.Problemset.pdf
Aguirre.Module2.Problemset.pdf
 
Cis 170 i lab 1 of 7
Cis 170 i lab 1 of 7Cis 170 i lab 1 of 7
Cis 170 i lab 1 of 7
 

More from faithxdunce63732

Assignment DetailsScenario You are member of a prisoner revie.docx
Assignment DetailsScenario You are member of a prisoner revie.docxAssignment DetailsScenario You are member of a prisoner revie.docx
Assignment DetailsScenario You are member of a prisoner revie.docxfaithxdunce63732
 
Assignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docxAssignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docxfaithxdunce63732
 
Assignment DetailsScenario You are a new patrol officer in a .docx
Assignment DetailsScenario You are a new patrol officer in a .docxAssignment DetailsScenario You are a new patrol officer in a .docx
Assignment DetailsScenario You are a new patrol officer in a .docxfaithxdunce63732
 
Assignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docxAssignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docxfaithxdunce63732
 
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docxAssignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docxfaithxdunce63732
 
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docxAssignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docxfaithxdunce63732
 
Assignment DetailsPart IRespond to the following.docx
Assignment DetailsPart IRespond to the following.docxAssignment DetailsPart IRespond to the following.docx
Assignment DetailsPart IRespond to the following.docxfaithxdunce63732
 
Assignment DetailsPlease discuss the following in your main post.docx
Assignment DetailsPlease discuss the following in your main post.docxAssignment DetailsPlease discuss the following in your main post.docx
Assignment DetailsPlease discuss the following in your main post.docxfaithxdunce63732
 
Assignment DetailsPennsylvania was the leader in sentencing and .docx
Assignment DetailsPennsylvania was the leader in sentencing and .docxAssignment DetailsPennsylvania was the leader in sentencing and .docx
Assignment DetailsPennsylvania was the leader in sentencing and .docxfaithxdunce63732
 
Assignment DetailsPart IRespond to the followingReview .docx
Assignment DetailsPart IRespond to the followingReview .docxAssignment DetailsPart IRespond to the followingReview .docx
Assignment DetailsPart IRespond to the followingReview .docxfaithxdunce63732
 
Assignment DetailsPart IRespond to the following questio.docx
Assignment DetailsPart IRespond to the following questio.docxAssignment DetailsPart IRespond to the following questio.docx
Assignment DetailsPart IRespond to the following questio.docxfaithxdunce63732
 
Assignment DetailsPart IRespond to the following questions.docx
Assignment DetailsPart IRespond to the following questions.docxAssignment DetailsPart IRespond to the following questions.docx
Assignment DetailsPart IRespond to the following questions.docxfaithxdunce63732
 
Assignment DetailsOne thing that unites all humans—despite cultu.docx
Assignment DetailsOne thing that unites all humans—despite cultu.docxAssignment DetailsOne thing that unites all humans—despite cultu.docx
Assignment DetailsOne thing that unites all humans—despite cultu.docxfaithxdunce63732
 
Assignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docxAssignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docxfaithxdunce63732
 
Assignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docxAssignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docxfaithxdunce63732
 
Assignment DetailsIn this assignment, you will identify and .docx
Assignment DetailsIn this assignment, you will identify and .docxAssignment DetailsIn this assignment, you will identify and .docx
Assignment DetailsIn this assignment, you will identify and .docxfaithxdunce63732
 
Assignment DetailsFinancial statements are the primary means of .docx
Assignment DetailsFinancial statements are the primary means of .docxAssignment DetailsFinancial statements are the primary means of .docx
Assignment DetailsFinancial statements are the primary means of .docxfaithxdunce63732
 
Assignment DetailsIn this assignment, you will identify a pr.docx
Assignment DetailsIn this assignment, you will identify a pr.docxAssignment DetailsIn this assignment, you will identify a pr.docx
Assignment DetailsIn this assignment, you will identify a pr.docxfaithxdunce63732
 
Assignment DetailsHealth information technology (health IT) .docx
Assignment DetailsHealth information technology (health IT) .docxAssignment DetailsHealth information technology (health IT) .docx
Assignment DetailsHealth information technology (health IT) .docxfaithxdunce63732
 
Assignment DetailsDiscuss the followingWhat were some of .docx
Assignment DetailsDiscuss the followingWhat were some of .docxAssignment DetailsDiscuss the followingWhat were some of .docx
Assignment DetailsDiscuss the followingWhat were some of .docxfaithxdunce63732
 

More from faithxdunce63732 (20)

Assignment DetailsScenario You are member of a prisoner revie.docx
Assignment DetailsScenario You are member of a prisoner revie.docxAssignment DetailsScenario You are member of a prisoner revie.docx
Assignment DetailsScenario You are member of a prisoner revie.docx
 
Assignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docxAssignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docx
 
Assignment DetailsScenario You are a new patrol officer in a .docx
Assignment DetailsScenario You are a new patrol officer in a .docxAssignment DetailsScenario You are a new patrol officer in a .docx
Assignment DetailsScenario You are a new patrol officer in a .docx
 
Assignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docxAssignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docx
 
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docxAssignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
 
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docxAssignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
 
Assignment DetailsPart IRespond to the following.docx
Assignment DetailsPart IRespond to the following.docxAssignment DetailsPart IRespond to the following.docx
Assignment DetailsPart IRespond to the following.docx
 
Assignment DetailsPlease discuss the following in your main post.docx
Assignment DetailsPlease discuss the following in your main post.docxAssignment DetailsPlease discuss the following in your main post.docx
Assignment DetailsPlease discuss the following in your main post.docx
 
Assignment DetailsPennsylvania was the leader in sentencing and .docx
Assignment DetailsPennsylvania was the leader in sentencing and .docxAssignment DetailsPennsylvania was the leader in sentencing and .docx
Assignment DetailsPennsylvania was the leader in sentencing and .docx
 
Assignment DetailsPart IRespond to the followingReview .docx
Assignment DetailsPart IRespond to the followingReview .docxAssignment DetailsPart IRespond to the followingReview .docx
Assignment DetailsPart IRespond to the followingReview .docx
 
Assignment DetailsPart IRespond to the following questio.docx
Assignment DetailsPart IRespond to the following questio.docxAssignment DetailsPart IRespond to the following questio.docx
Assignment DetailsPart IRespond to the following questio.docx
 
Assignment DetailsPart IRespond to the following questions.docx
Assignment DetailsPart IRespond to the following questions.docxAssignment DetailsPart IRespond to the following questions.docx
Assignment DetailsPart IRespond to the following questions.docx
 
Assignment DetailsOne thing that unites all humans—despite cultu.docx
Assignment DetailsOne thing that unites all humans—despite cultu.docxAssignment DetailsOne thing that unites all humans—despite cultu.docx
Assignment DetailsOne thing that unites all humans—despite cultu.docx
 
Assignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docxAssignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docx
 
Assignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docxAssignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docx
 
Assignment DetailsIn this assignment, you will identify and .docx
Assignment DetailsIn this assignment, you will identify and .docxAssignment DetailsIn this assignment, you will identify and .docx
Assignment DetailsIn this assignment, you will identify and .docx
 
Assignment DetailsFinancial statements are the primary means of .docx
Assignment DetailsFinancial statements are the primary means of .docxAssignment DetailsFinancial statements are the primary means of .docx
Assignment DetailsFinancial statements are the primary means of .docx
 
Assignment DetailsIn this assignment, you will identify a pr.docx
Assignment DetailsIn this assignment, you will identify a pr.docxAssignment DetailsIn this assignment, you will identify a pr.docx
Assignment DetailsIn this assignment, you will identify a pr.docx
 
Assignment DetailsHealth information technology (health IT) .docx
Assignment DetailsHealth information technology (health IT) .docxAssignment DetailsHealth information technology (health IT) .docx
Assignment DetailsHealth information technology (health IT) .docx
 
Assignment DetailsDiscuss the followingWhat were some of .docx
Assignment DetailsDiscuss the followingWhat were some of .docxAssignment DetailsDiscuss the followingWhat were some of .docx
Assignment DetailsDiscuss the followingWhat were some of .docx
 

Recently uploaded

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

CPSC 120Spring 2014Lab 5Name _____________________.docx

  • 1. 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.
  • 2. 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";
  • 3. 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 _________________ _________________ _________________ _________________ _________________ _________________ _________________
  • 4. _________________ _________________ _________________ _________________ 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.
  • 5. 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?
  • 6. _____________________________________________________ ______________________________ 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
  • 7. 11 12 // Display the menu of choices 13 cout << "Choose a primary color by entering its number. nn"; 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 ___________________ ___________________
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. ___________________ ________ 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
  • 12. 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) LAB 5. 6 (20 points) –Programming Challenges of Chapter 4 # 9 Software Sales (Page 237) 1