Concept explainers
(Dangling-else Problem)C++ compliers always associate an else with the immediately preceding if unless told to do otherwise by the placement of braces ({and}). This behavior can lead to what is referred to as the dangling-else problem. The indentation of the nested statement
if (x > 5)
if (y > 5)
cout << “x and y are > 5”;
else
cout << “x is <=5”;
appears to indicate that if x is greater than 5, the nested if statement determines whether y is also greater than 5. If so, the statement outputs the sting “x and y are >5”. Otherwise, it appears that if x is not greater than 5, the else part of the if… else outputs the string “x is <=5”. Beware! This nested if… else statement does not execute as it appears. The complier actually interprets the statement as
if (x > 5)
if (y > 5)
cout << “x and y are >5”;
else
cout << “x is <=5”;
in which the body of the first if is a nested if… else. The outer if statement tests whether x is greater than 5. If so, execution continues by testing whether y is also greater than 5. If the second condition is true, the proper string - “x and y are > 5” - is displayed. However, if the second condition is false, the string, “x is <= 5” is displayed, even though we know that x is greater than 5. Equally bad, if the outer if statement’s condition is false, the inner if… else is skipped and nothing is displayed. For this exercise, add braces to the preceding code snippet to force the nested if… else statement to execute as it was originally intended.
Want to see the full answer?
Check out a sample textbook solutionChapter 4 Solutions
C++ How to Program (10th Edition)
- c++arrow_forwardC++ Programming Program Preface (Infix to Postfix) Write a program that converts an infix expression into an equivalent postfix expression. The rules to convert an infix expression into an equivalent postfix expression are as follows: Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows: a. Initialize pfx to an empty expression and also initialize the stack. b. Get the next symbol, sym, from infx. b.1. If sym is an operand, append sym to pfx. b.2. If sym is (, push sym into the stack. b.3. If sym is ), pop and append all of the symbols from the stack until the most recent left parentheses. Pop and discard the left parentheses. b.4. If sym is an operator: b.4.1. Pop and append all of the operators from the stack to pfx that are above the most recent left parentheses and have precedence greater than or equal to sym. b.4.2. Push sym onto the stack. c. After…arrow_forwardi need solution very very quickly please #data structure in c++# #the question below# The cafeteria in a university decided to make Black Friday, each student can take at most 3items from the cafeteria, if the student has an average greater than or equal 90, he can take all his itemsfor free and if the average between 60 and 89 the student can take 1 item only as free from his items,otherwise the student should pay for the items. before the cafeteria opens the door there are many andmany students waiting at the door, the guard decide to order the students about their averages in a priorityqueue. So the heist average comes first in the queue.Notes:- The student object consists of ID, Name, average and number of buying items between (0,3) items- The number of the students should be created randomly and must be less than 100 students- The number of buying items and the student average are created randomly.- The cafeteria has random number of items less than 200- The price of each item in…arrow_forward
- Compute the weakest precondition for each of the following assignment statements and postconditions. x = 2 * y -5; y = x + 2; {y < 2}arrow_forwardSynchronization (Social Distancing Problem) C++ multithreading At a playground, kids love to play on a play play structure. However, due to an ongoing pandemic, socialdistancing needs to be enforced. There are two types of kids—vaccinated, and non-vaccinated. The vaccinated kids have immunity against the virus hence don’t need social distancing. The non-vaccinated kids needs to maintain social distancing for their safety. The park managers came up with a solution that both the vaccinated and non-vaccinated kids cannot play at the play structure simultaneously.Below is a pseudocode for a solution using semaphores that allows any number of vaccinated kids on the play structure at the same time, but only one non-vaccinated kid. (All the kids are represented as threads.) If a non-vaccinated kid is playing at the structure, no other kid (either vaccinated or nonvaccinated) can be allowed. Any number of vaccinated kids can be at the play structure together. The solution must implement the…arrow_forwardI need the answer as soon as possiblearrow_forward
- -Python- Learning Objective: Use loops to iterate through the container Practice the enumerate() function to have simultaneous access to index and object Work with If statements to perform action only by specific condition Instructions You are parking in Los Angeles Airport and you can only remember the floor of where you parked your car. A programmer can help you find your car in the parking by only giving him the brand of your car. The program will give you the number of the parking lots that have cars with the same brand as yours. The program works as follows: Input from the user car_brand Loop over the cars in parking_cars list (hint: use enumerate) Save the indices in a list parking_lots of the cars with the same brand as the user's (car_brand) If parking_lots has values, print "Your car might be in any of these parking lots <parking_lots>". Otherwise, print "Sorry, your car is not on this floor." ex. input :Toyota / output: Your car might be in any of these parking lots…arrow_forwardQuestion 13 In C programming, construct an expression to express the following test condition: Tests that str1 is not equal to "xyz" and the length of str1 is less than or equal to 3arrow_forwardC Programming Language (Code With C Programming Language) Problem Title : Visible Trees There is a legend about a magical park with N × N trees. The trees are positioned in a square grid with N rows (numbered from 1 to N from north to south) and N columns (numbered from 1 to N from west to east). The height (in metres) of each tree is an integer between 1 and N × N, inclusive. Magically, the height of all trees is unique. Bunga is standing on the northmost point of the park and wants to count the number of visible trees for each Column. Similarly, Lestari is standing on the westmost point of the park and wants to count the number of visible trees for each Row. A tree X is visible if all other trees in front of the tree X are shorter than the tree X. For example, let N = 3 and the height (in metres) of the trees are as follows6 1 87 5 32 9 4 On the first column, Bunga can see two trees, as the tree on the third row is obstructed by the other trees. On the second column, Bunga can see…arrow_forward
- 58arrow_forwardLanguage C++ It has been observed by management that some faculty member at the university demonstrates a lackadaisical attitude toward teaching. They seldom go to class yet at the end of each month they receive full salary. Management has decided that GHs 200, 300, 400, and 500 will be deducted from a faculty’s salary if he/she offends once, twice, third time and forth time respectively in a month. This means that if a faculty member offends once in a month GHs 200 will be deducted, if a faculty member offends twice in a month, GHs 500 (i.e. 200+300) will be deducted, if a faculty member offends three times in a month GHs 900 (i.e. 200+300+400), will be deducted and if a faculty member offends four times in a month GHs 1,400 (i.e. 200+300+400+500). Assuming all faculty members are on a flat salary rate of GHs 2000. a. Write a program to request for the names of four faculty members and number times he/she has absented him/herself from class. One of them should have absented…arrow_forward4arrow_forward
- Np Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage