COMP2004 Programming Practice
2002 Summer School

Week 1 Wednesday Tutorial Exercises


  1. Write, compile, and test a C++ program which just prints a short message, such as "Hello world".

  2. Write a program which reads a month number from std::cin and outputs the corresponding month name to std::cout.

  3. Write a program which reads a month name from std::cin and outputs the corresponding month number to std::cout. The std::string class might be useful.
    #include <string>
    #include <iostream>
    int main() {
    	std::string word;
    	std::cin >> word;
    	std::cout << word << endl;
    }
    

  4. Write a program which reads in a date in the form <day> <month name> <year> (such as 10 January 2000) and outputs the corresponding day of the week. Feel free to use the following method. You can check your program by using the Unix cal utility. Run man cal for short documentation.

  5. Write a C++ program which reads in four floating point numbers representing the x and y coordinates of two points in 2-dimensional space and then calculates and outputs the distance between the two points. In the program, create a function distance with the prototype
    double distance(double x1, double x2, double y1, double y2)
    
    to perform the actual calculation. For those that have forgotten, the distance formula is:

    You might want to look up man -a sqrt.

  6. Write a function to calculate factorial. Factorial is defined as:
    factorial(n) = n*factorial(n-1)
    factorial(1) = 1
    factorial(0) = 1
    

  7. Write a function to calculate Ackermann's function. Ackermann's function is defined as:
    A(0, n) = n + 1
    A(m+1, 0) = A(m, 1)
    A(m+1, n+1) = A(m, A(m+1, n))