1. Discuss an important problem with the following code. Run the program and check the output of the program
#include<stdio.h> int *zoo() { int x; x = 2; return &x; } int zoo1(){ int y=675; int z=90; return y; } void main() { int *x, *zoo(); x = zoo(); zoo1(); printf("%d", *x); }
Solution:
Dangling reference, zoo() returns a pointer to the stack frame which is popped when the function is terminated. A new function zoo1 is called. The address pointed to by x now contains different values.2. Consider the following C function:
void multiply (int m, int n) { m = m*n; cout <<m<< "," < <n<< endl; }
Suppose the function is called with actual parameters i,j where i=2,j=3. If we are using call-by-value show what is printed when called with:
(a) multiply (i,j) and (b) multiply (i,i).
Now suppose that the parameters could be replaced by call-by-value-result parameters. Repeat parts (a) and (b) and explain the different effect (if any). Assume that the instructions are not executed sequentially.
Solution:
Assuming the instructions are not executed sequentially: Using call-by-value: (a) "6,3" (b) "4,2" Using call-by-value-result: (a) "6,3" (b) "4,4" The different effect is that when calling using call-by-value- result, an alias is set up for the actual parameter, referred to via the formal parameter. So, in part b using call-by-value- result, two aliases are set up for the variable i (one referred to by m and one referred to by n), so when m is changed in the line "m=m*n" this effectively changes the value of i (because of the alias), which (because of the other alias) effectively changes n.
3. In what unusual place can you use a function call when a function returns a values by reference? (hint: function call is usually used at the right hand side of equal sign =). Give a simple example.
Solution: On the left hand side of the equals sign. Following is a simple example. #include<iostream.h int x; // global variable int& setx(); //function declaration void main() { // set x to a value, using setx()=92; // using function call on the left side cout <<"\nx=" <<x; //display new value in x } int& setx() { return x; // returns the value to be modified }
In this program the function setx() is declared with a reference type, int&, as the return type: int& setx(). The function returns the values x by : return x. Now though it looks strange, you can put a call to this function on the left side of the equals sign: setx()=92. The result is that the variable returned by the function is assigned the value on the right side of the equals sign. The output of the program is x=92.
4. (a) What is the principle reason for passing arguments by reference?
(b) Suppose a function has the following declaration: void person(char* name, int age); Modify the declaration so that default value for name is unknown and age 20.
Solution: (a)To modify the original argument. (b)void person(char* name="unknown", int age=20);
5. Write C program generated a weight conversion table using function. What is the difference between program with function and without function? Hint: the ratio of kilo/pound = 0.45359.
Solution:
main()
{ int us_pounds;
printf(" US
lbs UK st. lbs
INT Kg\n");
for(us_pounds=10; us_pounds < 250;
us_pounds+=10)
print_converted(us_pounds);
}
print_converted(pounds)
int pounds;
/* Convert U.S. Weight to Imperial and International
Units. Print the results */
{ int stones = pounds / 14;
int uklbs = pounds % 14;
float kilos_per_pound = 0.45359;
float kilos = pounds *
kilos_per_pound;
printf("
%3d %2d
%2d %6.2f\n",
pounds, stones, uklbs, kilos);
}
Defining a function here has made the program larger, but what have we gained? The structure has been improved, but this may make little difference to the readability of such a small program. In a larger program, such structuring makes the program shorter, easier to read, and makes future maintenance of the program easier. Another benefit of defining a function, is that the function can easily be re-used in another program.
6. Write a recursive function to calculate a power of a number.
Solution:
double power(val, pow) double val; unsigned pow; { if(pow == 0) return(1.0); else return(power(val, pow - 1) * val); }Notice that each of these definitions incorporate a test. Where an input value gives a trivial result, then it is returned directly, otherwise the function calls itself, passing a changed version of the input values.
7. Write a recursive function that prints out a table of Fibonacci numbers. Fibonacci numbers are defined by a series in which any element is the sum of the previous two elements.
Solution:
int fib(num) int num; /* Fibonacci value of a number */ { switch(num) { case 0: return(0); break; case 1: return(1); break; default: return(fib(num - 1) + fib(num - 2)); break; } }