Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Problem Statement - Output writing function within function with array passing
Dry run the following program carefully and write the output.
TC++ #2746
Solution
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 x[i]=y++;
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]<<” “;