3.1. Exercises#

Many of these exercises are taken from past exams of ECE 244 Programming Fundamentals courses at University of Toronto. The solutions are provided in the answer boxes.

Headings in this page classify the exercises into different categories: [Easy], [Intermediate], and [Challenging]. I suggest you start by easy exercises and work your way up to the challenging ones.

Question 5 in Fall 2022 Midterm Exam [Easy]

Consider the following program.

#include <fstream>
#include <iostream>
using namespace std;

int main() {
  int a;
  ifstream inFile;
  inFile.open("input.txt");
  if (inFile.fail()) {
    return 1;
  }
  while (1) {
    inFile >> a;
    if (inFile.fail()) {
      cout << "failed.." << endl;
      inFile.clear();
      inFile.ignore(100, '\n');
      continue;
    }
    cout << "a = " << a << endl;
    break;
  }
  return 0;
}

Given the following contents of "input.txt", write the output.

  1. "input.txt"

      1
      2
      3
     
  2. "input.txt"

     a32
     b86
     3
     
  3. "input.txt"

     a32
     b86 3
     

Now you remove the infile.clear() from the code, so the program becomes:

#include <fstream>
#include <iostream>
using namespace std;
int main() {
  int a;
  ifstream inFile;
  inFile.open("input.txt");
  if (inFile.fail()) {
    return 1;
  }
  while (1) {
    inFile >> a;
    if (inFile.fail()) {
      cout << "failed.." << endl;
      // inFile.clear(); COMMENTED OUT
      inFile.ignore(100, '\n');
      continue;
    }
    cout << "a = " << a << endl;
    break;
  }
  return 0;
}
  1. "input.txt"

     1
     2
     3
     
  2. "input.txt"

     a32
     b86
     3
     
  3. "input.txt"

     a32
     b86 3
     

Question 2 in Fall 2021 Final Exam [Intermediate]

Write a C++ function void readInts() that repeatedly reads integers from the standard input (using cin) and then immediately outputs the input integer (using cout), one integer per line. When a '.' character is encountered, the function prints the message Done on a line by itself and returns. If the user enters any characters other than integer digits or the '.', the function prints the message Error on a line by itself and returns. You may assume the user will never enter eof.

Thus, for example, if the user enters 51 16 700 ., the function prints:

51
16
700
Done

However, if the user enters 101 21 13 abc 444, the function prints:

101
21
13
Error
void readInts() {

Question 3 in Fall 2018 Midterm Exam [Intermediate]

Consider the following program that uses stringstreams to read a command. The command has the following format:

count intArg

The command word is count and intArg is an integer argument. The command must have only one integer argument, e.g count 3.

The function handle_count performs the reading of the integer value. If the integer is valid, it returns true and updates the value of intArg. Otherwise, it returns false.

#include <iostream>
using namespace std;
#include <sstream>
#include <string>
// function prototype
bool handle_count( <fill in the blank> );

int main() {
  string line;
  string command;
  int intArg;
  getline(cin, line);
  stringstream lineStream(line);
  lineStream >> command;
  if (command == "count") {
    if (handle_count( <fill in the blank> )) {
      cout << "Integer argument is " << intArg << endl;
      return (0);
    } else {
      cout << "Invalid arguments" << endl;
      return (-1);
    }
  } else {
    cout << "Invalid command" << endl;
    return (-1);
  }
}
  1. Determine the number of arguments and the type of each argument and indicate them in the code above in the prototype of the function, handle_count. Further, indicate what parameters are passed to the function when it is invoked. Write your answers where indicated in the code above.

    You may not modify main by adding or removing line, other than by indicating the formal arguments in the function prototype and actual arguments of the function invocation.

  2. Write the header and body of the handle_count function below so it performs as indicated above.

Question 6 in Fall 2019 Midterm Exam [Intermediate]

For each of the following main functions, indicate the output produced in response to the user entering 1 2 3 4 five on the keyboard followed by the Enter key. Choose only one answer.

#include <iostream>
using namespace std;
int main() {
  int num = 0;
  int sum = 0;
  while (!cin.fail()) {
    cin >> num;
    sum = sum + num;
  }
  cout << sum << endl;
  return (0);
}

Choose one the following choices:

  1. 6

  2. 10

  3. 14

  4. None; the program runs in an infinite loop

#include <iostream>
using namespace std;
int main() {
  int num = 0;
  int sum = 0;
  bool more = true;
  while (more) {
    cin >> num;
    if (cin.fail())
      more = false;
    else
      sum = sum + num;
  }
  cout << sum << endl;
  return (0);
}

Choose one the following choices:

  1. 6

  2. 10

  3. 14

  4. None; the program runs in an infinite loop

Question 7 in Fall 2019 Midterm Exam [Intermediate]

Write a C++ function void readInts() that repeatedly reads integers from the standard input (using cin) and then immediately outputs the input integer (using cout), one integer per line.

When the end-of-file is reached, the function prints the message "End of File Reached" on a line by itself before returning. If a non-integer is input the function should print the message "Invalid Input" on a line by itself, should discard the rest of the stream and should continue reading integers again until the end-of-file is reached.

void readInts() {