COMP 2401/2001 (W13)

Assignment 2 - Due Friday, February 8th at 6pm (Early submission Wednesday, February 6th at 6pm)

Submit a single file UserID.assignment2.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 a Makefile (use the same makefile from Assignment #1) and all of your program?.c files for the coding questions.

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


  1. Read Chapter 3 of the Hoover book. (You can do this assignment with what we have talked about in class. This will be helpful soon.)

  2. A string in C is array of characters ending with the '\0' character. The scanf() function can read strings using the format modifier %s. When reading a string in this way, scanf() will skip over whitespace (space, tab, newline) until it reaches a non-whitespace character and then reads all the non-whitespace characters it sees until another whitespace is reached. It also automatically inserts the '\0' at the end of the string.

    Write a C program, called problem1.c, that reads in "words" from standard input and the prints the words to standard output with correct punctuation and spacing. Keep reading and processing words until you reach the word "QQQ". We will consider a word to be a maximal sequence of non-whitespace characters.

    The input can have arbitrary whitespace between each word and each letter might have any case. You can assume that the input will only consist of upper and lower case letters, the punctuation marks '.', '!', '?', ',', '\'', and the character '-'. The input will be one or more sentences with bad spacing and capitalization.

    In the output, the first word of each sentence will be capitalized and there will be one space between each word and sentence. The output will have no leading whitespace and no trailing whitespace (the terminating newline being the exception).

    For example, the input

      gOOey  goo  for   chewy  chewing   !
    that'S     wHAT      THAT Goo-GoOse is          doing.  QQQ
    
    should be output as the single line
    Gooey goo for chewy chewing! That's what that goo-goose is doing.
    
    A single newline will be at the end of this line.

    Notes


  3. Write a C program, called problem2.c, that does exactly the same thing as the previous problem but using the fgets() function to read the strings.

    To use the function, you must specify how many characters to read and where to read them from (in addition to where you want to save the input). For example,

    char s[100];
    fgets(s, 100, stdin);
    
    will read 99 chars from standard input and put it in the string s. The last element in the string (s[99]) will be set to \0. It will include all whitespace, including newlines, that it sees in the 99 chars.

    Note: You can assume that no line will have more than 5000 characters.


  4. Write a C program, called problem3.c, that repeatedly does the following:

    Each integer printed is followed by a newline.

    Note: Do not use include string.h. You need to write your own functions to deal with the strings.

    Note: For your program, use the following two strings

    char s1[256];
    char s2[256];
    

    Note: Your main function should not be very large. It should basically have a loop that reads in two words and then calls three functions which return integers that you will print.


  5. Write a C program, called problem4.c, that reads in two integers (that are the same value), n and m, from standard input. It then reads 2*n*m more integers. The first n*m are the entries of an nXm matrix. The next n*m are the entries for an mXn matrix. Your program will output the addition of these two matrices. That is, your program will print to standard output n*m integers that are the entries of the new matrix.

    Note: Remeber that m == n here. All three matrices (two input and out output) will all be square matrices.

    Note: Use 2-dimensional arrays to store your matrices.


  6. Write a C program, called problem5.c, that is the same as the previous problem except that n and m might not be the same, and instead of outputting the addition of two matrices it will output the multiplication of two matrices.

    Note: n != m in this case



Last modified : February 1, 2013