5.5. Exercises#

Many of these exercises are taken from past exams of APS105 Computer 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 Winter 2020 Midterm Exam [Easy]

Write the output of the following program.

#include <stdio.h>

void skipSpace(int n) {
  for (int i = 1; i <= n; i++) printf(" ");
}

void printLeft(int n) {
  for (int i = 1; i <= n; i++) printf("/");
}

void printRight(int n) {
  for (int i = 1; i <= n; i++) printf("\\");
}

int main(void) {
  const int TSize = 5;
  int i;
  skipSpace(TSize);
  printf("*\n");
  for (i = 0; i < TSize - 2; i++) {
    skipSpace(TSize - 1 - i);
    printLeft(i + 1);
    printf("|");
    printRight(i + 1);
    printf("\n");
  }
  skipSpace(TSize);
  printf("|");
  return 0;
}

Question 4 in Winter 2022 Midterm Exam[Easy]

The following function is called with three distinct characters and returns the middle character, e.g., if ‘a’, ‘b’, and ‘c’ is passed as arguments to the function, it returns ‘b’. Rewrite this function such that it only uses one return and one if statement.

char median(char a, char b, char c) {
  if (a < b) {
    if (b < c) {
      return b;
    } else if (a < c) {
      return c;
    } else {
      return a;
    }
  }
  if (c < b) {
    return b;
  }
  if (a < c) {
    return a;
  }
  return c;
}

Question 11 in Fall 2015 Midterm Exam [Intermediate]

Write a C function reverseDigits, the prototype of which is given below, that takes an integer argument value, and returns an integer with its digits reversed. For example, if the argument is 1234, the function will return 4321.

int reverseDigits(int value);

Question 12 in Fall 2015 Midterm Exam [Challenging]

In 1706, John Machin, then a professor in London UK, devised a formula for calculating \(\pi\):

\(\frac{\pi}{4} = 4 \cdot \arctan(\frac{1}{5}) − \arctan(\frac{1}{239})\)

where the arctan values can be calculated using the following formula:

\(\arctan(x) = x − \frac{x^3}{3} + \frac{x^5}{5} - \frac{x^7}{7} + \frac{x^9}{9} \dots - \frac{x^{299}}{299} + \frac{x^{301}}{301}\)

Write a C program that calculates and prints \(\pi\) using this method, printing \(10\) digits after the decimal point. You must define your own function called arctan to compute the arctan values, and you are not allowed to use the atan function from the math library. When your program is run, its output should be (up to 10 decimal places):

Pi = 3.1415926536

Question 9 in Winter 2020 Midterm Exam [Challenging]

A pythagorean triple is a triple of integers \((x, y, z)\) such that \(x^2 + y^2 = z^2\).

Complete this function that takes a single positive integer x as an argument and prints three positive integer values x, y and z such that:

  1. \(x^2 + y^2 = z^2\)

  2. \(y > 0\) and \(y < 100\)

  3. \(y < z\).

If there is no triple that satisfies these conditions print “no solution exists.” Start with the following definition:

void pythagoreanTriples (int x) {