Output writing function within function with array passing – CSKC
Sign In

Search

Solved Problem! #CPP#2746    siteicon   siteicon  

Problem Statement - Output writing function within function with array passing

Dry run the following program carefully and write the output.

TC++ #2746

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Solution


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]<<” “;


Share

Created: 17-Jan-2018 11:58 | Updated: 18-Jan-2018 00:12|