Anda di halaman 1dari 7

Part A (10 Marks)

1.

What is the prototype of a function pass that has a pass-by-value character


parameter and a pass-by-reference integer parameter? The function DOES NOT
return any value.
A.
void pass(char, int);
B.
void pass(char &, int);
C.
void pass(char, int &);
D.
void pass(char &, int &);

2.

The ____________________ value specifies the position of an element of a


component in an array.
A. index
B. function
C. variable
D. constant

3.

Which of the following statements will set the value of the fifth component of the
array beta to 88?
A.
beta[4] = 88;
B.
int beta[88];
C.
cin << beta[88] << endl;
D.
beta[5] = 88;

4.

Which of the following statements initialize each of the 5 elements of an integer


array named numbers to 8?
I. int numbers[5] = {8, 8, 8, 8, 8};
II. cout << numbers[5] = {8, 8, 8, 8, 8};
III. for(int j = 0; j < 5; j = j + 1)
numbers[j] = 8;
IV. for(int j = 0; j < 5; j = j + 1)
cin >> numbers[j];
A.
B.
C.
D.

I and III
I and IV
II and IV
I, III and IV

5.

The following statements will call function calc EXCEPT


A.
calc();
B.
call calc();
C.
z = calc();
D.
cout << calc();

6.

Which of the following shows the correct syntax of an if statement?


A.
if expression

B.
C.
D.

if { expression }
if ( expression )
expression if

7.

Which is not a loop structure?


A.
for
B.
do..while
C.
while
D.
repeat..until

8.

Determine the output of the following program fragment.


void main()
{
int y = 8, number = 0;
while (y < 10) {
number = number + 1;
y--;
}
cout << number;
}
A.
B.
C.
D.

2
0
The program has a problem of infinite loop.
The program has syntax error.

9.

Memory for the formal parameters and variables declared in the body of a
function
are
known
as
__________
variables.
A.
local
B.
global
C.
program
D.
function

10.

A function returning a value must specify, in its header line, the ____ of the value
that will be returned.
A.
size
B.
data type
C.
address
D.
use

PART B
QUESTION 1 - True False (10 Marks)
1. In creating C++ functions we must be concerned with both the function itself and how it
interacts with other functions, such as main().
ANS: T

2. Only after a called function requests that data be passed to it can the data be
manipulated by the function to produce a useful result.
ANS: F
3. Scope is the section of a program where an identifier is valid.
ANS: T
4. Every C++ function consists of two parts, a function header and a function body.
ANS: T
5. Initial values for the variables in the tested expression of a while statement do not have
to be initialized before the while statement is encountered.
ANS: F
6. A variable is a location in memory that holds changeable data.
ANS: T
7.

Local variable is a variable declared before the main function and accessible by any
function.
ANS: F

8.

Pass a copy of the value in the variable to the function for it to use is called pass-byvalue method.
ANS: T

9.

Individual value in an array is identified by a number called index or subscript.


ANS: T

10. A subscript must be an integer or integer expression.


ANS: T
QUESTION 2 (10 Marks)
Trace the following program and determine the output.
#include <iostream.h>
void main()
{

int gradesArray[10]= {87,68,94,100,83,78,85,91,76,87};


float average, sum=0.0;
cout<<"\tExamination Result\n";
cout<<"CSC316 Structured Programming\n\n";
cout<<"The grades are:\n";
for (int i=1;i<=10;i++)
{
cout<<"Student <<i<< : \t<< gradesArray[i-1]<<endl;
}

for (i=0;i<10;i++)
{
sum=sum + gradesArray[i];
}
average=sum / 10;
cout<<"The average of the grades is <<average<<endl;
}

(10 marks)

QUESTION 3 (12 Marks)


a)

Write a function named calculateBMI which will request the height (in meter) and weight
(in kg) from the user. Calculate the BMI by using this formula:
BMI = weight_in_kg / height_in_meter2
Then, the return the calculated BMI result.

b)

(6 marks)

Write a function named determineCategory() which will received the BMI result as the
parameter. Based on the BMI result, it will determine the BMI category by using the
following scale:
BMI result
< 18.5
18.5 24.9
25.0 29.9
> 30

BMI category
Underweight
Normal
Overweight
Obessity

Then, the function will display the BMI category.

(6 marks)

ANSWER:
c) float calculateBMI()
{
float height, weight, result;

- 1m
-1m

cout<<"Please enter your height:";


cin>>height;
cout<<"Please enter your weight:";
cin>>weight;
result=weight / (height * height);
return result;

-m
-m
-m
-m
- 1m
-1m

}
d) void determineCategory(float bmi)
{
if (bmi < 18.5)
cout<<"Your category is Underweight"<<endl;
else if (bmi >= 18.5 && bmi <=24.9)
cout<<"Your category is Normal"<<endl;
else if (bmi >= 20.5 && bmi <=29.9)
cout<<"Your category is Overweight"<<endl;
else
cout<<"Your category is Obessity"<<endl;
}

- 1m
-1m
-m
-1m
-m
-1m
-m
-1m
-m

Part C (10 Marks)


Answer ONE question only.
QUESTION 1
Write a program that read 50 numbers and then find their sum and print the numbers in
reverse order.

Answer:
// Program to read 50 numbers, find their sum, and
// print the numbers in reverse order.
#include <iostream.h>
int main()
{

int item[50];
int sum;
int counter;

// declare an array item of five components

cout<<"Enter 50 numbers."<<endl;
sum = 0;
for(counter = 0; counter < 50; counter++)
{
cin>>item[counter];
sum = sum + item[counter];
}
cout<<"The sum of the numbers is: "<<sum<<endl;
cout<<"The numbers in reverse order are: ";
//Print numbers in reverse order
for(counter = 49; counter >= 0; counter--)
cout<<item[counter]<<" ";
cout<<endl;
return 0;
}

QUESTION 2
Write a program that requires a user to enter 10 integers. Then write a function called
larger() that will take the 10 input and print the largest number.

Answer:
// Program: Largest
#include <iostream.h>
double larger(double x, double y);
int main()
{
double num; //variable to hold the current number
double max; //variable to hold the larger number
int count; //loop control variable
cout<<"Enter 10 numbers."<<endl;
cin>>num;
max = num;

//Step 1
//Step 1

for(count = 1; count < 10; count++)


{
cin>>num;
max = larger(max, num);
}

//Step 2

cout<<"The largest number is "<<max<<endl;

//Step 3

//Step 2a
//Step 2b

return 0;
}//end main
double larger(double x, double y)
{
if(x >= y)
return x;
else
return y;

Anda mungkin juga menyukai