Logical Operators

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?
Submit the revised LastFirst_lab43.cpp

The following is the code to be used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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;

}
We're not going to do your homework for you.

If you're too lazy to ask an actual question and resort to simply copy/pasting the assignment description, then I'm too lazy to help.
How could you rewrite gpa >= 2.0 in the first if statement using the NOT operator?
1
2
3
4
if(!(gpa <= 2.0 && year != 4))
	{
		std::cout << "Graduated!\n";
	}


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

Yes you could, but because year is a char, you'll be comparing the ASCII value of it. That makes no difference though. You can do this because something that does not equal four HAS to be either greater or less than it. In your case you'd check for something less than. And I'd actually recommend doing this because of the case where year is greater than 4(it'd still turn out true then because it doesn't equal '4').

Hope this helped. You should be able to figure out the rest on your own. =D
Wow...
I'd probably delete that post asap.
Last edited on
That was meant for Disch. you posted while i did, so i didnt see your help, thanks homie, im gonna try that.
Yes I know. That's why you should delete it
Why do you think I should delete that?
I figured it'd speak for it self. Regardless he already saw it probably by now. Anyways it's better to actually ask a question, not C/P. I personally don't care, but most other people do. It shows that you actually want to learn what you're asking and not just looking to have someone do the assignment for you.
I served in the marine corps for 15 years and was deployed 3 times including a 30 month tour in Iraq. I've been busy with my kids and I have an assignment due. There's a break where I'll be able to catch up with what I've missed and don't understand, but until then, I need to turn something in. That's all. Just looking for a little bit of help.
Thanks man. I sent you an email and you can respond with your actual email this time.
The answer to the third question is now the students only have to meet one of the two requirements to graduate. The logical or operator only requires one of the two to be true for it all to be true. Also the only students who will not graduate are those whose gpa is less than 2.0 and whose year is not equal to 4. Some problems arise from this though... The else if statement sometimes won't even get executed when it *should* be. This is because the if statement is checking to see if gpa is greater than 2.0 or year is equivalent to 4. What if the gpa is 1.0 but the year is 4? According to the else if statement he should NOT graduate. However he still will because in that case the first if evaluates to true and the else if never gets executed.

To insult a marine?

Because Disch knew that Phaxen was a Marine, right?
A very angry american with access to guns. Talk about living up to the stereotype.
Topic archived. No new replies allowed.