Lab06, CISC105 Summer 2005

Directions

Programs

  1. (20) Write a program with a function that takes a 2-d square matrix as an argument and transposes the matrix. Transposing a matrix means that for every element in matrix m and transposed result n, m(i,j) = n(j,i).

    Initialize a 2-d array to the values below when you declare it, print the matrix, then call your transpose function on it and print the result. (Write other functions as appropriate.)

    Initially:

    1 2 3
    4 5 6
    7 8 9
    

    The transposed result should be

    1 4 7
    2 5 8
    3 6 9
    
  2. (10) Write a program that creates an integer variable x and an integer pointer variable xPtr. Put the value 4 in x. Assign xPtr the address of x. Now print:
  3. (20) Write a function that takes three doubles as reference parameters and puts them in order largest to smallest. Print the variables in main before and after you call the function. Reorder the variables in as few comparisons and swaps as possible. (Recall the sorting algorithms discussed in class.)
  4. (15) Write a program that reads a text message from a user character-by-character into a char array (using scanf with %c or getChar). The message should be no longer than 30 characters (but how long should your array be?). Now print ``Your message is: '' and the input, using a single printf.
  5. (20) Declare a 5x5 2-d array of char. Use nested for loops to fill it with asterisks and spaces, as below (the spaces look like underscores, but yours should be spaces). Then use nested for loops to print out the whole array one character at a time in its square shape.
    *____
    _*___
    __*__
    ___*_
    ____*
    
  6. (20) Create a 10x15 2-d array of characters. Fill it with words of all different lengths from standard input, using scanf with %s inside a loop. Print the contents of the array, one per line.

    Now, add a 10x22 array (before any non-declaration statements). After filling the 10x15 array, copy the contents of the 10x15 array into the 10x22 array using strcpy. Then, use strcat() to add the suffix "Hello" to every word in the array. Print each new word on a line, followed by the length of the word as shown by strlen().

  7. (35) Make three versions of program 4 that reads a text message from a user into a character array: one that uses getchar() to scan one character at a time, one that uses gets(), and one that uses fgets(). Write all your output using putchar(). (You may want to write a function to print your strings.) Put each version into a function and call from main().
  8. (20) Write a function that takes an integer n and a string as parameters and prints the first n letters of the string. If n is larger than the string, your program should stop at the end of the string. Put the function call in a loop in main so the user can input five pairs of data.
  9. (20) Modify the previous program so that it calls your function only once. Pass it an integer and a string from the command line. Rename your executable a.out as "truncate" and demonstrate it, e.g.,
    > truncate 3 bark
    bar
    
  10. (20) Modify the previous program again so that it calls your function k times. Have your main read in two integers and a string from the command line. The first integer will be k and the second will be n. The string will be the name of a data file. Read in k words from the data file and print the truncated words. Only work on a single word at a time, i.e., do not store all the words at once.

    Example Run:

    > truncate2 5 4 words.txt
    pape
    dog
    catc
    girl
    boyf
    

You should have a total of 10 programs named lab06.1.c to lab06.10.c. Make a single script file (see lab00 for the instructions) where you cat, compile, and run each one in its final form (if it didn't compile, don't run it in the script - mark the place in the printed script file with a marker so it stands out).

Note: Cat, compile, and run each program in order! Do not cat all programs, then compile, etc.

Execute your program multiple times to show that you tested the program well.

Submission

Email the tar file to Gang by midnight on Wednesday (see lab00 for directions). Give the paper version to Gang at the beginning of your next lab.

Grading

Grading is as noted above for each program for a total of 200 points.


Adapted from Terry Harvey and Phill Conrad.