How it would execute on a computer

Assignment Help Data Structure & Algorithms
Reference no: EM13809468

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
11int main(){
12 int tmp = 0;
13 tmp = mystery(3,2);
14 printf("result is %d\n", tmp);
15 return EXIT_SUCCESS;
16}

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-

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 }

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;

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-

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Reference no: EM13809468

Questions Cloud

Write a research paper on the rockefellers : Write a research paper on The Rockefellers.
Describe the moral and ethical issues faced by managers : Describe the moral and ethical issues faced by managers dealing with your selected topic. Include how the issue affects other individuals.
What examples for tesol for elementary students : What examples for TESOL for elementary students?
Failure of washington mutual : Closure of Seattle-based saving and loan association Washington Mutual (WaMu) was by far the biggest bank failure in the history of the United States.
How it would execute on a computer : 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..
Analyze sender and receiver using the communication process : Analyze these messages using the communication process. The analysis should include the following: Descriptions of the purpose, sender, receiver, message, environment, technology, noise, and feedback.
Describe the adaptive work that needs to be accomplished : Describe the adaptive work that needs to be accomplished.
University provides job-share programs : If your university provides job-share programs, builds a day-care facility, and only uses recycled paper, it could be said to be ________.
Find the rate of change of the height when the height is : A right circular cylinder has a diameter of 12 in. and a height of 12 in. If water is flowing in at the rate of 4π in3 per minute, find the rate of change of the height when the height is 4 in.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Creating database for a human resources group

Construct a database for a human resources group. List a few different tables and columns to store the HR information.

  Look scheduling policy

Given that it takes 1.75 ms to travel from one track to the next of a hard drive; that the arm is originally positioned at Track 15 moving toward the low- numbered tracks; and that you are using the LOOK scheduling policy

  Create and implement dynamic programming algorithm

Create and implement such dynamic programming algorithm and examine it. You are not sure if CEO must get invited to party, but you suspect that you might get fired if he is not.

  Design the logic that merges the two files

Design the logic that merges the two files to produce one combined name-and-address file, which the office staff can use for addressing mailings of the practice's monthly Healthy Lifestyles newsletter

  Lets examine the heap enqueuedequeue operations with

lets analyze the heap enqueuedequeue operations with different assumptions. imagine that the elements already in the

  Create a flowchart and give the pseudocode for searching an

respond to the following about arrays and their implementations describe an array and its various implementations.

  Which of insertion sort-mergesort and quicksort are stable

A sorting algorithm is described as stable if equal elements are in the same relative order in the sorted sequence as in the original sequence.

  Nbspa stack evaluating the postfix expression lrm using

nbspa stack evaluating the postfix expression lrm using linked list implementationthis step will use the queue

  Create algorithm to read arbitrary number of data record

Create the algorithm to read arbitrary number of data records, each containing name, age, and code. Code of 1 will indicate female, a code of 2 will indicate male.

  Write algorithm to find schedule obtains maximum amount

Write down algorithm to find schedule which obtains maximum amount of profit, assuming that all processing times are integers between 1 and n. Determine running time of your algorithm.

  Designing asp.net assignment

Design an ASP.NET assignment with Visual Studio.NET 2005. Create an event handler that assigns the entry in the TextBox control to Label control's Text property when the consumer clicks the Button control.

  Write schedule produced by earliest deadline first algorithm

Given below are two sets of real-time, periodic tasks. For (a), will the schedule produced by Earliest Deadline First algorithm meet all the deadlines?

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