Output writing – Function – pass by reference – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Exam Questions-CBSE12A-2018-01D #CPP#5197    siteicon   siteicon  

Problem Statement - Output writing – Function – pass by reference

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

void Revert(int &Num, int Last=2)
{
  Last=(Last%2==0)?Last+1:Last-1;
  for(int C=1; C<=Last; C++)
  Num+=C;
}

void main()
{
  int A=20,B=4;
  Revert(A,B);
  cout<<A<<"&"<<B<<endl;
  B--;
  Revert(A,B);
  cout<<A<<"#"<<B<<endl;
  Revert(B);
  cout<<A<<"#"<<B<<endl;
}

Solution

TC++ #5197

Run Output

35&4
38#3
38#9

Notes

Revert(A,B); //This will add 1,2,3,4,5 successively to number A making it 21,23,26,30 and 35 as per logic since B is 4 and it will allow loop to run for 5 times as Last will become 5
cout<<A<<“&”<<B<<endl; // This will print 35&4 as per logic

B–; //B reduced to 3 after this instruction
Revert(A,B); //This will add 1,2 successively to number A which is now 35 (as A is passed by reference) making it 36,38 as per logic since B is 3 and it will allow loop to run for 2 times as Last will become 2

cout<<A<<“#”<<B<<endl;
 // This will print 38&3 as per logic

Revert(B); //This will add 1,2,3 successively to number B making it 4,6,9  as per logic since second param will take default value of 2 and it will allow loop to run for 3 times as Last will become 3
cout<<A<<“#”<<B<<endl; // This will print 38#9 as per logic

Common Errors

  • Student often forget that the number is being added successively to initial number and not at once.


Share

CSKC| Created: 3-Dec-2018 | Updated: 4-Dec-2018|CBSE12A-2018






×
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


Back