Output writing function within function with array passing – Computer Sir Ki Class


Lost your password?

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


Solved Problem! #CPP#2746 siteicon   siteicon   siteicon  

Problem Statement - Output writing function within function with array passing

Dry run the following program carefully and write the output.

TC++ #2746


Run Output

65 11 65 13 65

Let us begin by function which is called first in the main()

void fn2(int x[],int y=10) //Here array initialised in main with 1,2,3,4,5 is called by reference
for(int i=0;i<5;++i) //Fixed traversing size of 5 has been given
value of x[i] after each iteration would be 10,11,12,13,14 as the y would post increment.

fn1(x);  //Now we shall understand fn1 and then new values.

void fn1(int p[],char q=’A’) //another function which is called from fn2. It can be directly called in fn2 as it has been written above it and its reference would be available to fn2. Here function seems to again pass the same array by reference and a character variable q with default value ‘A’ has been passed.
for(int i=0;i<5;++i) //5 iterations will happen

if(p[i]%2==0) p[i]=q;
This will test divisibility by 2 and which ever member is divisible by 2 will be replaced by value of q. Since the value of q is ‘A’ it will get filled as numeric value 65 (ascii code of A) because the target array is an integer array. So after each iteration of array which now has 10,11,12,13,14 all the even numbers will be replaced by 65. The array will now become
65 11 65 13 65

The output will be printed in the main routine using the following line.
for(int i=0;i<5;i++) cout<<a[i]<<” “;


sunmitra| Created: 17-Jan-2018 | Updated: 18-Jan-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