Trace the program

Assignment Help Programming Languages
Reference no: EM13786930

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

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int mystery(int v1, int v2){
5 if( (v1 < 1) || (v2 < 1) ) return 0;
6 if( v1 < v2 ) return mystery(v1, v2-1)+1;
7 if( v2 < v1 ) return mystery(v1-1, v2)+1;
8 return mystery(v1-1, v2-1);
9 }
10
11 int main(){
12 int tmp = 0;
13 tmp = mystery(3,2);
14 printf("result is %d\n", tmp);
15 return EXIT_SUCCESS;
16 }

Tests 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
1 // all arrays in this program will have same size
2 #define SIZE 3
3
4 void rotate(int data[], int n){
5 int index = 0;
6 int tmp[SIZE];
7
8 // copying data into tmp array
9 for(index = 0 ; index < SIZE ; index++){
10 tmp[index] = data[index];
11 }
12
13 for(index = 0 ; index < SIZE ; index++){
14 next = (index + n) % SIZE;
15 data[next] = tmp[index];
16 }
17 }

Tests Table to Fill

Feel free to add / remove rows if necessary

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

1 int mystery(int v1, int v2){
2 if( (v1 < 1) || (v2 < 1) ) return 0;
3 if( v1 < v2 ) return mystery(v1, v2-1)+1;
4 if( v2 < v1 ) return mystery(v1-1, v2)+1;
5 return mystery(v1-1, v2-1);
6 }

What did you modify

Your Improved Version
1 int mystery(int v1, int v2){
2 if( (v1 < 1) || (v2 < 1) ) return 0;
3 if( v1 < v2 ) return mystery(v1, v2-1)+1;
4 if( v2 < v1 ) return mystery(v1-1, v2)+1;
5 return mystery(v1-1, v2-1);
6 }
Summary of Improvements
Feel free to add rows to, or remove rows from, this table as needed;


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

1 // arrays passed as d1 / d2 have the following size
2 // array passed as result will always be 2*SIZE
3 #define SIZE 3
4
5 void interleave(int d1[], int d2[], int result[]){
6 int n=0;
7 for( ; n <= SIZE ; n++){
8 result[n] = d1[n];
9 result[n+1] = d2[n];
10 }
11 }

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.

1 // arrays passed as d1 / d2 have the following size
2 #define SIZE 3
3
4 void interleave(int d1[], int d2[], int result[]){
5 int n=0;
6 for( ; n <= SIZE ; n++){
7 result[n] = d1[n];
8 result[n+1] = d2[n];
9 }
10 }

 

Reference no: EM13786930

Questions Cloud

The economic incentives for recycling : What steps can local businesses take to help improve the economic incentives for recycling?
Describe the exclusionary rule : Describe the "Exclusionary Rule" and what it is meant to protect the citizens of this country from. What are the benefits of the Exclusionary Rule? Explain
Write an essay that compare viewpoints on value of college : Write an Essay of Min 700 words Compare and Contrast viewpoints on the value of college.
Describe the elements of proof for an arson fire : Describe the elements of proof for an arson fire. Identify and describe at least 2 distinct motives for individuals to commit arson. The motives you select should fall under 2 different categories listed below: Social and Economic
Trace the program : 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 v..
Ethical issues related to the supervisor : For this assignment, you will refer to the Course Case Study. Reread the case study, looking specifically at issues related to clinical supervision. Examine the ACA's ethical guidelines related to the issue of supervision in Section F and answer t..
Explain the crime scene parameters of an arson fire : Describe the crime scene parameters of an arson fire, including one where an explosive reaction may have occurred. Identify the role of the fire department personnel and their authority at an arson fire. What is the authority of the state fire marsha..
Organizational talent and workforce management : Organizational Talent Workforce Management.
What is the process for removing and collecting the evidence : What is the process for removing and collecting the evidence? Explain in detail. Your analysis must reference specific tests, histological staining, microscopes, and other equipment or techniques that should be used

Reviews

Write a Review

Programming Languages Questions & Answers

  Explain a script namedmyfind.sh that performs a subset

Write a script namedmyfind.sh that performs a subset of the find command. Your script must handle the following options:-name, -type, -newer, -maxdepth, -mindepth, and-exec

  Write a powershell script changenames

Create a batch file CHANGENAMES.BAT that does the following (note that %1, %2, and %3 refer to command line arguments passed to the batch file when executed): Creates a set of %1 empty files with randomly generated names, with filename extension %2..

  Design recursive program to generate random blurbs

A Whoozit is the character 'x' followed by zero or more 'y's. A Whatzit is a 'q' followed by either a 'z' or a 'd', followed by a Whoozit. Design and implement a recursive program that generates random Blurbs in this alien language.

  Write a main program that first reads all available meals

Write a main program that first reads all available meals from a file called menu.txt. Write a function called create_event. This function is be called if a customer of the company wants to book an event.

  How write the program to calculate simple matrix

How to write the program to calculate simple 3x3 spreadsheet containing integers and strings. First, input spreadsheet source values from console.

  Write function concatenation of two strings as its input

Assume f is function which returns result of reversing string of symbols given as its input, and g is function which returns concatenation of two strings given as its input.

  Write complete payroll program for a company

Now, write a complete PAYROLL program for a company in which each employee falls into one of the 3 categories - Administrative, FactoryEmployee or Salesperson.

  Write a program to prints the sum of all elements with an

write a program to prints the sum of all elements with an even index and all elements with an odd index of the

  Provide a graphical view of the planets and bases

Intergalactic Navigation. Provide a graphical view of the planets and bases, and meteorite fields and display the requested path from start to destination.

  Differentiating heavyweight and lightweight process

What is the difference between a heavyweight and a lightweight process? Give an example of where heavyweight processes are appropriate.

  Write an algorithm in structured english

Write an algorithm in structured English (pseudocode) that describes the steps required to perform the task specified and implement your algorithm in Python.

  Write program which functions similarly to atm

Write program which functions similarly to the ATM. A user must be able to give their account number, choose whether they want to make a deposit or a withdrawal

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