QuestionQuestion

Goal

You will implement, in C using the Ubuntu Linux environment, a suite of functions to simulate a function call stack, in order to help users track the execution of their programs. This means that, for this assignment, your user will be another programmer! The functions that you implement will create and remove stack frames as the user’s functions are called and as they return. At every stage, you will print out the content of the simulated stack frame. In this assignment, you will:
• implement functions to initialize a simulated function call stack, and push and pop stack frames onto the stack
• augment existing code to use the simulated call stack

Learning Objectives
• work with arrays and structures
• perform simple pointer manipulations
• practice pass-by-reference parameter passing
• develop software for other programmers rather than end users

Instructions
The stack functions that you implement will keep track of the functions that are called and returned within a program. Although the stack functions can be used in any program, for this assignment the functions that we track will be sumIterative and sumRecursive.
Begin by carefully studying the code provided. It’s important that you understand how the sum functions work, because you will have to track their execution. You also have to thoroughly understand the structure definitions provided in the stackDefs.h file, because you must use these exact type definitions in your program. A stack is made up of an array of stack frames. Each stack frame contains information about a particular call to a sum function, including the function’s name and integer values for its parameters. Every time a function is called, a stack frame is “pushed” to the back of the stack (the array of frames), and when this function returns, its stack frame is “popped” (removed) from the array. This means that you will be pushing and popping stack frames only from the back of the array. Every call to a sum function will have different parameter values, so it’s important to have a different stack frame for each call.
You must use the header file and main function provided in the skeleton code. Do not make any changes to the function prototypes provided. All basic error checking must be performed. NOTE: Until we cover Makefiles in class, programs with multiple source files can be compiled as follows: gcc –o execName file1.c file2.c file3.c where execName is the name of your executable file and fileX.c are your source files.

1. Stack functions
You will implement the following functions:
• an initialization function with prototype: void st_init(StackType *stk) o this function initializes the fields of the given stk structure (individual frames do not need to be initialized until they are used)
• a push function with prototype: void st_push(StackType *stk, char *fname, int num, int *arr) o this function initializes a new stack frame at the next available position in the frame array contained in stk o it initializes the new stack frame with the function name specified in fname, the number of parameters indicated in num, and each individual parameter value specified in arr
o it prints out the content of the stack using st_dump
• a pop function with prototype: void st_pop(StackType *stk, int num, int *arr)
o this function “removes” the stack frame at the last occupied position in the frame array contained in stk; before we do that, we want to print out the current values in the stack frame, but there’s a problem! the stack frame contains the values of parameters, which are copied by the push function but then do not change during the execution of the function; if we want to print out the values at the end of the function, we have to reset them using the newest values, which are passed in to the pop function in arr
o first, the st_pop function must re-initialize the parameter values in the stack frame to be removed, using the newest values contained in arr
o next, the function prints out the content of the stack using st_dump o it then “removes” the stack frame from the stack

2. Using the stack
Modify the main function to declare and initialize a stack variable. This variable must be local to main and passed by reference as a parameter to both the sumIterative and sumRecursive functions.
Modify the sumIterative and sumRecursive functions to take a stack pointer as parameter. Make the following changes to each of these functions: • at the beginning of the function:
o create a temporary integer array to contain the values of two of the function’s parameters: the number of elements to be summed and the resulting sum (the value, not the pointer!)
o call the st_push function with the function name as a string, a value of 2 for the number of parameters, and the temporary integer array containing the parameter values
• just before the function returns:
o reset the values in the temporary integer array to the new current values for the number of elements to be summed and the resulting sum
o call the st_pop function with the a value of 2 for the number of parameters and the temporary integer array containing the current parameter values
Test your program thoroughly to make sure that it works correctly with any arbitrary number of elements to be summed. When the iterative sum function is called, it is called once and it returns once. When the recursive function is called, you should see in the stack frame the nested recursive calls for each element of the array to be summed, and the same number of returns.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#include <stdio.h>
#include <string.h>
#include "stackDefs.h"

void sumRecursive(StackType * stk, int numElements, int *intArray, int *sum)
{
    /* create a temporary integer array to contain the values of two of
    * the function’s parameters: the number of elements to be
    * summed and the resulting sum (the value, not the pointer!)
    */
    int tmpArray[2];
    tmpArray[0] = numElements;      
    tmpArray[1] = *sum;
   
    /* call the st_push function with the function name as a string,
    * a value of 2 for the number of parameters,
    * and the temporary integer array containing the parameter values
    */
    printf("*** sum function start ***\n");
    st_push(stk,"sumIterative",2,tmpArray);
   
    if (numElements == 0) {
      *sum = 0;
      tmpArray[1] = *sum;
      printf("*** sum function end ***\n");
    st_pop(stk, 2, tmpArray);
      return;
    }...
$60.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats