SOLUTION
60-140-01/02
Fall 2015: In-class Exercise for Class Participation
(Oct 19/20, 2015)
Also Good as a Mock Midterm exercise. Solution will be
given later, but first, trace through the following program and answer the
questions following, and hand in your paper with your name.
Student Name and
id:___________________________________________________________
Lab Section(circle
one): Lab 51, 52, 53, 54, 55, 56, 57
Lecture Section
(circle): 1 , 2
#include <stdio.h>
/* Global variables
*/
int first=10, second=20, third=5, Ans1, Ans2;
/* Function Prototypes are declared now */
void module1(int, int, int *, int *);
int module2(int, int, int, int *);
void main(void)
{
int fourth,
fifth, sixth, Ans3, Ans4;
/* Executable instructions in main follow */
fourth = 8;
sixth = 10;
fifth = 30;
Ans1 =
module2(first, second, third, &Ans2);
printf("Ans1
and Ans2 are %d %d\n", Ans1, Ans2);
/*1st printf in main */
Ans3 =
module2(fourth, fifth, sixth, &Ans4);
printf("Ans3
and Ans4 are %d %d\n", Ans3, Ans4);
/*2nd printf in main */
module1(first,
fourth, &Ans1, &Ans2);
printf("Ans1
and Ans2 are %d %d\n", Ans1, Ans2);
/*3rd printf in main */
module1(second, sixth,
&Ans3, &Ans4);
printf("Ans3 and Ans4 are %d %d\n", Ans3, Ans4); /*4th printf in main */
}
int seventh = 2;
/* The function
definitions are presented next */
void module1(int Num1, int Num2, int *Res1, int *Res2)
{
*Res1 =
(Num1 % Num2) * 2;
*Res2 =
(Num1 + Num2)/5 + *Res1 + seventh;
printf("printed
from module1 are %d %d \n", *Res1,
*Res2);
}
int eight;
int module2(int Num1, int Num2, int Num3, int *Res2)
{
int temp;
temp = Num1
- Num2 + Num3;
*Res2
= temp * 10;
eight =
temp;
return(eight);
}
1. Write the values printed by the printf instructions
in the program in the table below:
Which
printf |
Values
printed |
First printf in main |
Ans1
and Ans2 are -5 -50 |
Second printf in main |
Ans3
and Ans4 are -12 -120 |
Third printf in main |
Ans1
and Ans2 are 4 9 |
Fourth printf in main |
Ans3
and Ans4 are 0 8 |
All values ever printed by the printf in module1 |
4 9
0 8 |
2. A function prototype in the program is:_
int
module2(int, int, int, int *);
or
void
module1(int, int, int *, int *);
3. A
function call in the program is:
Ans1 =
module2(first, second, third, &Ans2);
Which makes a call to module2 in this assignment instruction.
Another function call in the solution is:
module1(first,
fourth, &Ans1, &Ans2);
There are two other function calls in the program.
4. A
list of actual parameters in the program is:
(first,
second, third, &Ans2) in the first function call or
(first,
fourth, &Ans1, &Ans2) from the following function call
module1(first, fourth, &Ans1, &Ans2);
5. A
list of formal parameters in the program is:
(int Num1, int Num2, int *Res1, int *Res2) in the
function definition for module1, or (int
Num1, int Num2, int Num3, int *Res2) in the function definition for module2.
6. A
call-by-reference actual parameter in the program is:
&Ans1 or &Ans2 or &Ans3 or &Ans4.
7. A
call-by-value actual parameter in the program is: first, second, third, fourth,
fifth or sixth.
8. The
list of global and local variables of the functions are:
Function |
Global variables |
Local Variables |
main |
first, second, third, Ans1, Ans2 |
int fourth, fifth, sixth, Ans3, Ans4 |
module1 |
first, second, third, Ans1, Ans2, seventh |
None |
module2 |
first, second, third, Ans1, Ans2, seventh, eight |
temp |