60-140

Introduction to Algorithms and Programming I

Dr. Christie Ezeife

Lab. Exercises  #3  Solution (Lab Date:  Week 5 of Classes)

 

Objectives are to:

1.                                                Practise on solving problems using top-down design approach by writing algorithms and programs with functions as taught in chapter 4 of text book.

2.                                                Feel comfortable with such concepts as: function prototypes, function call, function definition, call-by-value parameters, call-by-reference parameters, formal and actual parameters, local and global variables, structure and flowcharts, cohesion and coupling, variable scoping rules.

 

Continue to prepare for Quiz #1 if not yet written.

 

Que. 1.   Given the following program, show the values of the variables a, b, c, x, y, z in the main function after each function call to FindSum. Also, show the values of a, b, c in FindSum immediately after executing each function call to FindSum.

 

#include <stdio.h>

 

/*   function prototype declaration for FindSum   */

void FindSum(int, int, int *);

int main(void)

{

int a=2, b=5, c=1, x=3, y=4, z=7;

 

/*   body of main   */

  FindSum (a, b, &c);    /* a first call to FindSum   */

  printf(“first call in main %d  %d  %d  %d  %d  %d \n”, a, b, c, x, y, z); 

  FindSum (x, y, &z);    /* a second call to FindSum   */

  printf(“second call in main %d  %d  %d  %d  %d  %d \n”, a, b, c, x, y, z); 

  return  0;

}

 

/*   definition of FindSum   */

void FindSum (int a, int b, int *c)

{

  a += (b * 2);

  b += (b * 2);

*c += (b * 2);

  printf(“in FindSum: %d  %d  %d \n”, a, b, *c); 

}

 

 

 

Solution to Que 1

 

i.                    First trace the above program with hand and write the values of the variables a, b, c, x, y, z in the main function after each function call to FindSum.

Also, record the values of variables a, b, c in FindSum immediately after executing each function call to FindSum.

In main

    a

    b

   c

   x

    y

  z

After 1st call to FindSum

2

5

31

3

4

7

After 2nd call to FindSum

2

5

31

3

4

31

 

In FindSum

a

b

*c

After 1st call to FindSum

12

15

31

After 2nd call to FindSum

11

12

31

 

ii.                  Then, type in the program, compile and run to compare the results of your trace with that of your run.

If the two results do not match, talk to your lab instructor or GA for help

 

Que. 2.            a) Question is: What is meant by the scope of a variable?

                        b) Give the variable scoping rules

                        c) Why is the use of parameters preferred over the use of global variables?

 

Solution to Que 2

 

a)      What is meant by the scope of a variable?

 

The scope of a variable defines the modules or functions of a solution where the  variable can be used in an algorithmic or program instruction.
 

(b)    Give the variable scoping rules

Variables declared within a module are the module's local variables while variables above a module in the algorithmic/program solution are this module's global variables.

(c ) Why is the use of parameters preferred over the use of global variables?

 

The problem of side effect, where a global variable can be modified accidentally is eliminated.

There is data protection through the use of local variables.

 

Que. 3.            Do question 1 of Section 4.6.

1.      Do questions 1 of Section 4.6 of course book, with examples.

 

Solution to Que 3:

 

1.      What are the differences between the following pairs of terms?

a)   local and global variables

-          A local variable is a variable defined inside a function which can only be used within this function; while a global variable is a variable defined outside all modules and it is global to all functions defined below it in the algorithmic solution.

 

b)  a function prototype declaration and a function definition
- A function prototype declaration is included at the beginning of an algorithmic solution and is used to tell the name of  the functions used in the solution, the type of result they return and the types of all its parameters in order.  A function definition is used to specify the exact sequence of tasks that this particular function performs.

 

c)   a function definition and a function call
- A function definition specifies the instructions for performing the task the function is created for, and includes a list of formal parameters in the function heading.  A function call is the instruction used to invoke the function to actually perform the operations given some actual input data as parameters.

d)  call-by-reference and call-by-value
- Call-by-reference is a method for passing input and output data to/from a function which requires that the address of the parameter be passed, not  a copy of its value.  This enables the function to directly modify the parameter in the calling module. Call-by-value, on the other hand,  passes only a copy of the value of the parameter and not its address to the function; and by so doing, any modification made to this value within the function can not be seen in the calling module.

e)   cohesion and coupling
- Cohesion is the ability to create modules or subproblems that perform independent tasks, while coupling is the means by which these independent modules are able to work together towards achieving the same goal.  Cohesion is accomplished through structure chart and coupling is accomplished through input and output interfaces between modules of the structure chart.

f)   structure chart and flowchart
- Structure chart is the tool for breaking problems down into subproblems, while flowchart is the tool for graphically representing an algorithmic solution.

g)  formal and actual parameters
- Formal parameters are the names given to input and output parameters in the function heading of the function definition.  They are place holders for the actual parameters which are specified in the function call instructions in the calling modules.