Question #1 - Tracing Programs
As with previous exams, trace the following program by tracking the values of all variables in the table provided below.
Program to Trace
#include
int main(){
1 int array[5] = { 0,1,2,3,4} ;
2 int * p1, **p2 ;
3 int * parray[2] = { NULL , NULL };
4 p1= & array[2] ;
5 *p1 = 20 ;
6 p2 = & p1 ;
7 ** p2 = 30 ;
8 parray[0] = & array[0];
9 parray[1] = & **p2;
10 *parray[0] = 30 ;
11 *parray[1] = 300;
}
Trace Table to Fill
In order to make it easier for you to trace pointer values, here is a table telling you the memory addresses of all the variables in the above program.
|
Variables
|
array[0] to array[4]
|
p1
|
p2
|
parray[0] to parray[1]
|
Memory Address
|
1000
|
1004
|
1008
|
1012
|
1016
|
1020
|
1024
|
1028
|
1032
|
The following table is where you will be tracing your program;
Use ??? to denote an undefined value; e.g. non-initialized variable.
Use error to denote the dereference of a NULL or undefined pointer.
As for previous exam, a given line should only modify the columns of the variables which it modified. However, because we use pointers, you might have to modify other columns if your line just dereferenced a pointer to modify another variable.
Question #2 - Testing Programs
You are going to write tests for a function. Unlike previous exams, we do not provide you with the source of this function but only its requirements. Therefore, your tests will have to allow us to verify that an arbitrary implementation fulfills requirements.
Program Requirements
The signature / prototype of the function;
char* str_reverse(char * str);
Its requirements are as follows;
Work with any value for the char*
Return a newly allocated string which is reversed compared to the original
Return a copy of an empty string
Return NULL if the string is NULL
Returns NULL if the string is more than 10 characters long, including end of string marker
Tests Table to Fill
Feel free to add / remove rows if necessary
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
Improve readability
Handle bad parameter values by returning NULL
Handle memory allocation problems by returning NULL
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
Summary of Improvements
Feel free to add rows to, or remove rows from, this table as needed;
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 function. You will use this information as requirements;
Role
Takes two non-null, non-empty, strings of same length, containing only lower-case alphabetical characters
Return a newly allocated string of same length than s1
For each character at index n in this new string, its ASCII index should be 141 plus half of the positive difference between the ASCII values of the characters at index n in s1 & s2
Parameters
s1 & s2 non-null, non-empty strings of same size
Return Values
NULL if the parameters are not what we expected
NULL if memory allocation was a problem
Address of a newly allocated string as described above
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.
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.