COMP 2401/2001 (W13)

Assignment 1 - Due Friday 1st at 6pm

Problem #1 edited January 27.

Problem #0 edited January 28.

Submit a single file UserID.assignment1.tar to cuLearn. Replace UserID with your 9-digit Carleton student ID number. Use the tar program (see pg 85 of the Linux Phrasebook) to create the tar file (similar to a zip file). It should contain the modified Makefile from problem 0 and all program?.c files for the coding questions.

Your grade for this assignment will be based on correctness and style.


  1. Edited Monday, January 28

    Download the following Makefile. Add one rule for the submit target at the bottom of the Makefile as specified in the file. If you include the math library you can add a -lm at the end of your rule for any of the targets. Otherwise, do not change anything else in the Makefile. We will use this Makefile when compiling your code (and you should too).

    We will compile with a version of GCC that does not need the -lm so don't worry if your submission needed this.


  2. Edited Sunday, January 27

    Write a C program, called problem1.c, that reads (and processes) characters from standard input until a 'Q' is reached. Each letter and digit read should be printed to standard output (including the Q). The output will consist of a single line of characters (letters and digits), with no extra white-space added, ending with Q and then a newline.

    For example, if the input, entered from the keyboard, was

    a[return]
    b[return]
    Q[return]
    
    then the output would be the single line
    abQ
    
    with a newline at the end of the output. The same output would be given for the input
    abQ[return]
    
    and
    a[return]
    bQ[return]
    

    Note: Just use scanf() and printf(). You can use other functions if you wish but these are sufficient.

    Note: You can assume that the input will only consist of letters, digits, and whitespace (space, tab, linefeed, etc).


  3. Write a C program, called problem2.c, that computes the average and standard deviation of a collection of numbers entered from standard input. The program reads zero or more non-negative decimal numbers (doubles) followed by the decimal number -1.0. Once -1.0 is entered, the program prints to standard output the average and standard deviation of all the non-negative numbers entered and the main function returns 0.

    The output should be a single line that looks exactly like

    average,standard_deviation
    
    where average is the average of the numbers and standard_deviation is the standard deviation. Both of these are doubles. There should be no spaces around the comma and the line should end with a newline character.

    Note: Use the sample standard deviation (which uses N-1 instead of N in the denominator inside the square root of the formula).

    Note: Do not use an array for this question.


  4. Write a C program, called problem3.c, that reads a single integer n from standard input and then reads n more integers. After reading all the integers the program displays all the integers (one integer per line) in the reverse order that they were read.

    For example, if the input, entered from the keyboard, was

    3[return]
    12[return]
    -9[return]
    1[return]
    
    then the output would be three lines
    1
    -9
    12
    

  5. Write a C program, called problem4.c, that validates Canadian Social Insurance Numbers. The program repeatedly reads an unsigned integer from standard input until the number 0 is entered. For each number entered, the program will display "valid" or "invalid" on a single line, depending if the number entered is valid or invalid, and then read another number from standard input (or exit the program if 0 is entered).

    The validation procedure can be found at http://en.wikipedia.org/wiki/Social_Insurance_Number#Validation.


  6. Write a C program, called problem5.c, that reads characters from standard input until three consecutive 'Q' characters are read. The program processes the read characters and gives output to standard output as follows:

    For example, the input

    abcABCAA\n3\N6-QQQ[return]
    
    will give the output
    ABCabca2
    6\n18-
    
    where the newline character is unseen but causes an actual newline.


Last modified : January 23rd, 6:45pm