Question

In: Computer Science

1)Write a C++ program which clearly demonstrates how the dangling else ambiguity is resolved. Your program...

1)Write a C++ program which clearly demonstrates how the dangling else ambiguity is resolved. Your program should print distinct results depending on if C++ uses inner-if or outer-if resolution. Turn in a listing of your program, the output, and a description of how your program demonstrates the semantic resolution of the ambiguity.

2) Give a context-free grammar describing the syntax of the following; Non-empty strings of 0’s and 1’s that represent binary numbers that have odd parity

Solutions

Expert Solution

1)

Dangling else is an ambiguity which is created when there are 2 if statements followed by single else. So the compiler cannot understand which if the else belongs to, so that is called dangling else.

Demo of program output with inner and outer if separately.

#include <iostream>

using namespace std;

int main() {
int a;
//input a number
cout<< "\nEnter a number";
cin>>a;
cout << "\nDemo of dangling else, outer if";
if(a>5)
{
cout<<"\na is greater than 5";
if(a>100)
cout<<"\na is greater than 100";
}
else
cout<<"\na is less than 100";
//example 2
cout << "\nDemo of dangling else, inner if";
if(a>5)
{ cout<<"\na is greater than 5";
if(a>100)
cout<<"\na is greater than 1000";
else
cout<<"\na is less than 1000";
}
}

To get rid of dangling else, put braces for the compiler to know which if is being referred for the else.

Sample output


Related Solutions

Show that the following attempt to solve the dangling else ambiguity is still ambiguous. statement →...
Show that the following attempt to solve the dangling else ambiguity is still ambiguous. statement → if ( exp ) statement | matched-stmt matched-stmt → if ( exp ) matched-stmt else statement | other exp → 0 | 1
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Write a C++ program to computer the nth Fibonacci Number in 3 ways. Clearly define which...
Write a C++ program to computer the nth Fibonacci Number in 3 ways. Clearly define which way you are solving in your code using comments. You must provide an algorithm to solve for the nth Fibonacci Number in 1. a straight-forward recursive solution 2. a top-down memoized solution and 3. a bottom-up dynamic programming solution. These algorithms will be tested with randomly generated input, and a single non-negative number will be given through command line.
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
Describe how each of Algol 60, Pascal, and Pytho 3 handles the dangling else problem.
Describe how each of Algol 60, Pascal, and Pytho 3 handles the dangling else problem.
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a program driver that demonstrates that your additions work. Use the following sets as part...
Write a program driver that demonstrates that your additions work. Use the following sets as part of your demonstration A {1,3,8} B {2,3,5,10} C {4,6} show A && B A - B A || B A / B A && C A - C A || C A / C
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on...
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on page 108.  Use the format specified earlier (initial block of comments with TCC logo, name, etc) Display instructions so that the user understands the purpose of the program and what to enter. Display the results in increasing (non-decreasing) order. Run the program for the following 6 test cases.Turn in a printout of the program and printouts of the 6 test cases.(The result should...
Use c++language Use the pseudocode description below to write a program that uses an if, else...
Use c++language Use the pseudocode description below to write a program that uses an if, else if, else decision structure for a program that will determine if someone lives in Boston. 1. display message that describes what the program will do. 2. ask the user to input an answer to the question: Do you live in Boston?. 3. if they entered 'y', display a message confirming that they live in Boston. 4. if they entered 'n' , display a message...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT