COMP2004 Programming Practice
2002 Summer School

Week 1 Friday Tutorial Exercises


  1. The math.h library provides functions to round floating point numbers up (ceil()) and down (floor()), but not to the nearest integer, ie. up or down, depending on whether the fractional part of the floating point number (ie. the part after the decimal place) is greater than or equal to 0.5 or not. Write a function to do this rounding using the functions provided by the math.h library. Your function should have a prototype of:
    long round(double x)
    

  2. Repeat the previous question, but this time without using any library functions at all.

  3. Write a function which uses the math.h library functions to determine how many digits a given unsigned integer has. Your function should have the following prototype:
    unsigned int numDigits(unsigned long x)
    

  4. Repeat the previous question, but this time without using any library functions at all. (Hint: notice that for the number 7254 (for instance), using integer division: 7254 / 10000 = 0, but 7254 / 1000 > 0.)

  5. Write a program that reads from cin, and outputs the number of words read, as well as the length of the longest word, and the average word length.

  6. Write a function called join, which takes as an argument a string and an vector of strings. It should return a string which is the array of strings joined by the first string. For example:
    std::vector<std::string> vs;
    vs.push_back("hello"); vs.push_back("how"); vs.push_back("are");
    std::string j = "fred";
    std::cout << join(j,vs) << endl;
    
    Should output: hellofredhowfredare

  7. Write a split function, which does the opposite of join (ie. is passed two strings, and returns a vector of strings)

  8. Write a program that reads from cin, and outputs each line reversed. You should use a function called reverse with the following prototype
    std::string reverse(std::string s)
    
    to do the actual string reversing.

  9. Write a program that mimics the Unix tac utility. This utility reads in a file and then outputs the lines in reverse order.

  10. Extend this program so that the entire file is output in reverse (ignoring newlines). This is easily accomplished by reversing each line as it's output.

  11. Write a program which outputs the transpose of its input. This is where columns are exchanged for rows and vice-versa. For example, if the input is
    ABC
    DEF
    GHIJ
    
    then the output is
    ADG
    BEH
    CFI
      J
    

  12. Use your numDigits() function from exercise 3 or 4 to write a function which converts an unsigned long into a std::vector<unsigned int> of digits. For example, convert the integer 23711 into a vector containing the elements 2, 3, 7, 1, 1. Your function should have the following prototype:
    void getDigits(std::vector<unsigned int> &digits, unsigned long number)
    
    Hint: you might find a function like this useful for Assignment 1.

  13. Repeat the previous question, but this time without using any of your other functions or any library functions at all.