CISC106-010,
Fall 2007 12/7/2007
Practice
Questions for the Final Exam
Instructions:
To get most out of this
exercise, first do the questions yourself; then click here to check your
answers.
Contents
Look at the practice questions at http://www.udel.edu/CIS/106/iaydin/07F/practice/practice_2007_12_3.txt
Q1. Assume we have a C++ file named silly1.cc. The content of silly1.cc is below. There are three problems in silly1.cc that will cause compile-time errors (i.e., the file will not compile). Find out what these errors are.
#include <iostream.h>
using namespace std;
int main(){
i = 55;
cout >> endl >> "my number
is: " >> i >> endl
return 0;
}
Q2. Assume we have a C++ function isSeven() with the content as below. There are two errors in this function. One of the errors is a compile-time error (i.e., the function will not compile correctly) and the other one is a run-time error (i.e., the function will not work correctly, eventhough there were no errors during the compilation). Find out these two problems.
//
// isSeven() function
returns true if the number i is 7
// inputs:
// i: the number to test
// outputs:
// true if i is 7 else return false.
//
bool isSeven(int i)
{
bool result;
result = false;
if (i = 7)
result = true;
}
Q3. Making a C++ program interactive. Assume we have a C++ file named even.cc that includes a main() funtion and a function named isEven(), as below. Modify the main() function to make the program work interactively. That is, receive a number from the user and then output on the screen to if the number is even or odd (hint: you can use cout function to prompt to the user for a number and then cin function to read the user input).
#include
<iostream.h>
using namespace std;
// declaring the function
prototype
bool isEven(int i);
int main(){
int i = 7 ;
if ( isEven(i) )
{
cout << endl << "number
" << i << " is an even number" << endl ;
}
else
{
cout << endl << "number
" << i << " is an odd number" << endl ;
}
return 0;
}
//
// isEven() returns true
if number i is an even number
// inputs:
// i : the number to check
// outputs:
// return true if the number is even, false
otherwise
//
bool isEven(int i)
{
bool result = false;
//
// note that percent sign % is the
//
aritmetic mod (remainder) operator in C++
// For example,
//
// 5 % 3 is equal to 2
// 4 % 2 is equal to 0
//
// if a number is divisible by 2 (i.e., the
remainder
// is zero) then the number is an even
number
//
if (i % 2 == 0)
{
result =
true;
}
return result;
}
Q4. Writing a C++ function by using (calling) another C++ function. Write a function named isOdd() using the function isEven() in question above.
The function prototype for isOdd() is given below, fill in the rest of the function.
bool isOdd(int i)
{
}
Q5. Writing a loop in C++. Write a for or while loop in C++ that will print all the integers that are less than 10 to the screen (make sure you define your variables before using them).
Q1. Converting a MATLAB function to a C++ function. We have a function M-file named mysum.m. The content of the M-file is below.
function sum = mysum(x)
% inputs:
% x: a vector
% outputs:
% sum: the sum of the elements in vector x
sum = 0;
n = 1; % the loop counter
while n <= length(x)
sum = sum + x(n);
n = n+1;
end %end of while
return;
end % end of the function
Write a C++ function that does perform the same task on an integer array. The function prototype of the C++ function is given below. Fill in the rest of the function.
// addElement() function:
returns the sum of the integers
// in integer array x
// inputs:
// x: the integer array
// len: the number of elements
in the array
// outputs:
// sum of the integers in integer array x
//
int addElements(int x[],
int len)
{
}
Q2. Calling a function in MATLAB. In MATLAB, assume we have a vector v, we want to use mysum function M-file in the previous question to calculate the sum of the elements in a vector v. Complete the MATLAB session below (i.e., write a function call for mysum function)
>> clear
>> v = [15 0 -5 10]
>> % write the function call here
Q3. Calling a function in C++. We have the following .cc file. We want to calculate the sume of the elements in an integer array A. How can we use (i.e., call ) the function addElements(), in the previous question, to perform the summation? Complete the main() function below.
int main(){
const int SIZE = 4;
int A[SIZE] = {5, 60, -10};
int sum;
// write the function call below
sum =
cout << endl << "Sum of the elements of array A
is: " << sum << endl;
return 0;
}
Q3. Converting a loop in MATLAB to a loop in C++. We have the MATLAB code piece below:
i = 3;
while (i>=0)
fprintf('o');
i = i - 1;
end
fprintf('x\n');
Understand what this code does (what is the screen output) and then write a while loop in C++ that performs the same task (hint: you can use cout to print to the screen).
Q4. Converting a loop in MATLAB to a loop in C++. We have the following MATLAB code piece.
for i=5:1
fprintf('o\n');
end
fprintf('x\n');
Understand what this code does (what is the screen output) and then write a for loop in C++ that performs the same task (hint: you can use cout to print to the screen).