In: Computer Science
// Lab Homework 11
// Logical Operator AND and OR
// Demonstrate how to use a logical operator
// with a Boolean expression.
#include <iostream>
#include <cctype> // for tolower()
using namespace std;
int main()
{
int numberOfCreditHours; // Number of credit hours
student has completed
float gpa; // The student's cumulative grade point
average
// Ask the user two questions:
cout << "Answer the following questions:"
<< endl << endl;
cout << "How many credit hours have you
completed (0-200): ";
cin >> numberOfCreditHours;
cout << "What is your cumulative GPA
(0.00-4.00)? ";
cin >> gpa;
cout << endl;
// TODO: Students can petition for graduation
if:
// - they have completed 48+ credit hours
// - they have a GPA of 2.0 or greater
//
// Use Logical operators in complex logical
expressions to decide which of the following
// four messages to print:
//
// 1) You may petition for graduation.
// 2) You must raise your GPA before you can
graduate.
// 3) You must complete more credit hours before you
can graduate.
// 4) You must raise your GPA and complete more
credits before graduating.
//
// Use just ONE of the logical operators below in the
expression
// to choose the approprate message. You MAY use the
chosen operator
// more than
once.
//
// Logical AND: &&
// Logical OR: ||
//
// Note: Do NOT worry about situations where the user
doesn't enter Y, y, N, or n
// Use the if structure provided below.
if // (<write a compound logic expression here
for graduating>)
//graduation message
else if // (<write a compound logic expression
here for too low gpa>)
//raise gpa message
else if // (<write a compound logic expression
here for too few credits>)
//complete more credits message
else // else is good enough, no logic expression
needed - it's the only other possibility
// raise gpa and earn more credits
message
cout << endl << endl;
return 0;
}
/* Sample interaction and output:
Test #1
==========================================
Answer the following questions:
How many credit hours have you completed (0-200): 60
What is your cumulative GPA (0.00-4.00)? 3.75
You may petition for graduation.
Press any key to continue . . .
Test #2
==========================================
Answer the following questions:
How many credit hours have you completed (0-200): 60
What is your cumulative GPA (0.00-4.00)? 1.99
You must raise your GPA before you can graduate.
Press any key to continue . . .
Test #3
==========================================
Answer the following questions:
How many credit hours have you completed (0-200): 47
What is your cumulative GPA (0.00-4.00)? 3.25
You must complete more credit hours before you can graduate.
Press any key to continue . . .
Test #4
==========================================
Answer the following questions:
How many credit hours have you completed (0-200): 45
What is your cumulative GPA (0.00-4.00)? 1.75
You must raise your GPA and complete more credits before graduating.
Press any key to continue . . .
*/
If you have any doubts, please give me comment...
// Lab Homework 11
// Logical Operator AND and OR
// Demonstrate how to use a logical operator
// with a Boolean expression.
#include <iostream>
#include <cctype> // for tolower()
using namespace std;
int main()
{
int numberOfCreditHours; // Number of credit hours student has completed
float gpa; // The student's cumulative grade point average
// Ask the user two questions:
cout << "Answer the following questions:" << endl
<< endl;
cout << "How many credit hours have you completed (0-200): ";
cin >> numberOfCreditHours;
cout << "What is your cumulative GPA (0.00-4.00)? ";
cin >> gpa;
cout << endl;
// TODO: Students can petition for graduation if:
// - they have completed 48+ credit hours
// - they have a GPA of 2.0 or greater
//
// Use Logical operators in complex logical expressions to decide which of the following
// four messages to print:
//
// 1) You may petition for graduation.
// 2) You must raise your GPA before you can graduate.
// 3) You must complete more credit hours before you can graduate.
// 4) You must raise your GPA and complete more credits before graduating.
//
// Use just ONE of the logical operators below in the expression
// to choose the approprate message. You MAY use the chosen operator
// more than once.
//
// Logical AND: &&
// Logical OR: ||
//
// Note: Do NOT worry about situations where the user doesn't enter Y, y, N, or n
// Use the if structure provided below.
if (numberOfCreditHours > 48 && gpa >= 2.0) // (<write a compound logic expression here for graduating>)
//graduation message
cout << "You may petition for graduation." << endl;
else if (numberOfCreditHours > 48 && gpa < 2.0) // (<write a compound logic expression here for too low gpa>)
//raise gpa message
cout << "You must raise your GPA before you can graduate." << endl;
else if (numberOfCreditHours <= 48 && gpa > 2.0) // (<write a compound logic expression here for too few credits>)
//complete more credits message
cout << "You must complete more credit hours before you can graduate." << endl;
else // else is good enough, no logic expression needed - it's the only other possibility
// raise gpa and earn more credits message
cout << "You must raise your GPA and complete more credits before graduating." << endl;
cout << endl
<< endl;
return 0;
}
nagarajuanagaraju-Vostro-3550:26092019$ g++ lab hw11.cpp nagarajuanagaraju-Vostro-3550:26092019$ ./a.out, Answer the following questions: How many credit hours have you completed (0-200): 60 What is your cumulative GPA (0.00-4.00)? 3.75 You may petition for graduation. nagarajuanagaraju-Vostro-3550:26092019$ ./a.out Answer the following questions: How many credit hours have you completed (0-200): 60, What is your cumulative GPA (0.00-4.00)? 1.99 You must raise your GPA before you can graduate. nagaraju@nagaraju-Vostro-3550:26092019$ /a.out, Answer the following questions: How many credit hours have you completed (0-200): 47 What is your cumulative GPA (0.00-4.00)? 3.25 You must complete more credit hours before you can graduate. nagarajuanagaraju-Vostro-3550:26092019$ ./a.out, Answer the following questions: How many credit hours have you completed (0-200): 45 What is your cumulative GPA (0.00-4.00)? 1.75 You must raise your GPA and complete more credits before graduating.