HW2(2) 6. Suppose that you have the following variable declarations: int x = 10;
ID: 3627222 • Letter: H
Question
HW2(2)6. Suppose that you have the following variable declarations:
int x = 10;
int y = 15;
int z = 20;
Determine whether the following expressions are true or false.
a. !(x < 10)
b. x <= 5 || y > 15
c. (x != 5) && (y == z)
d. x <= z && (x + y >= z)
7. What is the output of the following code fragment?
int x = 100;
int y = 200;
if (x > 100 && y <= 200)
cout << x << " " << y << " " << x + y << endl;
else
cout << x << " " << y << " " << 2 * x - y << endl;
8. Given a char variable called gender, write C++ statements to output "Male" if gender contains upper or lower case M, "Female" if it contains upper or lower case F, or "Invalid Gender" if it contains anything else. Use if / else if statements.
9. Given a char variable called gender, write C++ statements to out "Male" if gender contains upper or lower case M, "Female" if it contains upper or lower case F, or "Invalid Gender" if it contains anything else. Use a switch statement.
10. What is the value of the beta variable after the following code executes?
int beta = 3;
switch(beta)
{
case 3:
beta += 3;
case 1:
beta++;
break;
case 5:
beta += 5;
case 4:
beta += 4;
}
Explanation / Answer
sorry about 9 it is correct now 8 is with if statment and 9 with seitch cases
a)
true
false
false
true
7)
100 200 0
8)
char gender;
cin>>gender;
if(gender=='M' || gender== 'm')
cout<<"Male"<<endl;
else if(gender=='F' || gender== 'f')
cout<<"Female"<<endl;
else
cout<<"Invalid Gender"<<endl;
9) char gender;
cin>>gender;
switch(gender)
{
case 'M':
case'm':
cout<<"Male"<<endl;
break;
case'F':
case'f':
cout<<"female"<<endl;
break;
default :
cout<<"Invalid Gender"<<endl;
}
10)
beta = 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.