SlideShare a Scribd company logo
1 of 17
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 (
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 <stdio.h>
2 #include <stdlib.h>
3 int pwr(int x, int y){
4
if( y == 0 )
return 1;
5
if( y == 1 )
return x;
6
return x * pwr(x, y-1);
7 }
8 int main(){
9
printf("%d to power %d is %dn", 5, 3, pwr(5,3));
10
return EXIT_SUCCESS;
11 }
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 emptyQuestion #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 int mystery(int v1, int v2){
4
if( (v1 < 1) || (v2 < 1) )
return 0;
5
if( v1 < v2 ) return mystery(v1, v2-1)+1;
6
if( v2 < v1 ) return mystery(v1-1, v2)+1;
7
return mystery(v1-1, v2-1);
8 }
9 int main(){
10
int tmp = 0;
11 tmp = mystery(3,2);
12
printf("result is %dn", tmp);
13
return EXIT_SUCCESS;
14 }
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 void rotate(int data[], int n){
4
int index = 0;
5
int tmp[SIZE];
6
// copying data into tmp array
7
for(index = 0 ; index < SIZE ; index++){
8
tmp[index] = data[index];
9
}
10
for(index = 0 ; index < SIZE ; index++){
11
next = (index + n) % SIZE;
12
data[next] = tmp[index];
13
}
14 }
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 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 }
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 void interleave(int d1[], int d2[], int result[]){
4
int n=0;
5
for( ; n <= SIZE ; n++){
6
result[n] = d1[n];
7
result[n+1] = d2[n];
8
}
9 }

More Related Content

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

Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potterdistributed matters
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfezhilvizhiyan
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsJames Brotsos
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdfHans Jones
 
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.pdfKabilaArun
 
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.pdfattalurilalitha
 
System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSubash John
 
Ecs 10 programming assignment 4 loopapalooza
Ecs 10 programming assignment 4   loopapaloozaEcs 10 programming assignment 4   loopapalooza
Ecs 10 programming assignment 4 loopapaloozaJenniferBall44
 
Software Testing for Data Scientists
Software Testing for Data ScientistsSoftware Testing for Data Scientists
Software Testing for Data ScientistsAjay Ohri
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 

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

Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potter
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.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
 
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 code examples
C code examplesC code examples
C code examples
 
System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancements
 
Ecs 10 programming assignment 4 loopapalooza
Ecs 10 programming assignment 4   loopapaloozaEcs 10 programming assignment 4   loopapalooza
Ecs 10 programming assignment 4 loopapalooza
 
C important questions
C important questionsC important questions
C important questions
 
3 Function & Storage Class.pptx
3 Function & Storage Class.pptx3 Function & Storage Class.pptx
3 Function & Storage Class.pptx
 
JavaProgrammingManual
JavaProgrammingManualJavaProgrammingManual
JavaProgrammingManual
 
Software Testing for Data Scientists
Software Testing for Data ScientistsSoftware Testing for Data Scientists
Software Testing for Data Scientists
 
functions
functionsfunctions
functions
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 

More from jacksnathalie

OverviewThe US is currently undergoing an energy boom largel.docx
OverviewThe US is currently undergoing an energy boom largel.docxOverviewThe US is currently undergoing an energy boom largel.docx
OverviewThe US is currently undergoing an energy boom largel.docxjacksnathalie
 
OverviewThe United Nations (UN) has hired you as a consultan.docx
OverviewThe United Nations (UN) has hired you as a consultan.docxOverviewThe United Nations (UN) has hired you as a consultan.docx
OverviewThe United Nations (UN) has hired you as a consultan.docxjacksnathalie
 
OverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docxOverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docxjacksnathalie
 
OverviewThis week, we begin our examination of contemporary resp.docx
OverviewThis week, we begin our examination of contemporary resp.docxOverviewThis week, we begin our examination of contemporary resp.docx
OverviewThis week, we begin our examination of contemporary resp.docxjacksnathalie
 
OverviewProgress monitoring is a type of formative assessment in.docx
OverviewProgress monitoring is a type of formative assessment in.docxOverviewProgress monitoring is a type of formative assessment in.docx
OverviewProgress monitoring is a type of formative assessment in.docxjacksnathalie
 
OverviewThe work you do throughout the modules culminates into a.docx
OverviewThe work you do throughout the modules culminates into a.docxOverviewThe work you do throughout the modules culminates into a.docx
OverviewThe work you do throughout the modules culminates into a.docxjacksnathalie
 
OverviewThis discussion is about organizational design and.docx
OverviewThis discussion is about organizational design and.docxOverviewThis discussion is about organizational design and.docx
OverviewThis discussion is about organizational design and.docxjacksnathalie
 
OverviewScholarly dissemination is essential for any doctora.docx
OverviewScholarly dissemination is essential for any doctora.docxOverviewScholarly dissemination is essential for any doctora.docx
OverviewScholarly dissemination is essential for any doctora.docxjacksnathalie
 
OverviewRegardless of whether you own a business or are a s.docx
OverviewRegardless of whether you own a business or are a s.docxOverviewRegardless of whether you own a business or are a s.docx
OverviewRegardless of whether you own a business or are a s.docxjacksnathalie
 
OverviewImagine you have been hired as a consultant for th.docx
OverviewImagine you have been hired as a consultant for th.docxOverviewImagine you have been hired as a consultant for th.docx
OverviewImagine you have been hired as a consultant for th.docxjacksnathalie
 
OverviewDevelop a 4–6-page position about a specific health care.docx
OverviewDevelop a 4–6-page position about a specific health care.docxOverviewDevelop a 4–6-page position about a specific health care.docx
OverviewDevelop a 4–6-page position about a specific health care.docxjacksnathalie
 
Overview This purpose of the week 6 discussion board is to exam.docx
Overview This purpose of the week 6 discussion board is to exam.docxOverview This purpose of the week 6 discussion board is to exam.docx
Overview This purpose of the week 6 discussion board is to exam.docxjacksnathalie
 
Overall Scenario Always Fresh Foods Inc. is a food distributor w.docx
Overall Scenario Always Fresh Foods Inc. is a food distributor w.docxOverall Scenario Always Fresh Foods Inc. is a food distributor w.docx
Overall Scenario Always Fresh Foods Inc. is a food distributor w.docxjacksnathalie
 
OverviewCreate a 15-minute oral presentation (3–4 pages) that .docx
OverviewCreate a 15-minute oral presentation (3–4 pages) that .docxOverviewCreate a 15-minute oral presentation (3–4 pages) that .docx
OverviewCreate a 15-minute oral presentation (3–4 pages) that .docxjacksnathalie
 
Overall CommentsHi Khanh,Overall you made a nice start with y.docx
Overall CommentsHi Khanh,Overall you made a nice start with y.docxOverall CommentsHi Khanh,Overall you made a nice start with y.docx
Overall CommentsHi Khanh,Overall you made a nice start with y.docxjacksnathalie
 
Overall CommentsHi Khanh,Overall you made a nice start with.docx
Overall CommentsHi Khanh,Overall you made a nice start with.docxOverall CommentsHi Khanh,Overall you made a nice start with.docx
Overall CommentsHi Khanh,Overall you made a nice start with.docxjacksnathalie
 
Overall feedbackYou addressed most all of the assignment req.docx
Overall feedbackYou addressed most all  of the assignment req.docxOverall feedbackYou addressed most all  of the assignment req.docx
Overall feedbackYou addressed most all of the assignment req.docxjacksnathalie
 
Overall Comments Overall you made a nice start with your U02a1 .docx
Overall Comments Overall you made a nice start with your U02a1 .docxOverall Comments Overall you made a nice start with your U02a1 .docx
Overall Comments Overall you made a nice start with your U02a1 .docxjacksnathalie
 
Overview This purpose of the week 12 discussion board is to e.docx
Overview This purpose of the week 12 discussion board is to e.docxOverview This purpose of the week 12 discussion board is to e.docx
Overview This purpose of the week 12 discussion board is to e.docxjacksnathalie
 
Over the years, the style and practice of leadership within law .docx
Over the years, the style and practice of leadership within law .docxOver the years, the style and practice of leadership within law .docx
Over the years, the style and practice of leadership within law .docxjacksnathalie
 

More from jacksnathalie (20)

OverviewThe US is currently undergoing an energy boom largel.docx
OverviewThe US is currently undergoing an energy boom largel.docxOverviewThe US is currently undergoing an energy boom largel.docx
OverviewThe US is currently undergoing an energy boom largel.docx
 
OverviewThe United Nations (UN) has hired you as a consultan.docx
OverviewThe United Nations (UN) has hired you as a consultan.docxOverviewThe United Nations (UN) has hired you as a consultan.docx
OverviewThe United Nations (UN) has hired you as a consultan.docx
 
OverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docxOverviewThis project will allow you to write a program to get mo.docx
OverviewThis project will allow you to write a program to get mo.docx
 
OverviewThis week, we begin our examination of contemporary resp.docx
OverviewThis week, we begin our examination of contemporary resp.docxOverviewThis week, we begin our examination of contemporary resp.docx
OverviewThis week, we begin our examination of contemporary resp.docx
 
OverviewProgress monitoring is a type of formative assessment in.docx
OverviewProgress monitoring is a type of formative assessment in.docxOverviewProgress monitoring is a type of formative assessment in.docx
OverviewProgress monitoring is a type of formative assessment in.docx
 
OverviewThe work you do throughout the modules culminates into a.docx
OverviewThe work you do throughout the modules culminates into a.docxOverviewThe work you do throughout the modules culminates into a.docx
OverviewThe work you do throughout the modules culminates into a.docx
 
OverviewThis discussion is about organizational design and.docx
OverviewThis discussion is about organizational design and.docxOverviewThis discussion is about organizational design and.docx
OverviewThis discussion is about organizational design and.docx
 
OverviewScholarly dissemination is essential for any doctora.docx
OverviewScholarly dissemination is essential for any doctora.docxOverviewScholarly dissemination is essential for any doctora.docx
OverviewScholarly dissemination is essential for any doctora.docx
 
OverviewRegardless of whether you own a business or are a s.docx
OverviewRegardless of whether you own a business or are a s.docxOverviewRegardless of whether you own a business or are a s.docx
OverviewRegardless of whether you own a business or are a s.docx
 
OverviewImagine you have been hired as a consultant for th.docx
OverviewImagine you have been hired as a consultant for th.docxOverviewImagine you have been hired as a consultant for th.docx
OverviewImagine you have been hired as a consultant for th.docx
 
OverviewDevelop a 4–6-page position about a specific health care.docx
OverviewDevelop a 4–6-page position about a specific health care.docxOverviewDevelop a 4–6-page position about a specific health care.docx
OverviewDevelop a 4–6-page position about a specific health care.docx
 
Overview This purpose of the week 6 discussion board is to exam.docx
Overview This purpose of the week 6 discussion board is to exam.docxOverview This purpose of the week 6 discussion board is to exam.docx
Overview This purpose of the week 6 discussion board is to exam.docx
 
Overall Scenario Always Fresh Foods Inc. is a food distributor w.docx
Overall Scenario Always Fresh Foods Inc. is a food distributor w.docxOverall Scenario Always Fresh Foods Inc. is a food distributor w.docx
Overall Scenario Always Fresh Foods Inc. is a food distributor w.docx
 
OverviewCreate a 15-minute oral presentation (3–4 pages) that .docx
OverviewCreate a 15-minute oral presentation (3–4 pages) that .docxOverviewCreate a 15-minute oral presentation (3–4 pages) that .docx
OverviewCreate a 15-minute oral presentation (3–4 pages) that .docx
 
Overall CommentsHi Khanh,Overall you made a nice start with y.docx
Overall CommentsHi Khanh,Overall you made a nice start with y.docxOverall CommentsHi Khanh,Overall you made a nice start with y.docx
Overall CommentsHi Khanh,Overall you made a nice start with y.docx
 
Overall CommentsHi Khanh,Overall you made a nice start with.docx
Overall CommentsHi Khanh,Overall you made a nice start with.docxOverall CommentsHi Khanh,Overall you made a nice start with.docx
Overall CommentsHi Khanh,Overall you made a nice start with.docx
 
Overall feedbackYou addressed most all of the assignment req.docx
Overall feedbackYou addressed most all  of the assignment req.docxOverall feedbackYou addressed most all  of the assignment req.docx
Overall feedbackYou addressed most all of the assignment req.docx
 
Overall Comments Overall you made a nice start with your U02a1 .docx
Overall Comments Overall you made a nice start with your U02a1 .docxOverall Comments Overall you made a nice start with your U02a1 .docx
Overall Comments Overall you made a nice start with your U02a1 .docx
 
Overview This purpose of the week 12 discussion board is to e.docx
Overview This purpose of the week 12 discussion board is to e.docxOverview This purpose of the week 12 discussion board is to e.docx
Overview This purpose of the week 12 discussion board is to e.docx
 
Over the years, the style and practice of leadership within law .docx
Over the years, the style and practice of leadership within law .docxOver the years, the style and practice of leadership within law .docx
Over the years, the style and practice of leadership within law .docx
 

Recently uploaded

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.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 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 ( Question # points Feedback Max Scored 1 Tracing 3 2 Testing 2 3
  • 2. 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 <stdio.h> 2 #include <stdlib.h> 3 int pwr(int x, int y){ 4 if( y == 0 ) return 1; 5 if( y == 1 ) return x; 6 return x * pwr(x, y-1);
  • 3. 7 } 8 int main(){ 9 printf("%d to power %d is %dn", 5, 3, pwr(5,3)); 10 return EXIT_SUCCESS; 11 } 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
  • 4. 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
  • 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
  • 6. · 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
  • 7. = 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 emptyQuestion #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
  • 8. 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 int mystery(int v1, int v2){ 4 if( (v1 < 1) || (v2 < 1) ) return 0; 5 if( v1 < v2 ) return mystery(v1, v2-1)+1; 6 if( v2 < v1 ) return mystery(v1-1, v2)+1; 7 return mystery(v1-1, v2-1); 8 } 9 int main(){ 10 int tmp = 0; 11 tmp = mystery(3,2); 12 printf("result is %dn", tmp); 13 return EXIT_SUCCESS; 14 }
  • 9. 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”.
  • 10. 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 void rotate(int data[], int n){ 4 int index = 0; 5 int tmp[SIZE]; 6 // copying data into tmp array 7 for(index = 0 ; index < SIZE ; index++){ 8 tmp[index] = data[index]; 9 } 10 for(index = 0 ; index < SIZE ; index++){
  • 11. 11 next = (index + n) % SIZE; 12 data[next] = tmp[index]; 13 } 14 } 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
  • 12. 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
  • 13. 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;
  • 14. 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
  • 15. · 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 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 } 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
  • 16. 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 void interleave(int d1[], int d2[], int result[]){ 4 int n=0; 5 for( ; n <= SIZE ; n++){ 6