Lesson 04 In-Class Exercises

Starter project for these in-class exercises

Part 0: "Tracing": Basic Arrays - Follow Along With The Teacher

Download, extract, and open up the provided starter project for Visual Studio.  In the Program.cs file you will find the Array_Basics.RunExercise method, which you will trace through using this file: trace file for Array_FollowAlong.

p.s.v. Main() 
{
	int x = 1; // to remind them of this style
	x = 3; // to show how the stack changes
	int[] nums = new int[x]; // point out that [3] also works
		// emphasize the 'zeroing out' thing, the memory address thing
	nums[1] = 10;
	// UPDATE TABLE - show them how to copy the table at this point
	x = 2;
	nums[x] = 20;
	Console.WriteLine(x);
	Console.WriteLine(nums[x]);
	
	// manually resizing the array
	int[] biggerArray = new int[ nums.Length + 2];
	for(int i = 0; i < nums.Length; i++)
	{
		biggerArray[i] = nums[i];
                // UPDATE TABLE - copy the table here
	}
	
	// key line:
	nums = biggerArray;
}

 

Part 1: "Tracing": Basic Arrays - Finding The Maximum

Download, extract, and open up the provided starter project for Visual Studio.  In the Program.cs file you will find the Array_Basics.RunExercise method, which you will trace through using this file: trace file for Array_Basics.RunExercise.

Keep in mind that unlike BIT 115 (wherein you were expected to transcribe each and every line as you hand-executed the entire program) in these exercises you are expected to mentally simulate the execution of the program, updating the trace as you go. 

In order to have a well-defined, consistent procedure for y'all to follow, you should do the following: start mentally simulating the program, using the top-most cell in the trace file as your 'scratch space' to keep track of what the program is doing - we're going to call this the 'current cell'.  When you see a comment that that looks like: // SWITCH TO NEW CELL IN THE TRACE FILE you should copy all the values from the current into the cell below it, and then you should switch to using that new cell as your current cell  

Question: What will you put into the spaces of variables that are not yet defined?  What about variables that are defined, but that have not yet been assigned a value?

Be prepared to walk the class through your trace, verbally explaining the execution of the program while using the trace to illustrate the value of variables at various points in the program's run.

Tip:  You can make new cells for yourself by moving your mouse pointer just to the left of the rows that you want to copy (the mouse icon will become an arrow, rather than the normal Word cursor bar), selecting the rows, and then copying (Control+C) the rows, moving the cursor down below the last table and pasting (Control+V). 

Pro Tip: Many people find it very, very effective to print out the trace file and fill this out by hand. 

Part 2: "Tracing": Arrays As Parameters

Trace through the Array_Parameters.RunExercise method using this file: trace file for Array_Parameters.RunExercise.
You do not need to include a reference to the ArrayHelper object (we'll look at that in more detail when we cover Object Oriented Programming)

Be prepared to walk the class through your trace, verbally explaining the execution of the program while using the trace to illustrate the value of variables at various points in the program's run.

Part 3: "Tracing": Returning Arrays

Trace through the Array_Return.RunExercise method using this file: trace file for Array_Return.RunExercise.You do not need to include a reference to the ArrayHelper object.

You should also ignore any of the // SWITCH TO NEW CELL IN THE TRACE FILE comments in FillArray - because of that you will not need to track any of the variables in the FillArrayMethod.

Note: The arrays in the trace file for this part are all 5 elements long.  If you're not using all five elements then put a dash ( - ) into the cells that don't exist.

Be prepared to walk the class through your trace, verbally explaining the execution of the program while using the trace to illustrate the value of variables at various points in the program's run.

One Minute Paper

"A “one-minute paper” may be defined as a very short, in-class writing activity (taking one-minute or less to complete) in response to an instructor-posed question, which prompts students to reflect on the day’s lesson and provides the instructor with useful feedback." (from http://www.oncourseworkshop.com/Awareness012.htm).

For this One Minute Paper, I would like you to think back on both the preview videos / viewing quiz and today's in-class exercises, and quickly write up answers to the questions listed therein - it should take a couple minutes or so.

Head on over to the Google Docs form, and fill out the One Minute Paper for this lecture.

HIDDEN INSTRUCTOR NOTES

It was a bit much to draw everything on the board, then expect them to do the trace on their own. 

Instead of the whiteboard lecture, what about having them follow along as I fill in a simple-ish trace.  Something like:

p.s.v. Main() 
{
	int x = 5; // to remind them of this style
	x = 3; // to show how the stack changes
	int[] nums = new int[x]; // point out that [3] also works
		// emphasize the 'zeroing out' thing, the memory address thing
	nums[1] = 10;
	// UPDATE TABLE - show them how to copy the table at this point
	x = 2;
	nums[x] = 20;
	C.WL(x);
	C.WL(nums[x]);
	
	// manually resizing the array
	int biggerArray = new int[ nums.Length + 4];
	for(int i = 0; i < nums.Length; i++)
	{
		biggerArray[i] = nums[i];
	}
	
	// key line:
	nums = biggerArray;
}
 

Future Ideas

PCE review: basic parameters?