Details of this Paper

Four Questions assignment

Description

solution


Question

E2 ? 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;Last Name;First Name;Student ID #;Here is the grading matrix where the TA will leave feedback. If you scored 100%, you will most likely not see any feedback J;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.;1;#include;2;#include;3;4;int pwr(int x, int y);5;if(y == 0) return 1;6;if(y == 1) return x;7;return x * pwr(x, y-1);8;9;10;int main();11;printf("%d to power %d is %d\n", 5, 3, pwr(5,3));12;return EXIT_SUCCESS;13;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;Line #;What happens?;Stack is;10;Entering main function;main?s activation record;-;No local vars / parameters;11;Invoking function pwr as part of executing the printf;Stack is the same, no need to repeat it;4;Entering pwr function with arguments 5 & 3;main?s activation record;-;No local vars / parameters;pwr(5,3) activation record;-;x is 5;-;y is 3;5;Testing if y is 0 ? false;6;Testing if y is 1 ? false;7;Invoking pwr(5,2) as part of return statement;4;Entering pwr function with arguments 5 & 2;main?s activation record;-;No local vars / parameters;pwr(5,3) activation record;-;x is 5;-;y is 3;pwr(5,2) activation record;-;x is 5;-;y is 2;5;Testing if y is 0 ? false;6;Testing if y is 1 ? false;7;Invoking pwr(5,1);4;Entering pwr function with arguments 5 & 1;main?s activation record;-;No local vars / parameters;pwr(5,3) activation record;-;x is 5;-;y is 3;pwr(5,2) activation record;-;x is 5;-;y is 2;pwr(5,1) activation record;-;x is 5;-;y is 1;5;Testing if y is 0 ? false;6;Testing if y is 1 ? true;6;Return value x which is 5;7;Back from invocation of pwr(5,1) with result 5.;main?s activation record;-;No local vars / parameters;pwr(5,3) activation record;-;x is 5;-;y is 3;pwr(5,2) activation record;-;x is 5;-;y is 2;7;Return the result * x;= 5 * 5;= 25;7;Back from invocation of pwr(5,2) with result 25;main?s activation record;-;No local vars / parameters;pwr(5,3) activation record;-;x is 5;-;y is 3;7;Return the result * x;= 5 * 25;= 125;11;Back from invocation of pwr(5,3) with result 125;main?s activation record;-;No local vars / parameters;11;Printf now displays;?5 to the power 3 is 125;12;return EXIT_SUCCESS from main function;Stack is empty;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;2;#include;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;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

 

Paper#67400 | Written in 18-Jul-2015

Price : $37
SiteLock