SlideShare a Scribd company logo
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
Hanrahan
First Name
Kane
Student ID #
U84918862
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
#include
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 %dn", 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
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
#include
#include
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 %dn", 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
No local vars/ parameters
12
Define & initialize tmp
main’s activation record
tmp is 0
4
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];
}
}
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
1
2
3
4
5
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);a
}
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];
}
}
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx

More Related Content

Similar to E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx

Software Testing for Data Scientists
Software Testing for Data ScientistsSoftware Testing for Data Scientists
Software Testing for Data Scientists
Ajay Ohri
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
DrGSakthiGovindaraju
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
attalurilalitha
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
janakim15
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
functions
functionsfunctions
functions
teach4uin
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
MuhammadMaazShaik
 
R Programming: Comparing Objects In R
R Programming: Comparing Objects In RR Programming: Comparing Objects In R
R Programming: Comparing Objects In R
Rsquared Academy
 
Feature Engineering in NLP.pdf
Feature Engineering in NLP.pdfFeature Engineering in NLP.pdf
Feature Engineering in NLP.pdf
bilaje4244prolugcom
 
As Level Computer Science Book -2
As Level Computer Science  Book -2As Level Computer Science  Book -2
As Level Computer Science Book -2
DIGDARSHAN KUNWAR
 
Ecs 10 programming assignment 4 loopapalooza
Ecs 10 programming assignment 4   loopapaloozaEcs 10 programming assignment 4   loopapalooza
Ecs 10 programming assignment 4 loopapalooza
JenniferBall44
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
HARSHSHARMA840
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
Hans Jones
 
Array Cont
Array ContArray Cont
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
Eelco Visser
 

Similar to E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx (20)

Software Testing for Data Scientists
Software Testing for Data ScientistsSoftware Testing for Data Scientists
Software Testing for Data Scientists
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
C important questions
C important questionsC important questions
C important questions
 
C code examples
C code examplesC code examples
C code examples
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
functions
functionsfunctions
functions
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 
R Programming: Comparing Objects In R
R Programming: Comparing Objects In RR Programming: Comparing Objects In R
R Programming: Comparing Objects In R
 
Feature Engineering in NLP.pdf
Feature Engineering in NLP.pdfFeature Engineering in NLP.pdf
Feature Engineering in NLP.pdf
 
As Level Computer Science Book -2
As Level Computer Science  Book -2As Level Computer Science  Book -2
As Level Computer Science Book -2
 
Ecs 10 programming assignment 4 loopapalooza
Ecs 10 programming assignment 4   loopapaloozaEcs 10 programming assignment 4   loopapalooza
Ecs 10 programming assignment 4 loopapalooza
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
 
Visual Basic
Visual BasicVisual Basic
Visual Basic
 
Visual Basic
Visual BasicVisual Basic
Visual Basic
 
Array Cont
Array ContArray Cont
Array Cont
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 

More from shandicollingwood

This is the second part of Margaret SangerSignificant Contribution.docx
This is the second part of Margaret SangerSignificant Contribution.docxThis is the second part of Margaret SangerSignificant Contribution.docx
This is the second part of Margaret SangerSignificant Contribution.docx
shandicollingwood
 
this is the scenariothis is a discussion I need a paragraph under .docx
this is the scenariothis is a discussion I need a paragraph under .docxthis is the scenariothis is a discussion I need a paragraph under .docx
this is the scenariothis is a discussion I need a paragraph under .docx
shandicollingwood
 
this is the scenaro this is a discussion not a paperHSA515 Wee.docx
this is the scenaro this is a discussion not a paperHSA515 Wee.docxthis is the scenaro this is a discussion not a paperHSA515 Wee.docx
this is the scenaro this is a discussion not a paperHSA515 Wee.docx
shandicollingwood
 
This is the questionNavigate through the official website for .docx
This is the questionNavigate through the official website for .docxThis is the questionNavigate through the official website for .docx
This is the questionNavigate through the official website for .docx
shandicollingwood
 
This is the scenarioHSA515 Week 1 Scenario Script Health Care.docx
This is the scenarioHSA515 Week 1 Scenario Script Health Care.docxThis is the scenarioHSA515 Week 1 Scenario Script Health Care.docx
This is the scenarioHSA515 Week 1 Scenario Script Health Care.docx
shandicollingwood
 
This is the reading and the chart that has to be completed is includ.docx
This is the reading and the chart that has to be completed is includ.docxThis is the reading and the chart that has to be completed is includ.docx
This is the reading and the chart that has to be completed is includ.docx
shandicollingwood
 
This is the question. Please can you use APA format. I need refere.docx
This is the question. Please can you use APA format. I need refere.docxThis is the question. Please can you use APA format. I need refere.docx
This is the question. Please can you use APA format. I need refere.docx
shandicollingwood
 
this is the lecture that goes with the discussionHSA525 Week 4.docx
this is the lecture that goes with the discussionHSA525 Week 4.docxthis is the lecture that goes with the discussionHSA525 Week 4.docx
this is the lecture that goes with the discussionHSA525 Week 4.docx
shandicollingwood
 
This is the last part of your course project. There are two parts to.docx
This is the last part of your course project. There are two parts to.docxThis is the last part of your course project. There are two parts to.docx
This is the last part of your course project. There are two parts to.docx
shandicollingwood
 
This course is Diversity, not HRMUtilizing the resources from th.docx
This course is Diversity, not HRMUtilizing the resources from th.docxThis course is Diversity, not HRMUtilizing the resources from th.docx
This course is Diversity, not HRMUtilizing the resources from th.docx
shandicollingwood
 
This course introduces themes in literature and provides guided stud.docx
This course introduces themes in literature and provides guided stud.docxThis course introduces themes in literature and provides guided stud.docx
This course introduces themes in literature and provides guided stud.docx
shandicollingwood
 
This assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docx
This assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docxThis assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docx
This assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docx
shandicollingwood
 
This assignments contains 4 small assignments1.In this discussion .docx
This assignments contains 4 small assignments1.In this discussion .docxThis assignments contains 4 small assignments1.In this discussion .docx
This assignments contains 4 small assignments1.In this discussion .docx
shandicollingwood
 
this assignment, you will review your current level of adjustment..docx
this assignment, you will review your current level of adjustment..docxthis assignment, you will review your current level of adjustment..docx
this assignment, you will review your current level of adjustment..docx
shandicollingwood
 
This assignment requires you to examine your cultural values about d.docx
This assignment requires you to examine your cultural values about d.docxThis assignment requires you to examine your cultural values about d.docx
This assignment requires you to examine your cultural values about d.docx
shandicollingwood
 
This assignment should include the outline of PERSUASIVE SPEECH.docx
This assignment should include the outline of PERSUASIVE SPEECH.docxThis assignment should include the outline of PERSUASIVE SPEECH.docx
This assignment should include the outline of PERSUASIVE SPEECH.docx
shandicollingwood
 
This assignment should include the following partsØIntroducti.docx
This assignment should include the following partsØIntroducti.docxThis assignment should include the following partsØIntroducti.docx
This assignment should include the following partsØIntroducti.docx
shandicollingwood
 
This assignment requires that your format a fake manuscript into APA.docx
This assignment requires that your format a fake manuscript into APA.docxThis assignment requires that your format a fake manuscript into APA.docx
This assignment requires that your format a fake manuscript into APA.docx
shandicollingwood
 
This Assignment must be original, no plagerism,Examine the ways .docx
This Assignment must be original, no plagerism,Examine the ways .docxThis Assignment must be original, no plagerism,Examine the ways .docx
This Assignment must be original, no plagerism,Examine the ways .docx
shandicollingwood
 
This assignment must be completed by 09052016 at 8PM Pacific Stand.docx
This assignment must be completed by 09052016 at 8PM Pacific Stand.docxThis assignment must be completed by 09052016 at 8PM Pacific Stand.docx
This assignment must be completed by 09052016 at 8PM Pacific Stand.docx
shandicollingwood
 

More from shandicollingwood (20)

This is the second part of Margaret SangerSignificant Contribution.docx
This is the second part of Margaret SangerSignificant Contribution.docxThis is the second part of Margaret SangerSignificant Contribution.docx
This is the second part of Margaret SangerSignificant Contribution.docx
 
this is the scenariothis is a discussion I need a paragraph under .docx
this is the scenariothis is a discussion I need a paragraph under .docxthis is the scenariothis is a discussion I need a paragraph under .docx
this is the scenariothis is a discussion I need a paragraph under .docx
 
this is the scenaro this is a discussion not a paperHSA515 Wee.docx
this is the scenaro this is a discussion not a paperHSA515 Wee.docxthis is the scenaro this is a discussion not a paperHSA515 Wee.docx
this is the scenaro this is a discussion not a paperHSA515 Wee.docx
 
This is the questionNavigate through the official website for .docx
This is the questionNavigate through the official website for .docxThis is the questionNavigate through the official website for .docx
This is the questionNavigate through the official website for .docx
 
This is the scenarioHSA515 Week 1 Scenario Script Health Care.docx
This is the scenarioHSA515 Week 1 Scenario Script Health Care.docxThis is the scenarioHSA515 Week 1 Scenario Script Health Care.docx
This is the scenarioHSA515 Week 1 Scenario Script Health Care.docx
 
This is the reading and the chart that has to be completed is includ.docx
This is the reading and the chart that has to be completed is includ.docxThis is the reading and the chart that has to be completed is includ.docx
This is the reading and the chart that has to be completed is includ.docx
 
This is the question. Please can you use APA format. I need refere.docx
This is the question. Please can you use APA format. I need refere.docxThis is the question. Please can you use APA format. I need refere.docx
This is the question. Please can you use APA format. I need refere.docx
 
this is the lecture that goes with the discussionHSA525 Week 4.docx
this is the lecture that goes with the discussionHSA525 Week 4.docxthis is the lecture that goes with the discussionHSA525 Week 4.docx
this is the lecture that goes with the discussionHSA525 Week 4.docx
 
This is the last part of your course project. There are two parts to.docx
This is the last part of your course project. There are two parts to.docxThis is the last part of your course project. There are two parts to.docx
This is the last part of your course project. There are two parts to.docx
 
This course is Diversity, not HRMUtilizing the resources from th.docx
This course is Diversity, not HRMUtilizing the resources from th.docxThis course is Diversity, not HRMUtilizing the resources from th.docx
This course is Diversity, not HRMUtilizing the resources from th.docx
 
This course introduces themes in literature and provides guided stud.docx
This course introduces themes in literature and provides guided stud.docxThis course introduces themes in literature and provides guided stud.docx
This course introduces themes in literature and provides guided stud.docx
 
This assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docx
This assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docxThis assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docx
This assingment is due Monday May 9th, 2016, at 1130 A.M. Pacific T.docx
 
This assignments contains 4 small assignments1.In this discussion .docx
This assignments contains 4 small assignments1.In this discussion .docxThis assignments contains 4 small assignments1.In this discussion .docx
This assignments contains 4 small assignments1.In this discussion .docx
 
this assignment, you will review your current level of adjustment..docx
this assignment, you will review your current level of adjustment..docxthis assignment, you will review your current level of adjustment..docx
this assignment, you will review your current level of adjustment..docx
 
This assignment requires you to examine your cultural values about d.docx
This assignment requires you to examine your cultural values about d.docxThis assignment requires you to examine your cultural values about d.docx
This assignment requires you to examine your cultural values about d.docx
 
This assignment should include the outline of PERSUASIVE SPEECH.docx
This assignment should include the outline of PERSUASIVE SPEECH.docxThis assignment should include the outline of PERSUASIVE SPEECH.docx
This assignment should include the outline of PERSUASIVE SPEECH.docx
 
This assignment should include the following partsØIntroducti.docx
This assignment should include the following partsØIntroducti.docxThis assignment should include the following partsØIntroducti.docx
This assignment should include the following partsØIntroducti.docx
 
This assignment requires that your format a fake manuscript into APA.docx
This assignment requires that your format a fake manuscript into APA.docxThis assignment requires that your format a fake manuscript into APA.docx
This assignment requires that your format a fake manuscript into APA.docx
 
This Assignment must be original, no plagerism,Examine the ways .docx
This Assignment must be original, no plagerism,Examine the ways .docxThis Assignment must be original, no plagerism,Examine the ways .docx
This Assignment must be original, no plagerism,Examine the ways .docx
 
This assignment must be completed by 09052016 at 8PM Pacific Stand.docx
This assignment must be completed by 09052016 at 8PM Pacific Stand.docxThis assignment must be completed by 09052016 at 8PM Pacific Stand.docx
This assignment must be completed by 09052016 at 8PM Pacific Stand.docx
 

Recently uploaded

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx

  • 1. 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 Hanrahan First Name Kane Student ID # U84918862 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. 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 #include int pwr(int x, int y){ if( y == 0 ) return 1; if( y == 1 ) return x; return x * pwr(x, y-1); } int main(){
  • 3. printf("%d to power %d is %dn", 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 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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 #include 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 %dn", 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 No local vars/ parameters 12
  • 8. Define & initialize tmp main’s activation record tmp is 0 4 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){
  • 9. 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]; } } 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
  • 11. 5 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);a } Your Improved Version int mystery(int v1, int v2){
  • 12. 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 =
  • 13. {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
  • 14. 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]; } }