bg287

Test #3.

Posted in csc110 by bnmng on 2010 04/04

Here’s my take on the sample test that we got on Thursday

1)
The following loop calculates 2 to the power of n.
For example, when n is 3, it calculates 2*2*2 (three times)

Identify the parts of the loop using the following list:
1) Test
2) Update of control
3) Initialization of Execution
4) Update of Execution
5) Initialization of control

n=2;
i=1;  // initialization of control
product=1; // initialization of execution
while(i <=n) // test
{ product=product*2; // update of execution
i=i+1} // update of control

When two people are talking, the word “execution” refers to something like an activity or an event; as in the execution of an order, or the execution of an operation, or the execution of Paul Powel.  I’m against the death penalty – not because some people don’t need killing; but because the government shouldn’t have that authority.  But I digress.  For our purposes, and the purpose of test3, think of “execution” not as an activity but as a variable.  The purpose of the loop is to update the execution.  In the above example, the execution is productproduct is the reaon for the loop’s existence.  So you can say to you’re girlfriend or boyfriend, “You’re my execution.”  She or he won’t know what you’re talking about but you’re a computer geek so it probably happens all the time.  So when product gets initialized, that’s the initialization of execution, and when product gets updated, that’s the update of execution.

The control is like a traffic light.  You’ll base your decision on what to do by the color of the traffic light (stop, go, or go fast before it changes).  The traffic light doesn’t make you stop; That’s a decision you have to make on your own. You decide to stop (hopefully) when you see a red light.  In the loop, the control is i.  i has a value which changes as the loop executes, and each time the loop goes around, the program makes a decision, based on the value of the control, whether to stop or go.

The test is where the decision is made.  At the traffic light, the test is “Go if the light is green” or “Stop if the light is red”,  In the example above, the test is “Go while i is less than n“.

What is the output of the following program?

#include <iostream>
using namespace std;
void inorder(float &, float &);

int main()
{
	float n1=41.6, n2=19.2;

	inorder (n1, n2);
	cout << "n1= "<< n1 << "n2=" << n2 << endl;
	return 0;
}
void inorder (float &num1, float & num2)
{
	float temp;
	if(num1 > num2) 
	{
		temp=num1;
		num1=num2;
		num2=temp;
	}
}

nl =  19.2        n2 = 41.6

Answer the same question if the protype and heading are changed as follows:

void inorder (float, float);
void inorder(float num1, float num2)

nl =  41.6        n2 = 19.2

Remember the simile with the teacher and the students.  Passing by reference (using the “&”) is like sending the function to the board to work on the original data.  Passing by value (not using the “&”) is like giving a copy of the data to the function and expecting brand new data back.  

In the first program, the plan worked.  We said to the function, “Here’s two references to variables, n1 and n2.  Go find them and do your thing on them.   But don’t take them; When you’re done, leave them right were they are.”

In the second program, we screwed up.  We said “Here’s two copies of my variables, go do your thing on them.”  But we never got any data back because the function doesn’t have a return statement. Even if the function did have a return statement, it could only return one value.

Once you give something to a function, you never get it back.  You can get back something else, if the function has a return statement, but the data you gave to the function will die when the function ends..  

Rewrite the following while loop as both a do-while and a for loop.  Write in C++, not pseudocode.

i=5;
while (i >=1)
{ 
	cout << i << endl;
	i = i-1;
}

A do-while loop:

I actually screwed up my do -while loop on the last test, but I got it right now. You can trust me.

i=5;

do 

{

	cout <<i << endl;

	i = i-1;

} while (i >=1);




A for loop:

for(i=5; i>=1; i=i-1)

{

	cout << i << endl;

}




A do-while is similar to a while but the test is at the end of the loop instead of the beginning. Remember to put the semicolon after the test on the do while loop but not on the while loop.

A for loop has the initialaztion, test, and the update of the contol all in one line.

What is the output of the following code?

#include 
void proc4(int &, int)
int main()
{
	int D, E;
	D=27; E=18;
	proc4(D,E);
	cout <<"After call in main" << D << " " << E << endl;
	return 0;
}
void proc4 (int & r, int s)
{
	cout << " Beginning of function " << r << " " < < s << endl;
	r=41;
	r=63;
	cout << " end of function " << r << " " < < s << endl;
}

Beginning of function 27 18
end of function 41 63
After the call in main 41 18

This is another exercise in by-reference vs by value.  Main called proc4 and passed two parameters to it.  The first was called D, and the second was called  E.  

For the fist parameter, proc4, using the “&”, took the parameter as a reference.  It said, “I need to find this variable and do something with it”.  For the second parameter, proc4 took it as a value.  It said, “I got a variable to keep for the rest of my life”.  

It doesn’t matter that main and proc4 called these variables by different names.    I call my son “Bill”,  but where he works, everyone calls him “Billy”.  It’s the same guy with different names.  On the other hand, there’s another guy were he works also named “Bill”, but he’s not my son.  Different guy; same name.  They don’t have the same name in the same place, which is why they call my son “Billy”.  He would prefer they called him “William” but that’s beside the point.  The point is, don’t worry about the names.  Worry about the order they were passed and whether they were passed by reference or value.

So proc4 took a reference to one variable and a copy of another.  The variable that it had a reference to, it called r.  It had to find r but it couldn’t take it away.  The copy of the other variable, it called s.  It owned that copy and never give it back.

The first thing that proc4 did was cout the values of r and s.  Then proc4 changed r, and left it where it found it.  Proc4 also changed s, but s was proc4‘s own personal copy.  

Proc4 then couted the new values of r and s.  Having done it’s thing, Proc4 ended and took its copy of s with it.  

Meanwhile, back at main, after calling proc4, D was changed.  That’s because D was the variable that proc4 called “r”.  It was the variable that proc4 found, changed, and left in place.  But nothing happened to E.  Proc4 had a copy of E, which it called s, but proc4 never had the original E.

Given the following code, answer the questions after it

#include 
void proc4 (int &, int);  // function prototype 


int main ()
{
	int D, E;
	D = 27; E=18;
	proc4 (D,E);  //function call
	cout << "After call in main " << D << " " << E << endl;
	return 0;
}
 void proc4(int & r, int s)
// function body starts here
{
	cout << " Beginning of function " << r << " " << s << endl;
	r=41;
	s=63;
	cout << " end of function " << r << " " << s << endl;
}// function body ends here

What are the actual parameters? D and E

What are the formal parameters? r and s

I don’t like the terminology, but I wasn’t there when they were passing out names. Just remember that the actual parameters are in the call. Actual:Call.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: