Page 79 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 79

Advanced Data Structure and Algorithms




                    Notes          return (n * y);
                                   }
                                   How are we to define the data area for this function? It must contain the parameter n and the

                                   local variables x and y. As we shall see, no temporary variables are needed. The data area must
                                   also contain a return address. In this case, there are two possible points to which we might want
                                   to return: the assignment of fact(x) to y, and the main program that called fact. Suppose that we
                                   had two labels and that we let the label label2 be the label of a section of code.
                                   Label 2: y = result;
                                   within the simulating program. Let the label 1 be the label of a statement

                                   Label 1: return(result);

                                   This reflects a convention that the variable to result contains the value to be returned by an
                                   invocation of the fact function. The return address will be stored as an integer i (equal to either 1
                                   or 2). To effect a return from a recursive call the statement
                                   switch(i) {
                                   case 1: goto label1;
                                   case 2: goto 1abe12;
                                   }
                                   is executed. Thus, if i  = =  1. a return is executed to the main program that called fact, and if
                                   i == 2, a return is simulated to the assignment of the returned value to the variable y in the
                                   previous execution of fact.
                                   Proving Correctness of Parenthesis in an Expression





                                     Lab Exercise   This program takes one mathematical expression and checks whether the
                                                opening and closing parenthesis match or not.
                                     #define LEFTBRACKET ‘{‘
                                     #define RIGHTBRACKET ‘}‘
                                     #include <stdio.h>
                                     main()
                                     {
                                     char t, *p, expr[100];
                                     printf(“Enter an expression :”);
                                     scanf(“%s”,expr);
                                     p=expr;
                                     while(p != ‘\0’)
                                     {
                                      if( *p == LEFTBRACKET) push(p);
                                      if(*p == RIGHTBRACKET) t=pop();
                                      if(t < 0) (printf(“ERROR:”));
                                      p++;
                                     }




          74                               LOVELY PROFESSIONAL UNIVERSITY
   74   75   76   77   78   79   80   81   82   83   84