CS220-ASMT#04

advertisement
CS220 Introduction to Computer Science
Spring 2009, Dr. Sheldon Liang
Homework & Quizzes #04
(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
Pre-defined function library is provided by C++
Extensibility of C++ is embodied by self-defined functions
A Function is the basic mechanism that fulfills algorithm 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/6
Session 2: Computations: Algorithms and Functions
Chapter 4: Top Down Approach: Function & Declaration
 Pre-HW&Q: Review and Think
Learning of this assignment:




Input - Argument List (formal parameter): right type, right order, right value
Process – Identifier tells functionality whose detail is hidden from the use
Output – the function is expected to return result after being called with inputs
Implementation of functions
1: A function can perform specific functionality, but the most important thing is to pass right
and reasonable parameters (arguments) into the function. Similarly, a coffee maker can make
coffee, but the pre-condition is that you need to put right and quality ground coffee or coffee
beans.
Argument list describes what parameters are needed to pass, parameters, pretty much, like
variable declaration: start with data type and argument (variable) follows. Argument list or
parameters can be seen as variables that are applied to the function.
float sqrt (float v);
// “float v” means a value of float is required to be
// passed to the function in order to calculate the sqrt
// (squire root)
2: The function will perform certain functionality (or process) which has semantic meaning, but
the identifier that names the function will describes the meaning.
float sqrt (float v);
// the identifier that names the function tells
// the meaning – what functionality is to be performed
3:
The function is expected to return the result after its being called. The return type
plays an important part in transferring computational result to the caller that asks function
by calling it.
float sqrt (float v);
// return type tells what is expected to be returned
// with another float data as input.
4:
Implementation of function means the fulfillment of certain algorithm that does (relatively)
perfect job. The designer of the function should be careful to give full consideration to all kinds
of possibilities that would bring the function to a death.
float sqrt ( float v )
{
float f;
// input through argument list
// local / temporary variables
.. // implementation of algorithm
return f;
}
// process
// output form the function, you can’t miss it
2/6
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. A function may return more than one item
-T
-F
Answer: FALSE
2. function naming rules follow variable naming rules
-T
-F
Answer: TRUE
3. the types of parameters are optional in the function declaration
-T
-F
Answer: FALSE
4. the parameter names are mandatory in the function header
-T
-F
Answer: FALSE
5. it is possible to have a function that has no parameters
-T
-F
Answer: FALSE
[5 x 1]
3/6
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. #include <cmath> is known as an ___________________.
ANSWER: include directive
2. Converting from one type to another is called ______________.
ANSWER: casting
3. In the following function declaration, the variable size is known as a
_________________.
int myFunction ( int size);
ANSWER: (formal) parameter
4. The ______________ describes how the function will work.
ANSWER: function body
5. When you want to use a function in your program, you would make a function
__________.
ANSWER: call or invocation
[5 x 1]
4/6
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. What is the value of x after the following code fragment executes?
float x = 36.0;
x = sqrt(x);
a. 36.0
b. 6.0
c. 3.0
d. 2.456
ANSWER: B
2. the fabs(double num) function
a. returns the most fabulous number
b. returns the largest whole number <= num
c. returns the negative value of num
d. returns the absolute value of num
ANSWER: D
3. If you need to write a function that will compute the cost of some candy, where
each piece costs 25 cents, which would be an appropriate function declaration?
a. int calculateCost(char name);
b. char calculateCost(int count);
c. int calculateCost int count;
d. int calculateCost(int count);
ANSWER: D
4. What is the value returned by the following function?
int function()
{
int value = 35;
return value + 5;
value += 10;
}
a. 35
b. 40
c. 50
d. 10
ANSWER: B
5. When parameters are passed between the calling code and the called function,
parameters and arguments are matched by:
a. their data types
b. their relative positions in the parameter and argument lists
c. their names
d. they are not matched up at all.
5/6
Session 2: Computations: Algorithms and Functions
6. If you have the two functions as shown,
int someFunction(int value);
float someFunction(float value);
and a variable x, which is a double, which function is called by the following
statement?
cout << someFunction(x);
a. void someFunction(int value);
b. void someFunction(float value);
c. Nothing, it is a syntax error
d. both functions are called
ANSWER: C
7. Which of the following are valid function calls to the fabs function?
a. fabs(3.5);
b. cout << fabs(3.5);
c. cin >> fabs(3.5);
d. fabs(cin >> x);
e. a,b and c
f. a and b
ANSWER: F
8. Multiple arguments to a function are separated by
a. comments
b. semicolons
c. colons
d. commas
e. periods
ANSWER: D
9. The functions pow(), sqrt(), and fabs() are found in which include file?
a. cstdlib
b. cmath
c. iostream
d. regular
ANSWER: B
10. The expression static_cast<double>(3) is called a
a. type cast
b. nultiplier
c. doubler
d. polymorphism
ANSWER: A
6/6
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