Define and initialize tmp mains activation record

Assignment Help Programming Languages
Reference no: EM131317746

Fundamentals, Functions & Arrays

Please refer to announcements for details about this exam. Make sure you fill the information below to avoid not being graded properly;

Here is the grading matrix where the TA will leave feedback. If you scored 100%, you will most likely not see any feedback

Question

# points

Feedback

Max

Scored

1

Tracing

3

 

 

2

Testing

2

 

 

3

Refactoring

2

 

 

4

Debugging

3

 

 

Interlude - How to Trace Recursive Programs

To help you fill the trace table in question #1, we start with an example of a small recursive program & provide you with the trace table we expect you to draw.

Example Program to Trace

This program implements a power function recursively. We do not have local variables in either function, thus making the information in each activation record a bit shorter.

#include <stdio.h>
#include <stdlib.h>

int pwr(int x, int y){
if( y == 0 ) return 1;
if( y == 1 ) return x;
return x * pwr(x, y-1);
}

int main(){
printf("%d to power %d is %d\n", 5, 3, pwr(5,3));
return EXIT_SUCCESS;
}

Example Trace Table
Please note the following about the table below;

We only write down the contents of the stack when it is changed, i.e. when we enter a function or assign a new value to a variable in the current activation record.

When we write the contents of the stack, we write the contents of the whole stack, including previous activation records.

Each activation record is identified by a bold line specifying the name of the function & the parameters passed to it when it was invoked.

It is followed by a bullet list with one item per parameter or local variable.

New activation records are added at the end of the contents of the previous stack

Question #1 - Tracing Programs

We are going to trace the following program, i.e. simulate in your head how it would execute on a computer. To help you, a trace table is provided for you to fill. Unlike exam E1, our focus here is not only on keeping track of the values of each variables but also the activation records pushed on the program's execution stack.

Program to Trace
#include <stdio.h>
#include <stdlib.h>

int mystery(int v1, int v2){
if( (v1 < 1) || (v2 < 1) ) return 0;
if( v1 < v2 ) return mystery(v1, v2-1)+1;
if( v2 < v1 ) return mystery(v1-1, v2)+1;
return mystery(v1-1, v2-1);
}

int main(){
int tmp = 0;
tmp = mystery(3,2);
printf("result is %d\n", tmp);
return EXIT_SUCCESS;
}
Trace Table to Fill
Feel free to add / remove rows if necessary

Line #

What happens?

Stack is

11

Entering main function

main's activation record

12

Define & initialize tmp

main's activation record
tmp is 0

...

 

 

Question #2 - Testing Programs

You are going to write tests for the following program.

Its requirements are to

Take an integer array data of SIZE elements

Take a positive, non-null, integer value n

If the value is null or negative, the program should not alter the array

If it is positive, each element in the array should be shifted right by n positions

If an element is pushed past the end of the array, we keep pushing it as if the end of the array connected to its start. Our array is a kind of "ring".

Your objective is to write tests which will guarantee

The program conforms to the requirements; the program below might or might not, your tests need to be able to determine this

All possible execution paths have been tested

Your program does not feature any of the novice errors discussed in the textbook / videos / ...

Program to Test

// all arrays in this program will have same size

#define SIZE 3

void rotate(int data[], int n){
int index = 0;
int tmp[SIZE];

// copying data into tmp array
for(index = 0 ; index < SIZE ; index++){
tmp[index] = data[index];
}

for(index = 0 ; index < SIZE ; index++){
next = (index + n) % SIZE;
data[next] = tmp[index];
}
}

Test #

Inputs' Values

Expected Results

Explanations;
What did you use this test for?
Why is it not redundant with others?

data

n

data

0

1

2

0

1

2

 

 

 

 

 

 

 

 

 

Question #3 - Refactoring Programs

Refactor the following code; i.e. improve its quality without modifying its behavior;

Use meaningful names for variables, parameters & functions

Provide proper documentation as required in the PAs

Provide proper indentation & programming style similar to the examples from the textbook, videos & PAs

Remove useless code

Simplify program

Improve performance

You will provide your version in the "Your Modified Version" subsection which is already pre-filled with the original. Then, for each improvement you applied, use the table in the "Summary" subsection to explain what you did & why you did it.

Program to Refactor

int mystery(int v1, int v2){
if( (v1 < 1) || (v2 < 1) ) return 0;
if( v1 < v2 ) return mystery(v1, v2-1)+1;
if( v2 < v1 ) return mystery(v1-1, v2)+1;
return mystery(v1-1, v2-1);
}

Your Improved Version
int mystery(int v1, int v2){
if( (v1 < 1) || (v2 < 1) ) return 0;
if( v1 < v2 ) return mystery(v1, v2-1)+1;
if( v2 < v1 ) return mystery(v1-1, v2)+1;
return mystery(v1-1, v2-1);
}

Summary of Improvements

Feel free to add rows to, or remove rows from, this table as needed;

What did you modify

How did you modify it?

Why did you modify it?

 

 

 

 

 

 

 

 

 

Question #4 - Debugging Programs

The following function has all sorts of problems in implementing the requirements. We need you to list the errors you found in the table below along with how you would fix them. You will then provide the fixed version of the function. Here is the documentation for the interleave function. You will use this information as requirements;

Role

Modifies the array result so it contains alternated elements from d1 & d2

Example - if d1 = {1,3,5} & d2 = {2,4,6} then result = {1,2,3,4,5,6}

Parameters
d1 & d2 arrays of SIZE integers
result array of SIZE*2 integers

No need to pass the size of the arrays, we expect SIZE to have been globally #define'd

Return Values
n/a
Program to Debug
// arrays passed as d1 / d2 have the following size
// array passed as result will always be 2*SIZE
#define SIZE 3

void interleave(int d1[], int d2[], int result[]){
int n=0;
for( ; n <= SIZE ; n++){
result[n] = d1[n];
result[n+1] = d2[n];
}
}
Bugs Table

Identify each bug you find & provide the lines # where it happens. In addition, explain what the problem was, then how you fixed it. If the same bug appears at different line, just enter it one time in the table but specify all the lines where it happens.

Bug #

Lines #

Problem you identified

How you fixed it

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Your Fixed Version

Apply all the fixes you listed in the table to the code below to make it your fixed version. Please note that if a bug is fixed below but not in the table, or the other way around, you won't get credit for it. You need to have both the bug and its explanations in the table and have it fixed below.

// arrays passed as d1 / d2 have the following size
#define SIZE 3

void interleave(int d1[], int d2[], int result[]){
int n=0;
for( ; n <= SIZE ; n++){
result[n] = d1[n];
result[n+1] = d2[n];
}
}

Reference no: EM131317746

Questions Cloud

Create an array that represents recommended ratings : Create a Java file called Recommender.java. Your program should behave as follows: Create an array that represents recommended ratings for the user. There should be 20 numbers in this array, one for each movie.
Identify one independent variable from your problem : Identify one independent variable from your problem statement in previous unit.How is the independent variable you've identified going to be represented? What is one indicator that could be used to represent this variable?Identify one dependent var..
Explain the nature of frequency shifting in each case : Identify the frequencies in the baseband, and the corresponding frequencies in the DSBSC, USB, and LSB spectra. Explain the nature of frequency shifting in each case.
Acquire in exchange : Assume that ¥87.04 equal $1. Also assume that 6.5614SKr equal $1. How many Japanese yen can you acquire in exchange for 4,300 Swedish krona?
Define and initialize tmp mains activation record : Define & initialize tmp main's activation record. When we write the contents of the stack, we write the contents of the whole stack, including previous activation records.
Describe the role of the financial institutions : Describe the role of the financial institutions and financial markets in our economy. Differentiate between primary and secondary markets. Differentiate between money and capital markets.
Calculate the derivative of the cost : MCD 4140: Computing for Engineers - You have been asked to manage a project to install a pipeline from an offshore gas platform to an onshore gas processing plant, and to find the cheapest design that is possible.
Taxes for the first year of project : The net working capital returns to its original level at the end of the project. The project is expected to generate annual sales of $550,000 and costs of $430,000. The tax rate is 35 percent and the required rate of return is 15 percent. What is ..
How do you find a vector perpendicular to a plane : What is the particular advantageous characteristic associated with the unit vectors in the Cartesian coordinate system?

Reviews

Write a Review

 

Programming Languages Questions & Answers

  Write a program that gets names as input from the user

Write a program that gets names as input from the user (Read from the console using cin. You may tell the user to enter a name or q to quit entering names.), allocates a new Person for each of them and stores the pointer to each of them in a vecto..

  Which array types could hold object references

What is the default initialization value for a character array?

  Implement ref integ result in data duplication

Would failure to implement Ref Integ result in data duplication, incorrect data sets, or broken table errors or is there a work around.

  Develop pseudo-code for program to retrieve bytes

Develop the pseudo-code for a program that will retrieve 2 bytes (NUM1 and NUM2) from memory, determine which is closest to the numeric value 50.

  A primitive data type that represents either true or false

A primitive data type that represents either true or false.

  How prospectors get through mine shaft only fifteen minutes

How can all four prospectors get through mine shaft in only 15 minutes? After you have solved this problem, describe how you got your foot in door.

  Read data from a file into our program and search by title

I need help getting the data from the data file to work correctly with the program. Please see the attached documents.

  Design a flowchart that is also a fully functional program

Using Visual Logic, design a flowchart that is also a fully functional program. According to your design, the program must: Continually accept data regarding the purchase of fruit until a sentinel value is entered.

  Write function which computes and returns sum of n numbers

Write a function, sumsteps2, which computes and returns sum of 1 to n in steps of 2, where n is argument passed to function. For example if 11 is passed, it will return 1 + 3 + 5 + 7 + 9 = 11.

  Design application that reads arbitary number of integers

Design and implement an application that reads an arbitary number of integers that are in the range of 0 to 50 inclusive and counts how many occurences of each are entered.

  Program to print a business travel expenses

Write a program to print a business travel expenses attachment for an income tax return.

  Create a query that lists the following fields

Create a query that lists the following fields, in the order in which they appear below:

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd