Macro definition for array filling – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Exam Questions-CBSE12A-2017-01D #CPP#5304    siteicon   siteicon  

Problem Statement - Macro definition for array filling

Find and write the output of the following C++ program code:
Note: Assume all required header files are already included in the program.

#define Diff(N1,N2) ((N1>N2)?N1-N2:N2-N1)
void main()
  int A,B,NUM[] = {10,23,14,54,32};
  for(int CNT =4; CNT>0; CNT--)



TC++ #5304

Run Output



#define Diff(N1,N2) ((N1>N2)?N1-N2:N2-N1)
This macro definition will give difference of N1 and N2 by using the ternary operator. If N1 is greater than N2, N1-N2 will be returned else N2-N1 will be returned

void main()
int A,B,NUM[] = {10,23,14,54,32}; //Integer A, B and a 5 member integer array initialized
for(int CNT =4; CNT>0; CNT–) //This loop will run 4 times only
A=NUM[CNT];  //Will begin from Last member of array and then reducing further
B=NUM[CNT-1]; //This will be Last but one member and then reducing further

cout<<Diff(A,B)<<‘#’; //This will be difference of 5th and 4th, then 4th and 3rd then 3rd and 2nd and then 2nd and 1st member of array. So the output would be 22#40#9#13#

Common Errors

  • Learners can commit mistake in finding the loop count as it is one less in this case.
  • Learners also need to take care that loop is being traversed in reverse direction.


CSKC| Created: 5-Dec-2018 | Updated: 5-Dec-2018|CBSE12A-2017

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues