Click here to Skip to main content
12,291,271 members (64,826 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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);

[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 27-Nov-12 6:47am
Edited 27-Nov-12 11:37am
Nelek93.8K
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
lida zar 27-Nov-12 13:11pm
   
isn't any body here to explain me how it solve?? :(
Rate this: bad
 
good
Please Sign up or sign in to vote.

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
  Permalink  
v3
Comments
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: bad
 
good
Please Sign up or sign in to vote.

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


[edit]spelling corrected[/edit]
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
  Permalink  
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.
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 ?

please help me dear richard to solve it ..plz??
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
what about z > z =y ?? i don't know its right way im going or not please help me dear richard??
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: bad
 
good
Please Sign up or sign in to vote.

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
  Permalink  
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?
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
   
Incorrect,Please refer Nelek solution;
x is 7 inside the function, means 7 assigned to outside Z.
etc
Here is the link to rean about Passing Parameters
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: bad
 
good
Please Sign up or sign in to vote.

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160518.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100