AlbaLed Posted November 1, 2003 Share Posted November 1, 2003 suppose the following C program int x = 3 ; // global var void fun(int a, int b) { x = b; } void main() { int list[10]; list[x] = 5; fun(x, list[x]) printf("x = %d", x) } What is printed when parameters are passed by 1. Value 2. Name 3. Reference 4. Value-result 2. consider the following C-like code float sum(float adder, int index, int length) { float tempSum = 0.0; for(index = 0; index tempSum = tempSum + adder; return tempSum; } Assume pass by name what is the returned by sum in the folloing cases a. sum(A, I, 100), where A & I are scalar b. sum(A[i]*A[i], I, 100) where A is an array of 100 elems, & I is int b. sum(A[i]*B[i], I, 100) where A & B is an array of 100 elems, & I is int[/color] There are no errors in the above code !!! 3. Describe any method you know for subrograms to access non-local variables. 4. For small arrays is it better to use static or dynamic allocation? Why? Now that we've discussed caches, page faults give a mature answer, otherwise you get a 0. Enjoyy!! Quote Link to comment Share on other sites More sharing options...
wood Posted November 1, 2003 Share Posted November 1, 2003 I believe you mean a = b; on the fun() function right? If so, 1. Value 3 2. Name 5 3. Reference 5 4. Value-result 5 Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 1, 2003 Author Share Posted November 1, 2003 I knew someone would get 4 wrong :D, pass-by-value-result (pbvr)does not always = pass-by-reference. This is what happens in pbvr fun(x, list[x]) space for a is allocated space for b is allocated addr_x = addr of x add_list_x = addr of list[x] Copying param values a = x = 3 b = list[x] = list[3] = 5 function execution x = b = 5 function done, now we have to copy params back put a=3 in addr_x put b=5 in addr_list_x therefore x is still going to be 3. It was tricky that's why I posted:D Quote Link to comment Share on other sites More sharing options...
wood Posted November 1, 2003 Share Posted November 1, 2003 AlbaLed... I assumed that you have a=b on the fun(). What's that 'i' in i=b=5 ?? Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 1, 2003 Author Share Posted November 1, 2003 should have been x, updated Quote Link to comment Share on other sites More sharing options...
wood Posted November 1, 2003 Share Posted November 1, 2003 Ok. My answer was related to fun() having a=b inside. I thought you had made a typo there specifying x=b; Good one! Wood Quote Link to comment Share on other sites More sharing options...
wood Posted November 2, 2003 Share Posted November 2, 2003 a. sum(A, I, 100), where A & I are scalar [color=Red]100*A[/color] b. sum(A[i]*A[i], I, 100) where A is an array of 100 elems, & I is int [color=Red]Sum of the squares of each element of A. A[0]^2+A[1]^2+...+A[99]^2[/color] b. sum(A[i]*B[i], I, 100) where A & B is an array of 100 elems, & I is int [color=Red]Result of the product of matrix A(1,100) and B(100,1) :) A[0]*B[0]+A[1]*B[1]+...+A[99]*B[99][/color]? Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 2, 2003 Author Share Posted November 2, 2003 A+ wood, it seems like u've done some homework lately :p. We need some more of this kind because after all it is a comp. sci. GRE, and I bet you there is going to be some of these stuff there. Here is another one for you wood. What is the difference between separate and independent compilation? Why are they useful? Give an example of at least 1? Quote Link to comment Share on other sites More sharing options...
wood Posted November 2, 2003 Share Posted November 2, 2003 I believe separate compilation is the compilation we do in Pascal or C/C++, where we create libraries and header files for other modules to compile/link and do some basic type checking for consistency. Independent compilaton is done in languages like Fortran, Assembly, PL/1, which there is no way of sharing information between modules at compile time. ? Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 2, 2003 Author Share Posted November 2, 2003 Separate compilation is compilation at different times but is not independent if either access of use of entities of the other parts are needed. Ada, Fortran 90, modula-2 provide this. The info needed is provided by building libraries. Independent compilation is when program units are compiled without information about any other program units. C, fortran77 provide this. No interface checking is done at all. Fortran77 does none anyways. Not sure what happens with C. I think C does not do too much interface checking either, consider printf() for example. I am not sure tho. you have any idea? Quote Link to comment Share on other sites More sharing options...
wood Posted November 2, 2003 Share Posted November 2, 2003 Not really... I'll search for that once I get a chance... Quote Link to comment Share on other sites More sharing options...
nonevent99 Posted November 2, 2003 Share Posted November 2, 2003 I hate to be the little boy in the Emperor's New Clothes story, but isn't the "pass by value" answer to Q1 = 5? int x = 3 ; // global var void fun(int a, int b) { x = b; } void main() { int list[10]; list[x] = 5; fun(x, list[x]) printf("x = %d", x) } Quote Link to comment Share on other sites More sharing options...
wood Posted November 2, 2003 Share Posted November 2, 2003 Yes, nonevent99... As I mentioned before, my answers were gathered on the belief that AlbaLed had made a typo and fun() should have been "a=b;". Wood Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.