ECE-1011 (SPRING 2003)

QUIZ #5 PREP SHEET

 

NOTE: Some of these questions/statements require you to think beyond the strict information given in the text.

(p 165) Functions may be called or invoked, from where? Can a programmer-defined function call a library function, or another programmer-defined function?

Function may be called from the main function or any other programmer-defined function.

(p 165) What is the difference between a function definition and function prototype?

A function prototype, informs the compiler that a programmer-defined function will be used.

A function definition is the complete coded function, including function type, name, arguments as well as executable statements to perform the function.

(p 166) Does a function require a return a value? If not, what should the type definition be?

No, if the function does not return a value, void should be used for the type function and the return statement should look like this: return;

 (p 166) Does a function require parameters? If not, what should the parameter type definition(s) be?

No, if the function does not require arguments a void should be placed in the argument position.

 (p 166) What are the three items that must be defined in a function definition?

Function type, function name, function arguments (parameters) along with type and variable name.

(p 170) If a function is invoked, before the function definition (a programmer-defined function), what must occur in your code before calling (invoking) the function?

A statement of the function prototype must be coded.

(p 171) Where are the function prototypes for the C-functions located, if you had many programmer-defined functions where would you put the prototypes?

C-library prototypes are located in the C-library header files. A user defined header file.

(p 172) What is the difference between a formal and actual parameter?

A formal parameter is the variable defined in the function definition, an actual parameter is the value that parameter takes on when the function is invoked.

 (p 172-3) What does it mean to have a coercion of an argument? What if the actual parameters were out-of-sequence in the calling function?

Argument types passed are not the same as defined by the function, the actual value of the argument will take on values as defined in chapter two of text.

Example: What if the following function is called as indicated, what will be the actual values used in the function?

int m = 10;

double = y,z = 13.2;

y = my_func(m, z);

double my_func(double x, int k)

{

                statements;

                return(1);

}

( answer x = 10 and k = 13)

(p174) What does it mean to have parameters passed as call-by-value (reference-by-value) and call-by-reference (reference-by-address)?

The first means the actual value is passed, the second means the address location of the argument is passed (more on that later in the class).

 (p174-5) Define the scope of a function or variable.

The scope of a function or variable is defined as the portion of the program that the function or variable is visible or accessible.

(p 175) List the storage classes. Automatic, External, Static, Register

(p 175) Define a local variable. A local variable’s scope is only within the function it is defined in.

(p 175) Define a global variable. A global variable’s scope is within the entire program (provided the keyword extern is used to define within each function it is used).

 (p176) Define a static variable. A static variable exists throughout the execution of the program, even when the function, that defined it, is not executing.

Random Numbers:

(na) The rand function returns an integer value between 0 and RAND_MAX and is defined as a pseudorandom number generator. This means that the random numbers will eventually repeat in sequence. In addition, the numbers are referred to as being Uniformly Distributed, meaning that any integer number is likely (probability wise) to be returned from the function. Some random number generators return certain numbers more than others, a practical example is a Gaussian distributed random number generator. Random numbers are used in program simulation to create  more “real-world” data for the simulation i.e, noise. Random numbers or noise can not be defined by an equation, but can be characterized by the distribution of their values. Those character values are most often denoted by, mean (average), median (mid-point value), variance (spread of numbers around the mean, in signals this is viewed as power of the signal).

(p177) Write a complete function that passes a seed value (if seed value is 0, does not change seed), starting limit, ending limit and returns a floating-point uniform random number.

double rand_f(int seed, double start, double stop)

{

            if (0 != seed) srand(seed);

            return (((double) rand()/RAND_MAX )* (stop-start) + start);

}

 (p 181) When calculating floating-point random numbers why is it necessary to divide by RAND_MAX?

Dividing by RAND_MAX normalizes the rand function returned value, that is no number is larger than 1. With a 0 -1 distribution it may be multiplied by the width of the random numbers desired and then added to the offset required.

Monte Carlo Simulation:

(section 4.4) A program written to simulate a real-world problem and uses random numbers is referred to as a “Monte Carlo” simulation. Note: Do not confuse this with the Monty Hall problem that you are working on!