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!


  // 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:






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:



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%]





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.

bool 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;

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;