Name:
Student ID:
DUE: 10 PM Thursday, November 7. Email instructor a text
file that contains your answers. Use "EE261 Quiz3" as subject of your
email.
1. Write a loop to sum up all odd numbers between 0 and a given upper
bound alpha, inclusive. For
example, if alpha = 5, then the sum should be 9 (1+3+5). [1%]
...
int main(){
int alpha;
cin >> alpha;
if (alpha < 0){
cout << "alpha cannot be negative. Quit."
<< endl;
return 1;
}
int sum;
// add your loop here!
sum = 0;
int odd = 1;
while (odd <= alpha){
sum += odd;
odd = odd + 2;
}
// output
cout << "The sum of all odd numbers between 0 and "
<< alpha << ", inclusive, is " << sum <<".\n";
return 0;
}
2. The following code snippet is designed to count the number of blanks
contained in a text file named "data.in", but it prints out 41 instead
of the expected value 9.
What
is wrong with it? [0.5%]
List out all the global variables, local variables, function calls,
arguments, and parameters that appear in the code snippet. [0.5%]
...
1 int countBlanks (ifstream
& inFile){
2 int count = 0;
3 char c;
4 inFile.get( c );
5 while ( inFile )
6 {
7 if
(c == ' '); {
8
++count;
9 }
10 inFile.get( c
);
11 }
12 return count;
13 }
14
15 ifstream inFile;
16 int numOfBlanks;
17 int main (){
18 inFile.open("data.in");
19 int
numOfBlanks = countBlanks(inFile);
20 cout
<< numOfBlanks << endl;
21 return 0;
22 }
The file "data.in" contains the following content, where exactly one
blank is used to separate each pair of words.
=======BEGIN of data.in===========
To Be or Not to Be, This is the
Question.
========END of data.in============
Answer:
There is an extra ; on line 7.
global variables: inFile on line 15, numOfBlanks on line 16
local variables: count, c, numOfBlanks on line 19
function calls: countBlanks() on line 19, inFile.get(c) on lines 4 and
10, inFile.open("data.in"), cout <<, (inFile) on line 5. It is
enough for full credit if you answered the first one.
arguments: inFile on line 19, "data.in" on line 19, c on lines 4 and
10, inFile on line 5, numOfBlanks and endl on line 20. It is enough for
full credit if you answered the first one.
parameters: inFile on line 1
3. Describe the output of the following two programs designed to solve
the same problem (suppose 100 is the expected output value). Explain
what causes the difference. [1%]
version A:
int result;
// precondition: e>=0
// postcondition: result = x^e
int power(int x, int e){
result = 1;
while (e != 0){
result = result * x;
--e;
}
return result;
}
int main(){
result = 100;
int oneThousand = power(10, 3);
// what is to be printed out?
cout << result << endl;
return 0;
}
version B:
// precondition: e>=0
// postcondition: result = x^e
int power(int x, int e){
int result = 1;
while (e != 0){
result = result * x;
--e;
}
return result;
}
int main(){
int result = 100;
int oneThousand = power(10, 3);
// what is to be printed out?
cout << result << endl;
return 0;
}
Answer:
Version A defines result as a global variable, which is modified by
power() as side effect. Version B defines result as a local variable,
which helps achieve the expected state.
4. Write a function named largestInteger,
which takes a parameter bound
of int type and yields the largest integer k so that k*k*k <= bound. You are not
allowed to use standard functions such as pow(). You can assume that
bound is not negative. It is up to you to decide whether to use value
parameters or reference parameters. [1%]
int largestInteger(int bound){
int k = 0;
while
((k+1)*(k+1)*(k+1) <= bound){
k = k+1;
}
return k;
}
or
void largestInteger(int bound, int
& k){
k = 0;
while
((k+1)*(k+1)*(k+1) <= bound){
k = k+1;
}
}
5. The four code snippets below all define a boolean variable isLeapYear, whose value should be
true at the end when the value of year indeed represents a leap year.
Convert the correct version(s)
into a value-returning function with the following function prototype.
boolean isLeapYear (int year) {
// fill in body here!
}
Note: the definition of a leap year can be found on Page 33 of the
book. [1%]
Your program must compile and run correctly to receive credit.
int year;
cin >> year;
bool isLeapYear;
A.
if (year % 4)
isLeapYear = false;
else if (year % 400==0)
isLeapYear = true;
else if (year % 100)
isLeapYear = true;
else
isLeapYear = false;
A is correct.
bool isLeapYear (int year) {
// fill in body here!
bool isLeapYear;
if (year % 4)
isLeapYear = false;
else if (year % 400==0)
isLeapYear = true;
else if (year % 100)
isLeapYear = true;
else
isLeapYear = false;
return isLeapYear;
}
B.
isLeapYear = false;
if (year % 4 == 0)
if (year % 100 == 0)
if (year % 400 ==
0)
isLeapYear
= true;
else
isLeapYear = true;
C.
isLeapYear = false;
if (year % 4 == 0)
if (year % 100) {
if (year % 400 ==
0)
isLeapYear
= true;
}
else
isLeapYear
= true;
D.
isLeapYear = year % 4 ==0 &&
year % 100 !=0 || year % 400 == 0;
D is correct.
bool isLeapYear (int year) {
// fill in body here!
return year % 4 ==0 &&
year % 100 !=0 || year % 400 == 0;
}