CS220-ASMT#05

advertisement
CS220 Introduction to Computer Science
Spring 2009, Dr. Sheldon Liang
Homework & Quizzes #05
(20 points)
Due Date: One Week Away from today
(Look at schedule in the syllabus)
Your Name: _______________
Your Score: ___________
Objectives:
 A top-down approach is essentially breaking down a system to gain insight
into its compositional sub-systems. In a top-down approach an overview of
the system is first formulated, specifying further process by calling
existing functions or by refining functions that are to be declared.
√
√
√
√
A function represents an abstraction of process
Call to a function focuses on IPO characteristics
Extensibility of C++ is embodied by self-defined functions
Self-defined functions reflect the core idea of programming in C++
This week’s homework & quizzes consists of three parts:
A. True / False
[5 pts]
B. Short Answer
[5 pts]
C. Multiple Choice
[10 pts]
Make sure your name is on this handout before turning it in

Since we stress “learning through lecturing and reading”, some questions
designed for homework & quizzes stimulate students to listen, think, and read for
the sake of deep learning. Be careful to locate find answer from the handouts and
text or lab-testing (that is, seek answer through program). 
 Trying your best in seeking answers will help you a lot.
 Leaving questions unanswered would earn you -1 point for each.
1/7
Session 2: Computations: Algorithms and Functions
Chapter 4: Top Down Approach: Function & Declaration
 Pre-HW&Q: Review and Think
Learning of this assignment:

A header / prototype shows two concerns:




How to call the function in the right way to its caller
How to further evolve the implementation to its definer
void is an empty type that won’t return anything to the caller
There are two parameter-passing modes in C++


Call by value  a formal argument gets a copy of a value
Call by reference a formal argument gets the location of a variable
1: ”How to call the function in the right way” is the first priority to both the caller and the
definer of function. In the first place, the definer needs to give a clear header that includes three
important aspects:
 Return type: what expects to be returned from a call to the function
 Functionality: what is going to be done by the call to the function
 Formal arguments: how to input your data while the call to the function
”How to further evolve the implementation” is very intentional when defining the header or
prototype. The header is not used for implementation in detail, but the definer is supposed to try
his/her best in making a blueprint by means of comments. A sample is given to show how to
comment a header for further implementation:
/*******************************************************
* This is a function that returns the squire root
*
input:
a float value
*
process:
sqrt
*
output: float
*-----------------------------------------------------* Comments:
*
since the squire of a arithmetic number can not be negative,
* this function has to refuse to accept a negative number as an input.
* a safe way for you to call this function is like follows:
*
#include <cmath>
*
... ...
*
float result = sqrt (fabs (v));
* or
*
float result = sqrt ((v >= 0.0)? v : -v);
********************************************************/
float sqrt (float v);
2:
Sometimes, we expect a function to perform a certain operation without data to be
returned. C++ pre-defines an empty data type void which means no data expects to be
returned, for instance:
void take_a_nap(amt_of_minutes); // no data to be returned
3: ”call by value” mode allows the formal argument to own a copy of the passed value,
so its corresponding real argument can be either a constant or a variable.
With call-by-value, the function in its body is free to use the formal argument as a
local variable. Any modification won’t affect the real argument.
“call by reference” is different from “call-by-value”. The real argument will pass the
location to the formal one, so the real argument must be a variable. With call-byreference, any modification will be brought back to the real argument because the
function will directly manipulate the real argument via its reference (location).
Example:
void swap (int&x, int& y);
2/7
Session 2: Computations: Algorithms and Functions
 In-HW&Q: Work and Quizzes
Q1. TRUE/FALSE (Check the box) [5 pts]
/* Check the box of T or F, and explain when it is F */
1. Functions can only have one return statement.
-T
-F
FALSE
2. A call to a function may bring you more than one result back
-T
-F
TRUE
3. Variables that are declared outside of any function body or parameter list are
considered global.
-T
-F
TRUE
4. A “call-by-reference” formal argument suggests that its corresponding real
parameter be either a constant or a variable.
-T
-F
FALSE
5. In a function with call-by-reference parameters, any changes to the formal
parameters will change the actual arguments passed to the function.
-T
-F
TRUE
[5 x 1]
3/7
Session 2: Computations: Algorithms and Functions
Q2. Short Answers (5 pts)
/* When you feel like more than one answer, write them all, some wrongs won’t make
you loss points. You are encouraged to do this way, which means multi-perspectives
about the same answer is encouraged
*/
1. The _________ of a variable is where that variable can be used.
ANSWER: scope
2. The black box analogy demonstrates the concept of _________________.
ANSWER: information hiding or procedural abstraction
3. In the following function declaration, the formal argument size is passed in
________________ mode.
int myFunction ( int& size);
call-by-reference
4. A function that does not return a value is known as a ________ function.
ANSWER: void
5. Write the code to convert the value in an integer variable named count to a
double. _____________________.
ANSWER: static_cast<double>(count)
[5 x 1]
4/7
Session 2: Computations: Algorithms and Functions
Q3: Multiple Choices [10 pts]
[10 x 1]
/* Try you best in explaining why some other choices would be wrong. You are
encouraged to do this way, which means you not just know what is right, but also
what is wrong. This may help you to earn extra points */
1. Using functions in a program is known as
a. data abstraction and information-hiding
b. procedural abstraction or top-down approach
c. poor programming style
d. calculus
ANSWER: B
2. If you have the following variable declaration in your program,
const int SIZE=34;
then which of the following statements are legal?
a. SIZE ++;
b. x = SIZE--;
c. cout << SIZE;
d. cin >> SIZE;
ANSWER: C
3. In the function declaration shown, the mechanism used to call this function is
known as:
double pow(double base, double exp);
a. pass by name
b. call by value
c. pass by reference
d. call by name
ANSWER: B
4. What is the value of i after the following function call?
//function definition
int doSomething(int value)
{
value = 35;
return value;
value = 13
}
//fragment of main program
int i=0;
cout << doSomething(i);
a. 13
b. 35
c. 48
d. 0
ANSWER: D
5/7
Session 2: Computations: Algorithms and Functions
5. You should make a parameter a reference parameter if:
a. You need the function to change the value of the argument passed to the
function.
b. you need to be able to change the value of the parameter in the function,
but not the value of the argument.
c. Always.
d. If any of the other parameters are reference parameters.
ANSWER: A
6. Which of the following are not legal function declarations?
a. int ave3(int a, int b, int c);
b. int 3ave(int a, int b, intc);
c. int ave3(int, int, int);
d. int ave_3(int a1, int a2, int a3);
ANSWER: B
7. Which of the following are true?
a. a function can call another function.
b. as long as the function is defined anywhere in your program, it can be
used anywhere else.
c. a function definition can contain another function definition.
d. if you have function prototypes, the order in which you define your
functions is important.
ANSWER: A
8. When the function below is called, the _____ of the actual parameters is passed to
the function definition.
double sqrt(double value);
a. name
b. value
c. address
d. scope
ANSWER: B
9. Given the function definition
void something ( int a, int& b )
{
int c;
c = a + 2;
a = a * 3;
b = c + a;
}
what is the output of the following code fragment that invokes something?
(All variables are of type int.)
r = 1;
s = 2;
6/7
Session 2: Computations: Algorithms and Functions
t = 3;
something(t, s);
cout << r << ' ' << s << ' ' << t << endl;
a. 1 14 3
b. 1 10 3
c. 5 14 3
d. 1 14 9
e. none of the above
ANSWER: A
10. Given the following function declaration and local variable declarations, which of
the following is not a correct function call?
int myInt;
float myFloat;
char ch;
void someFunction(int& first, float second, char third);
a. someFunction(1, 2.0, ch);
b. someFunction(myInt, myFloat, ch);
c. someFunction(myInt, 2.0, 'c');
d. someFunction(myInt, myFloat, '1');
ANSWER: A
7/7
Session 2: Computations: Algorithms and Functions
 Post-HW&Q: Questions raised from assignment
Q1: ____________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
A: (by Sheldon) __________________________________________________________
Q2: ____________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
A: (by Sheldon) __________________________________________________________
Q3: ____________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
A: (by Sheldon) __________________________________________________________
Download