Benha University
2nd Term (June 2011) Final Exam
Class: 1st Year Students
Subject: : Fundamentals of Structured Programming
Faculty of Computers & Informatics
Date: 12/6/2011
Time: 3 hours
Examiner: Dr. Walid Osamy
Answer the following questions:
Question No. (1) (15points) True /False
a) The individual elements of an array are accessed and indexed by unique
numbers. b) An array's size declarator can be a literal, a named constant, or a variable. ×
c) The binary search algorithm repeatedly divides the portion of an array being searched
in half. d) Two-dimensional arrays may be passed to functions, but the row size must be
specified in the definition of the parameter variable. ×
e) Arguments are passed to the function parameters in the order they appear in the
function call. f) It is not possible for a function to have some parameters with default arguments
and some without. ×
g) The exit function can only be called from main function. ×
h) The '&' operator dereferences a pointer. ×
i) The address operator is not needed to assign an array's address to a pointer. j) A semicolon is required after the closing brace of a structure declaration. k) A structure declaration does not define a variable. l) Prototype statements are placed before main() function. m) A driver is a dummy function that is called instead of the actual function it represents.
×
n) In unstructured programming all code is contained in a single continuous block which
makes it difficult to modified and hard to incorporate other code. o) The three basic control structures used in programming are input, output, and
calculation. ×
Page 1 of 8
Question No. (2) (10points) Find the Error
Each of the following definitions and program segments has errors. Locate as many as
you can.
a) char name [17] = "George washington";
int numbers[5] = (1, 2, , 4, , 5};
b) int table[10];
for (int x = 0; x < 20; x++)
{
cout << "Enter the next value: ";
cin >> tab1e(x) ;
}
c) int x, *ptr;
ptr = &x;
ptr = 100 ; // Store 100 in x
cout << x << endl;
d) struct TwoVals
{
int a, b;
};
int main ()
{
TwoVals . a =10;
TwoVals . b =5;
}
e) double average(int valuel, value2, value3)
{
double avg;
avg = value1 + value2 + value3 / 3.0;
missing retrun
}
Page 2 of 8
Question No. (3) (20points)
a) An application uses a two-dimensional array defined as follows.
int days[30][20] ;
i.
ii.
iii.
iv.
How many rows does the array have? 30
How many columns does the array have? 20
How many elements does the array have? 600
Write code that sums each row in the array and displays the results.
for(int row=0;row<30;row++)
{
double rsum=0;
for(int col=0;col<20;col++)
rsum+=days[row][col];
cout<<"\n Sum of row no. ("<<row<<") is: "<<rsum;
}
b) Look at the following code.
char name[10] ;
int s;
strcpy(name, "Jimmy") ;
s = strlen ( name);
What value will be stored in s after the code executes? 5
c) Look at the following code.
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the
value variable. cout<<*ptr;
d) Look at the following array definition.
int numbers[] = { 2, 4, 6, 8 , 10 };
What will the following statement display?
cout << *(numbers + 3) << endl;
will display 8
e) Look at the following structure declaration.
struct Point {
int x;
int y;
Page 3 of 8
} ;
Write statements that
i.
define a Point structure variable named center Point center;
ii.
assign 12 to the x member of center center.x=12;
iii.
assign 7 to the y member of center center.y=7;
iv.
display the contents of the x and y members of center
cout<<center.x<<" "<<center.y;
Question No. (4) (20points)
a) Why do local variables lose their values between calls to the function in which they
are defined? (Illustrate your answer with an example.)
A function's local variables exist only while the function is executing. This is known as the
lifetime of a local variable. When the function begins, its local variables and its
parameter variables are created in memory, and when the function ends, the local
variables and parameter variables are destroyed. This means that any value stored in a
local variable is lost between calls to the function in which the variable is declared.
b) i. Assume that tempNumber is a pointer that points to a dynamically allocated array.
Write code that releases the memory used by the array. delete [] tempNumber
ii. Is an array passed to a function by value or by reference? By reference
c) Write a function named half that uses a reference parameter variable to accept a
double argument. The function should prompt the user to enter a number then find
the half value of that number and store the result in the parameter variable.
Page 4 of 8
d) In a program you need to store the identification numbers of 10 employees (as
integers) and their weekly gross pay (as doubles).
i. Define two arrays that may be used in parallel to store the 10 employee
identification numbers and gross pay amounts.
ii. Write a loop that uses these arrays to print each employee's identification
number and weekly gross pay.
int ID[10];
double pay[10];
cout<<"country name"<< "\t population \n";
for(int ii=0;ii<12;ii++)
{
cout<< ID [ii]<<"\t \t \t"<< pay [ii]<<endl;
}
Question No. (5) (25points)
a) Write a function that returns an integer and accepts a pointer to a string as an
argument. The function should count the number of characters in the string and
return that number. Demonstrate the function in a simple C++ program that asks the
user to input a string, passes it to the function, and then displays the function's
return value.
Page 5 of 8
b) Write a C++ program that can be used to gather statistical data about the number of
movies college students see in a month. The program should perform the following
steps:
i. Ask the user how many students were surveyed. An array of integers with this
many elements should then be dynamically allocated.
ii. Allow the user to enter the number of movies each student saw into the array.
iii. Calculate and display the average of the values entered.
Page 6 of 8
c) The file grades.txt contains the grades of 400 students. Each line in the file has the
format <name> <grade> where <name> is a single string and <grade> is a real number
between 0 and 100. The students are listed in some arbitrary order.
Write a C++ program which reads this file and populates two arrays names and scores
with the names and scores respectively. The program then computes and outputs to the
screen:
i. The maximum grade along with a student who has this grade.
ii. The minimum grade along with a student who has this grade.
Page 7 of 8
Page 8 of 8
© Copyright 2026 Paperzz