Q.Rewrite the following C++ program after removing any/all syntactical error(s). Underline each correction done in the code: Note : Assume all required header files are already included in the program. #define Area(L,B) = L*B structure Recta { int Length,Breadth; }; void main() { Recta R = [10,15]; cout<<Area(Length.R,Breadth.R); }
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The code has five syntactical errors: an incorrect macro definition with =, wrong structure keyword, invalid aggregate initialization syntax, incorrect member access order, and wrong main() return type. The corrected program compiles and runs properly.
This is a debugging exercise that tests your understanding of C++ syntax across macros, structures, initialization, and member access. Each error violates a specific rule of C++ grammar.
The original code attempts to define a macro for calculating area, declare a structure for a rectangle, create an instance, and print the area. However, it contains multiple syntax violations that prevent compilation.
Errors and Corrections
-
Macro definition with assignment operator
The line
#define Area(L,B) = L*Bis incorrect because preprocessor macros don't use=. A macro definition directly replaces tokens.Correction: <u>
#define Area(L,B) L*B</u> -
Wrong keyword for structure
C++ uses
struct, notstructure.Correction: <u>
struct Recta</u> -
Invalid initialization syntax
The line
Recta R = [10,15];uses square brackets, which is not valid for structure initialization in C++. Aggregate initialization requires curly braces.Correction: <u>
Recta R = {10,15};</u> -
Incorrect member access syntax
Length.Rreverses the correct order. The object name comes first, then the dot operator, then the member name.Correction: <u>
R.Length</u> and <u>R.Breadth</u> -
Wrong return type for
main()In standard C++,
main()must returnint, notvoid. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.