Output Writing – Pixel structure passed by reference – Computer Sir Ki Class
  

Login
[lwa]



Solved Problem! #CPP#2987 siteicon   siteicon   siteicon  

Problem Statement - Output Writing – Pixel structure passed by reference

Write down the output of the program and give steps in arriving at such output.

TC++ #2987

#include <iostream.h>
#include <conio.h>
struct Pix {int x,y;};

void show(Pix &p)
{
cout<<p.x++<<","<<++p.y<<endl;
}

int main()
{
clrscr();
Pix P={50,60},Q,R;
R=P;
show(R);
P.x+=10;P.y+=30;
Q=R;
show(P); show(Q); show(R);
getch();
return 0;
}
#include <iostream>
using namespace std;
struct Pix {int x,y;};

void show(Pix &p)
{
cout<<p.x++<<","<<++p.y<<endl;
}

int main()
{
Pix P={50,60},Q,R;
R=P;
show(R);
P.x+=10;P.y+=30;
Q=R;
show(P); show(Q); show(R);
return 0;
}

Test it !

Solution


Run Output

50,61
60,91
51,62
51,62

Steps in arriving at output:

Pix P={50,60},Q,R; 
Here P will contain P.x=50 and P.y=60. Q.x,Q.y and R.x and R.y will have indeterminate values.

R=P;
Here R.x and R.y will have values same as P.x and P.y so they will be 50 and 60 respectively.

show(R);
Since value of y has a pre-increment so the value printed would be 50,61

P.x+=10;P.y+=30;
Now P.x will become 50+10 = 60 and P.y will become 60+30=90.

Q=R;
Here values of R.x and R.y will be copied to Q.x and Q.y so they will become 51 (due to previous post increment) and 61 as fixed earlier.

show(P);
This should print 60 and 91 (due to pre-increment in show)

show(Q);
This should print 51 and 62 due to pre increment in show

show(R);
This should also print 51 and 62 again to due to pre-increment in show.


Share

sunmitra| Created: 24-Jan-2018 | Updated: 24-Jan-2018|






Back