Jump to content
Urch Forums

Do you understand param. pass <- more (2)


AlbaLed

Recommended Posts

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!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...