1) A program has a char variable gender that has been assigned a value. Write a
ID: 3627756 • Letter: 1
Question
1) A program has a char variable gender that has been assigned a value. Write a switch statement that outputs "Male" if the variable contains lower or upper case 'm', "Female" if it contains lower or upper case 'f', or "Invalid Input" for any other value.2) Create a complete C++ program that prompts the user to enter his or her height in inches and outputs the following:
Output "Height is six feet or more." when the user's height is 6 feet or more.
Output "Height is less than five feet." when the user's height is less than 5 feet.
Output "Height is at least five feet but less than 6 feet" otherwise.
3) Write a complete C++ console mode program (#includes, etc., but no prologue) that solves a simple DC circuit problem. Given a DC voltage source of some magnitude, find the voltage drops across the 2 resistors. The user will provide the values for the voltage source and the 2 resistor values. The formula that you will need is:
VRx = Vsupply * Rx / (Rx + Ry) // Voltage Divider rule
•Use appropriate data types.
•Valid input is any positive real number. If the user input is zero or negative, your program should print an error message and not try to compute any results.
•Be sure to display explanatory text to make the input and output clear to the user. Results should be output with 3 digits following the decimal point.
4) Create a C++ program that uses a while-loop to display the odd numbers between 15 and 30 including 15. The numbers should be displayed one per line.
5) 11. Which of the following expressions is correct if you want to end a while-loop when the character variable answer is anything other than the character 'y' in either upper or lower case?
A) while(answer == 'y' && answer == 'Y')
B) while(answer == "y" && answer == "Y")
C) while(answer == 'y' || answer == 'Y')
D) while(answer == "y" || answer == "Y")
6) Which answer identifies the looping construct(s) that would be best suited when a program must repeat a set of tasks until a specific condition occurs? It is possible that the set of tasks will not need to execute at all if the specific conditions exists initially. Furthermore, there is no need for specifying initialization or increment clauses to correctly iterate or terminate.
A) for (;condition;)
B) do while (condition)
C)while (condition) or
for(;condition;)
D) while (condition)
7) Which statement correctly tests char variable keepgoing for the upper or lower case letter A?
A) if(keepgoing = 'a' || keepgoing = 'A')
B) if(keepgoing = 'a' || 'A')
C) if(keepgoing == 'a' && keepgoing == 'A')
D) if(keepgoing == 'a' || keepgoing == 'A')
8) A program needs to output the cost of shipping a package based on the weight of the package. Information is available that associates specific weight ranges with specific costs. For example, a package weighing between 1 and 2 pounds costs $2.50 to ship. The best selection structure to use to program this situation is _______.
A) a SWITCH statement
B) multiple IF statements
C) nested IF statements
D) multiple IF ELSE statements
9) For readability, all statements inside a loop body should be
A) indented the same distance as the loop control statement.
B) indented by one additional tab stop more than the loop control statement.
C) surrounded by an open and closing parenthesis.
D) written on the same line.
10) A user inputs his name in response to a prompt for a numeric value. The subsequent cin statement tries to get a numeric value and store it into an integer variable. How does a program detect this input error and clear it?
A) The cin input stream is broken, the application must be restarted.
B) cin.fail() detects a failure and cin.clear() clears the failure.
C) Use cin.fail() to detect the failure and cin.ignore() to clear the failure.
D) Use cin.ignore() to detect and clear the failure.
Explanation / Answer
A program has a char variable gender that has been assigned a value. Write a switch statement that outputs "Male" if the variable contains lower or upper case 'm', "Female" if it contains lower or upper case 'f', or "Invalid Input" for any other value.
#include<iostream>
using namespace std;
int main()
{
char input;
cout << " Enter Gender " ;
cin >> input;
switch(input)
{
case 'm':
case 'M' : cout << " male ";
case 'f':
case 'F': cout << "female";
default: cout << "invalid input ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.