Syntax of a value-returning function

advertisement

Midterm: IST 153 Fall 2003 Name Date

Each question is worth 2 points.

1.

An employee who telecommuting works from home and uses a microcomputer to communicate with the office.

2.

Hardware refers to the physical components of the microcomputer system.

3.

Software refers to the programs that tell the hardware how to perform a task.

4.

Object-oriented languages view a problem solution as a set of interacting objects.

5.

In top-down design, the programmer breaks the program into one or more task .

6.

Procedure-oriented languages need a(n) compiler to translate the high-level instructions into machine code.

7.

Another name for the selection structure is the decision structure.

8.

List, define, and give an example of each of the control structures discussed in this tutorial.

NOTE: Write essay answers on separate page.

Sequence structure: directs the computer to process the instructions, one after another, in the order listed in the program.

Repetition structure: directs the computer to repeat one or more instruction until some condition is met.

Selection structure / decision structure: directs the computer to make a decision , and then selects an appropriate action to take base on that dicision.

9.

List the steps involved in the problem-solving process used to create a computer program.

Analyze the problem

Plan the algorithm

Desk-check the algorithm

Code the algoritm into a program

Desk-check the algorithm

Evalute and modify (if necessary) the program

Midterm Page 1 Version 1

10.List and define the types of program errors that can occur. Which are easier to find?

Syntax error: occurs when you vialate one of the rules of the programming language, referred to as the language’s syntax. Syntax error is much easier to find.

Logic error: occur for a variety of reasons-such as mistyping a keyword or variable name, forgetting to enter a semicolon at the end of a statement, neglecting to enter an instruction, or entering the program instruction in the wrong order.

11.

Variables are computer memory locations that the program will use while running.

12.The set of rules that you must follow to use a language is called its syntax .

13.The line #include <iostream>is called a(n) directives .

14.A(n) function is a block of code that performs a task.

15.Every C++ program must have a(n) main function.

16.Assigning a beginning value to a memory location is referred to as assignment statement .

17.The C++ compiler may display the warning message "initializing: truncation from const double to float" when you initialize a float memory location to a number other than decimal .

18.Write a C++ instruction that reserves a named constant named PASSMARK. The data type is int, and the value is 75. const int PASSMARK = 75;

19.Write a C++ instruction that reserves a variable named testScore. Assign the data type char to the variable, and initialize it appropriately. char testScore = ‘ ’;

20.Write a C++ instruction that reserves a variable named percentInt, which will hold values such as 3.7, 2.9, and 5.8. Initialize it appropriately. float percentInt = 0.0;

21.To get a string of characters that contains a space, you use the C++ getline function.

22.The function header file contains the instructions needed for input and output operations in a C++ program.

23.Cmath and string are examples of library files.

24.A(n) void function does not return a value.

25.Write the C++ statement that multiplies the number 5 by itself four times and stores the result in an int variable named score. int score = pow(5,4);

Midterm Page 2 Version 1

26.Write the C++ statement that multiplies the square root of the number 100 by itself four times, and stores the result in an int variable named score. int score = pow(sqrt(100),4);

27.To ensure that the random number generator always is initialized with a unique number, most programmers use the built-in srand function as the seed value.

28.Write the C++statement that will display an integer between 14 and 60 on the screen.

Cout << 14 + rand()%(60-14+1);

29.Write the C++ statement to display three digits to the right of the decimal point for any number displayed on the screen.

Cout <<fixed;

Cout.precision(3);

30.Write the C++ statement to close the output file, testScores.dat file, which is associated with the outFile file object. outFile.close( );

// or outFile close(“testScore.dat”);

31.A(n) built in function's task is predefined and its code appears outside of the program in which the function is used.

32.Write a function header for a C++ function named payRate. The function will be passed hours, which is an integer, and rate, which is a decimal. The function will return an integer. int payRate(int hours, float rate)

33.Unless otherwise specified, variables in C++ are passed by value.

34.A variable's value indicates which portions of a program can use the variable.

35.In the case of a value-returning function, local variables remain in memory until after the computer processes the return statement.

36.What is the difference between local and global variables as they relate to functions?

Local variable : can be used only by the function in which they are declared or in whose parameterList they appear. It remains in memory until the function ends. In the case of a value-returning function, the function ends after the computer processes the return statement.

Global variable : typically are declared outside of any function in the program, and they remain in memory until the program ends. It can be used by any statement in the program.

37.Write the function header for the void function named printHead, which has two formal parameters, title and grade, which are both integers. void printHead(int title, int grade)

38.You pass a variable by reference when you want the receiving function to change the contents of the variable.

Midterm Page 3 Version 1

39.To determine whether an item is being passed by value or by reference, you must examine either the function prototype or the function header.

40.Write the function prototype for a void function named calcGrade. The function will be passed the name

(string), score1 (integer), score2 (integer), and avScore (integer) variables. The function's task is to calculate the average score and store the result in the avScore memory location. Use n, s1, s2, avg as the formal parameters. void calcGrade(string, int, int, int);

41.Write the function header for a void function named calcGrade. The function will be passed the name

(string), score1 (integer), score2 (integer), and avScore (integer) variables. The function's task is to calculate the average score and store the result in the avScore memory location. Use n, s1, s2, avg as the formal parameters. void calcGrade(string n, int s1, int s2, int avg)

42.

Write the statement to call a void function named calcGrade. The function will be passed the name

(string), score1 (integer), score2 (integer), and avScore (integer) variables. The function's task is to calculate the average score and store the result in the avScore memory location. Use n, s1, s2, avg as the formal parameters. calcGrade(name, socre1, score2, avScore);

43.

What are the differences between the syntax of a void and a value-returning function?

Syntax of a void:

Function header begin with keyword void , rater than a data type. The keyword void indicates that the fuction does not return a value.

Function body in a void function does not contain a return expression

Syntax of a value-returning function:

Function header begin with data type . It’s return a value

Function body in value-returning function contain a return expression.

44.. A(n) inner situation requires a nested decision or a nested if.

45. A decision inside of another decision is known as a(n) nested decision.

46. Programmers often use a problem-analysis tool called a(n) IPO to help organize the possible outcomes of multiple decisions.

Midterm Page 4 Version 1

47.

The variable rep in the figure below is known as a(n) counter variable .

48. Decreasing a control variable by one is known as incrementing the variable.

49 A(n) accumulator is a variable that you use to gather values.

50. A(n) counter is a variable you use to track the number of times an event has occurred.

Midterm Page 5 Version 1

Download