12,291,271 members (64,826 online)
Rate this:
See more:
Hi, may u forward me please in steps how this question get to this answer 7, 7 , 14 ?

many thanks.
```int cal (ref int x , ref int y , int z)
{
x = x + z + 2;
y = x + z + 3;
z=y;
return x;
}
int x = 2 , y = 4 , z = 1 ;
y = cal ( ref z , ref x , y);
```

OP's additional information copied from comment below
the teacher put this answers as:
A) 5, 6 , 4
B) 7, 7 , 14
C) 14 , 14 , 7
D) 7 , 7 , 7

it needs to know what's the value of x , y , z in output?
Posted 27-Nov-12 6:47am
Edited 27-Nov-12 11:37am
Nelek93.8K
v3
Sergey Alexandrovich Kryukov 27-Nov-12 12:56pm

Not a valid question. What steps? Do you need to input these values of x, y, z or calculate input to ensure required output? Ever learned elementary algebra of middle school grades?
--SA
lida zar 27-Nov-12 12:58pm

it was my exam question ... i couldn't solve it ... now i need to know how it solves? thanks
lida zar 27-Nov-12 13:00pm

i don't get to 7 , 7 , 14 ? ref doesn't work ok in my answers ... please help me to solve it?
Sergey Alexandrovich Kryukov 27-Nov-12 13:10pm

You did not even formulate the problem, did not ask my questions...
--SA
lida zar 27-Nov-12 13:04pm

the teacher put this answers as:
A) 5, 6 , 4
B) 7, 7 , 14
C) 14 , 14 , 7
D) 7 , 7 , 7

it needs to know what's the value of x , y , z in output?
lida zar 27-Nov-12 13:11pm

isn't any body here to explain me how it solve?? :(

Rate this:

## Solution 4

Y is always equal to X, so just pick that one

```private static int cal(ref int x, ref int y, int z)
{
Console.WriteLine("Function Start");

Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
x = x + z + 2;
Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
y = x + z + 3;
Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
z = y;
Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
Console.WriteLine("Function End\n");
return x;
}

private static void Main(string[] args)
{
int x = 2, y = 4, z = 1;
Console.WriteLine("x= {0};y={1};z={2}\n",x,y,z);
y = cal(ref z, ref x, y);
Console.WriteLine("x= {0};y={1};z={2}", x, y, z);

}```

```x= 2;y=4;z=1

Function Start
ref z= 1;ref x=2;y=4
ref z= 7;ref x=2;y=4
ref z= 7;ref x=14;y=4
ref z= 7;ref x=14;y=14
Function End

x= 14;y=7;z=7```
v3
lida zar 27-Nov-12 14:13pm

yeah bkoz of return x
Mathlab 27-Nov-12 15:55pm

The answer is 14,14,7 if they just want the function return values
Rate this:

## Solution 5

You are passing some parameters as reference, what it means everytime you assign a value inside the cal function, the value will be automatically redirected to the outside because of ref. So... the value of X at the first line is not the value of X on the second line, because referenced X has changed the value of the outside.

Another very important thing is what Richard MacCutchan already told you... pay attention to the order of the parameters
```outside X = inside Y outside Y = inside Z outside Z = inside X ```

spelling corrected[/edit]
v4
Rate this:

## Solution 1

Why not step through it with your debugger and you will be able to see exactly what happens. Notice also the order of variables passed in to method `cal`.
lida zar 27-Nov-12 13:07pm

i did but i don't know how computer Convert x to 7 , y to 7 and z to 14 ? i need its step otherwise i know whats its output ... i need its analysis ...
Sergey Alexandrovich Kryukov 27-Nov-12 13:12pm

How come you don't? It's written in front of you. If you don't understand even this, how would you hope a pass? By cheating (sorry, but...)?..
--SA
Richard MacCutchan 27-Nov-12 13:17pm

Well if you step through it one instruction at a time you can clearly see the initial values, the formulas, and the final values; it's simple mathematics. As I said before, look at the ordering of the variables in the call to the `cal` method and the names used within it.
lida zar 27-Nov-12 13:20pm

the point is in cal method or ref?
Richard MacCutchan 27-Nov-12 14:09pm

The variables passed in to cal are: z, x, y, but inside cal they are called x, y, z. The order is important.
lida zar 27-Nov-12 13:24pm

couldn't understand :(
lida zar 27-Nov-12 13:37pm

is it true richard??
x = 1 +4 + 2 > x=7
y = 7+4+3 > y=14
and how get z ... z= y??? z will be 14 or 7 ?

lida zar 27-Nov-12 13:39pm

is it true richard ?

x = 1 + 4 + 2 > x=7
y = 7 + 4 + 3 > y = 14
what about z = y ??

help me plz dear richard ?? i really need it very much
lida zar 27-Nov-12 13:42pm

yes you are right ?? but order is also important ...
look is it true ?
x = 1 + 4 + 2 > x = 7
y = 7 + 4 + 3 > y = 14
lida zar 27-Nov-12 13:18pm

y u discuss friend with me ... look how i did
1) x = 2 + 1 + 2 > x=5
2) y = 2 + 1 + 3 > y =6
3)z=y > z=6
i don't understand it ... if u can help me otherwise not need to discuss about my information ... offffo
Rate this:

## Solution 3

int x = 2 , y = 4 , z = 1 ;
y = cal ( ref z , ref x , y); .....caling Cal(ref 1, ref 2, 4)

int cal (ref int x , ref int y , int z) ......(ref 1,ref 2, 4)
{
x = x + z + 2; .......x = 1 + 4 + 2; now x is 7 which reference Z
y = x + z + 3;........y= 7 + 4 + 3; now y is 14 which refers X
z=y;......................z is 14 wich ref y;
return x;....... return 7
}

y = cal ( ref z , ref x , y); ....assign returned value to y which is 7

I change the function paramaters this way:-

```int x = 2 , y = 4 , z = 1 ;
y = cal( ref z , ref x , y);

int cal(ref int z, ref int x, int y)
{
z = z + y + 2;
x = z + y + 3;
y = x;
return z;
}```

The result are:
X=14
Y=7
Z=7
v5
lida zar 27-Nov-12 13:48pm

thanks very much HabeshaMan :)
lida zar 27-Nov-12 14:01pm

lida zar 27-Nov-12 14:03pm

z=14
x=7
y=7 its right?
HabeshaMan 27-Nov-12 17:07pm

X=14 , y=7 ,z=7
Mathlab 27-Nov-12 16:01pm

Incorrect
lida zar 28-Nov-12 1:16am

according my main question the answer is :
X=7 , y = 7 , z= 14 as i think x and y is same anyways ... so dear habeshaman x never can't be 14? what's ur idea friends? am i right or not?
HabeshaMan 28-Nov-12 11:10am

x is 7 inside the function, means 7 assigned to outside Z.
etc
http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx
Mathlab 28-Nov-12 10:27am

let me make it simple x is never 7 at any point in the calculation
lida zar 28-Nov-12 12:26pm

got it now .... thanks anyways from anyone which helped me to understand ref .
Rate this:

## Solution 2

It changes the value on calling the calc function.Nothing to do with order.
I think it has something to do with passing by ref.
when it calls the cal method it has value with x=1,y=2 & z=4.

I have no idea why it changes the values for x,y,z.
Richard MacCutchan 27-Nov-12 14:07pm

Because the variables passed in to the function are z, x, y but are referred to inside the function as x, y, z.

Top Experts
Last 24hrsThis month
 KARTHIK Bangalore 355 OriginalGriff 280 Sergey Alexandrovich Kryukov 198 ppolymorphe 155 Mehdi Gholam 144
 OriginalGriff 8,110 ppolymorphe 4,987 CHill60 4,940 KARTHIK Bangalore 4,347 Sergey Alexandrovich Kryukov 4,233