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

Lab 4.3 This lab introduces the logical operators AND, OR, and NOT in a menu dri

ID: 3842901 • Letter: L

Question

Lab 4.3

This lab introduces the logical operators AND, OR, and NOT in a menu driven application program.

Copy and paste the code below in a filename LastFirst_lab43.cpp (e.g. DoeJoe_lab43.cpp) and save it in Lab 4 folder.

Bring in the LastFirst_lab43.cpp program from the Lab 3 folder.

How could you rewrite gpa >= 2.0 in the first if statement using the NOT operator?

Could you replace year !='4' in the else if statement with year < 4 or year <= 3? Why or why not?

If you replace

if ( gpa >= 2.0 && year == '4') with

if ( gpa >= 2.0 || year == '4') and replace

else if ( year != '4'|| gpa < 2.0) with

else if ( year != '4' && gpa < 2.0)

which students will graduate and which will not graduate according to this new program? Does this handle all cases (i.e., all combinations of year and gpa)?

Could you replace else if ( year != '4'|| gpa < 2.0) with the single word else?

this introduces a logic error!

fix the logic error, and then

submit the revised LastFirst_lab43.cpp

The following is the code to be used:

// This program illustrates the use of logical operators

// PLACE YOUR NAME HERE

#include <iostream>

using namespace std;

int main()

{

char year;

float gpa;

cout << "What year student are you ?" << endl;

cout << "Enter 1 (freshman), 2 (sophomore), 3 (junior), or 4 (senior)" << endl << endl;

cin >> year;

cout << "Now enter your GPA" << endl;

cin >> gpa;

if (gpa >= 2.0 && year == '4')

          cout << "It is time to graduate soon" << endl;

     else if (year != '4'|| gpa <2.0)

          cout << "You need more schooling" << endl;

return 0;

}

Remember to submit your .cpp file

Explanation / Answer

This lab introduces the logical operators AND, OR, and NOT in a menu driven application program.
Copy and paste the code below in a filename LastFirst_lab43.cpp (e.g. DoeJoe_lab43.cpp) and save it in Lab 4 folder.
Bring in the LastFirst_lab43.cpp program from the Lab 3 folder.
Q)How could you rewrite gpa >= 2.0 in the first if statement using the NOT operator?
Ans) Negate the condition like below.

!(gpa < 2.0)

Q) Could you replace year !='4' in the else if statement with year < 4 or year <= 3? Why or why not?
Ans) Yes. We can replace year != '4' with year < 4 or year <= 3 in this context provided we are assuming that user will not enter any value greater than 4 or less than 1 bcause we have only four years available. If thats been the case then these new conditions hold good.

If you replace
if ( gpa >= 2.0 && year == '4') with
if ( gpa >= 2.0 || year == '4') and replace
else if ( year != '4'|| gpa < 2.0) with
else if ( year != '4' && gpa < 2.0)
which students will graduate and which will not graduate according to this new program? Does this handle all cases (i.e., all combinations of year and gpa)?
Ans) It will allow all year(1,2,3,4) students to graduate based on just the gpa score >= 2.0. Also will allow any 4th year student to graduate irrespective of their gpa value. That means even if gpa value is 0 and they are in 4th year, it will show "time to graduate soon".

It will not allow any 1,2,3 year student to graduate if their ga score is less than 2.0

Q) Could you replace else if ( year != '4'|| gpa < 2.0) with the single word else?
this introduces a logic error!
fix the logic error, and then
submit the revised LastFirst_lab43.cpp
Ans) I do not see any problem with replacing "if ( year != '4'|| gpa < 2.0)" with single word "else" assuming we will not allow any 1,2,3 year student unless they are in 4th year and score >= 2.0. Even though they are 3,2, 1 year students and score > 2.0 we are not allowing them to graduate as per the code so changing to else does not make any difference.