Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi, may u forward me please in steps how this question get to this answer 7, 7 , 14 ?

many thanks.
C#
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);


[Edit]Code block added[/Edit]

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?
please please help me ... tomorrow i have again C# exam ... thanks
Posted
Updated 27-Nov-12 11:37am
v3
Comments
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?
please please help me ... tomorrow i have again C# exam ... thanks

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

C#
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);

        }


CSS
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
 
Share this answer
 
v3
Comments
lida zar 27-Nov-12 14:13pm    
yeah bkoz of return x
[no name] 27-Nov-12 15:55pm    
The answer is 14,14,7 if they just want the function return values
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
<br />
outside X = inside Y<br />
outside Y = inside Z<br />
outside Z = inside X<br />


[edit]spelling corrected[/edit]
 
Share this answer
 
v4
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.
 
Share this answer
 
Comments
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.
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:-

C#
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
 
Share this answer
 
v5
Comments
lida zar 27-Nov-12 13:48pm    
thanks very much HabeshaMan :)
lida zar 27-Nov-12 14:01pm    
the last answer about x,y,z is ?
lida zar 27-Nov-12 14:03pm    
z=14
x=7
y=7 its right?
_Natula 27-Nov-12 17:07pm    
X=14 , y=7 ,z=7
[no name] 27-Nov-12 16:01pm    
Incorrect
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.
 
Share this answer
 
Comments
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900