Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. What if we wrote the increment procedure in C as follows: void inc (int *x) #x++ ; // this used to be (x) in
1. What if we wrote the increment procedure in C as follows: void inc (int *x) #x++ ; // this used to be (x) in the book Will this code compile without an error? If so, what does this procedure do and if not then what is the error? 2. (a) Consider the intswap C+ procedure void intswap (int& x, int& y) int t- x; Is this procedure in closed form Give an explanation for your answer. (b) Next consider the plymorphic swap procedure in C++: template void swap (T& x, T& y) T temp x y temp; Is this procedure in closed form? Give an explanation for your answer. 3. Suppose we tried to write a C procedure that would initialize an array to a certain size as follows: void int (int al ], int size) a - (int) malloc (sizeof (int)); Will this work? Provide your reasoning 4. Give the output of the following program using the four parameter passing methods. int i; it a[3]; void swap(int x, int y) =x-y; int main) a[0] = 2; al-1; a[2]0; swap(i, ali]); printf ("%d %d %d %d ", , swap (ali], ai]); printf("%d %d %d ", a[0], return 0; a[0], a[1], a[2]); a[1], a[2]); 5. Draw the stack of activation records for the following C program after the call to procedure r on line 16. Show the control links andll in the local names in each activatin record. (1) int x; (2) void p(void) double r-2; printf("gln", r) Iscientific format printf ("%d ", x); (8) void r(voiud) (10) int 1; (12) (13) void q(void) (14) (15) (16) (17) (18) (19) double x3; rO; pO; // continued on next page (20) int main) (21) (22) (23) (24) (25) pO; q0; return 0 Extra Credit FORTRAN has the convention that all parameter passing is by reference. Neverthe- less, as decribed in the chapter, it is possible to call subroutines with nonvariable expressions as arguments, as in CALL P(X, X+Y, 2) (a) Explain how it is possibel to pass a variable X by value in FORTRAN. 6. (b) Suppose that subroutine P is declared as follows: SUBROUTINE P(A) INTEGERA PRINT *, A A A1 RETURN END This subroutine can be called from the main program using the statement: CALL P(1) In some FORTRAN systems, this will cause a runtime error. In others, no runtime error occurs, but if the subroutine is called a second time with 1 as its argument, it may print the value 2. Explain how both behaviors might occur
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started