• The Text Book Programming in C
  • Basic Unix Command summary of chapter 2 of unix-introduction
  • exercise 1: Compiling C programs
    • Write a C program. Refer to pay0.c in textbook.
    • Your program will print a diamond. For example:
             $ gcc ex1-diamond.c 
             $ ./a.out 
                     1
                    1 1
                   1   1
                  1     1
                 1   2   1
                  1     1
                   1   1
                    1 1
                     1
             $ 
             
    • Use printf() to do your exercise.
    • You must use "%7d" to do your exercise where the '7' can be substituted with other numbers.
    • Due: 2018/Mar/22, 00:05
      You have to put your program under ~/c-teach-1062/exer1.
  • exercise 2: exercising 'if' and 'while'
    • Write a C program which is similar to exercise 1 but the size of diamond and the center number are read from input.
      For example:
             $ gcc ex2-diamond.c 
             $ ./a.out 
             size  : 4
             center: 9
                1
               1 1
              1   1
             1  9  1
              1   1
               1 1
                1
             size  : 5
             center: 11
                 1
                1 1
               1   1
              1     1
             1   X   1
              1     1
               1   1
                1 1
                 1
             size  : 3
             center: 1200
               1
              1 1
             1 C 1
              1 1
               1
             size  : 0
             $ 
             
    • Due: 2018/Mar/29, 00:05
      You have to put your program under ~/c-teach-1062/exer2.
  • exercise 3: practicing function
    • Write a program to print a 6x6 multiplication table.
    • All numbers should be shown in radix 7.
    • Function should be used in your program.
    • Use #define to eliminate magic numbers. For example, '7' should not appear in your program.
             $ gcc ex3-6x6.c 
             $ ./a.out 
                 |   1   2   3   4   5   6
             ----+--------------------------
                1|   1   2   3   4   5   6
                2|   2   4   6  11  13  15
                3|   3   6  12  15  21  24
                4|   4  11  15  22  26  33
                5|   5  13  21  26  34  42
                6|   6  15  24  33  42  51
             $ 
             
    • Due: 2018/Apr/06, 00:05
      You have to put your program under ~/c-teach-1062/exer3.
  • exercise 4: character operation
    • Write a program to read floating numbers and print the sum of them.
    • Read floating numbers from stdin until EOF is encountered. You should use getchar() to read the characters and convert them to a floating number. This process is similar to getint(). Finally, sum all the floating numbers and print the sum. For example,
             $ gcc ex4-get-float.c 
             $ ./a.out 
                123
              456.23 78.1
             Sum = 657.329956     <---------  ^D pressed
             $ 
             
    • Due: 2018/Apr/19, 00:05
      You have to put your program under ~/c-teach-1062/exer4.
  • exercise 5: practicing function
    • Write a program which draws a diamond. The program reads two integers from stdin. The first is the size and the second is the border. When the second integer is less than 10, the program shows its corresponding digit, when it is greater than or equals to 10 but less than 100, the program shows 'X', and when it is greater than or equals to 100, the programs shows 'C'. The following is an example run.
              $ ./a.out 
              size  : 4
              border: 9
                 9
                9 9
               9   9
              9     9
               9   9
                9 9
                 9
              size  : 3
              border: 120
                C
               C C
              C   C
               C C
                C
              size  : 3
              border: 43
                X
               X X
              X   X
               X X
                X
              size  : 0
              $  
            
    • To write this program you have to write two functions which prototypes are as follows.
            void nchars(int n, char ch);  /* write n 'ch' to stdout */
            char convert(int n);          /* convert n to a border character */
            
      And use them to complete your program. Of course, in addition to the previous two functions, you can define more functions to make your program more readable.
    • You can only use putchar() and scanf(). printf() is not allowed.
    • Due: 2018/May/10, 00:05
      You have to put your program under ~/c-teach-1062/exer5.
  • exercise 6: practicing pointer arguments with functions
    • First define two functions which convert coordinates between polar and cartesian. (Or between radian and retangular)
              void rec2rad(double x, double y,     double *r, double *theta);
              void rad2rec(double r, double theta, double *x, double *y);
            
    • Then write a main() to test those functions. For example:
              $ gcc ex6-rad-rec.c -lm
              $ ./a.out 
              r 3 4 
              (3.000000,4.000000) --> 5.000000, 0.927295
              r 3 -4
              (3.000000,-4.000000) --> 5.000000, -0.927295
              p 5.000000 -0.927295
              5.000000,-0.927295 --> (3.000001, -3.999999)
              p 5.000000 0.927295
              5.000000,0.927295 --> (3.000001, 3.999999)
              q
              $ 
            
    • The main loop will read a char which is a 'r', 'p', or 'q'.
      • If it is a r, then it should follows two doubles which represent x,y.
      • If it is a p, then it should follows two doubles which represent r,theta.
      • If it is a q, the just quit.
    • For using sin(), cos(), atan(), and sqrt(), include <math.h>.
      Try 'man sin' for more details.
    • The program should be linked with the mathematic library.
      See the previous example.
    • Due: 2018/May/17, 00:05
      You have to put your program under ~/c-teach-1062/exer6.
  • exercise 7: finding the maximum two integers
    • Find the maximum integers read from stdin.
    • First define two functions as follows. The read_array() will read integers into an array (pointed by the first argument). The number of integers does not exceed limit (the second argument). The function also returns an integer which is the number of integers read.
      Another function find_max2() will accept an integer array and its size and figure out the maximum two integers which are returned with two pointer arguments.
              int  read_array(int *int_array, int limit);
              void find_max2(int *int_array, int array_size, int &max, int &next);
            
    • Then write a main() to test those functions. For example:
              $ gcc ex7-max2.c 
              $ ./a.out 
              78 89 66 5433 88
              The max is 5433, the next is 89
              $ ./a.out 
              12
              23
              23 90 -98
              The max is 90, the next is 23
              $ 
            
    • Due: 2018/May/23, 00:05
      You have to put your program under ~/c-teach-1062/exer7.
  • exercise 8: practicing fopen()
    • First, read two filenames from stdin, one for input, one for output.
    • Second, use fopen() to open these two files.
    • Then repeatedly do the following read and write operations on files until EOF is encountered.
    • Read a line from input. It consists of a name and a score.
    • Write the name and score to output. But if the score is less than 60, "Fail" is printed, and if the score is greater than 99, just print 99.
    • Finally, close the two opened files.
    • For example:
              $ cat ex8-in.dat
              klim 104
              milk 23
              redcow 88
              oak 59
              $ gcc ex8-fopen.c 
              $ ./a.out 
              input  file name: ex8-in.dat
              output file name: ex8-out.dat
              $ cat ex8-out.dat
              klim       99
              milk       Fail
              redcow     88
              oak        Fail
              $ 
            
    • Due: 2018/May/30, 00:05
      You have to put your program under ~/c-teach-1062/exer8.
  • exercise 9: simulating 'grep' command
    • Read the man page of 'grep' first. (Try ''man grep''.)
    • The program will be invoked with at least one argument, which is the pattern string. Other arguments if existed are filenames.
    • If no files are provided, read from stdin. Otherwise, read from each file sequentially.
    • Repeatedly read a line until EOF is encountered. For each read line, if it contains the pattern, show it, otherwise, ignore it.
    • For example:
              $ gcc ex9-grep.c 
              $ ./a.out klim       <-------- no files, read from stdin
              Oh, it is good.      <-------- first line,  ignored
              klim is fine.        <-------- second line, contains 'klim'
              klim is fine.        <--------   so show it
              Good klim            <-------- third line, contains "klim'
              Good klim            <--------   so show it
              milk
              aaaaaklimmmmmmm      <-------- contains klim
              aaaaaklimmmmmmm      <--------   so show it   
              $ ./a.out klim ex8-in.dat ex9-grep.c     <------ read from files
              klim 104
              $ ./a.out klim ex8-in.dat ex8-out.dat    <------ read from files
              klim 104
              klim       99
              $ 
            
    • For reading a line, use fgets(). gets() is not allowed. A program uses gets() will get -100.
    • Type 'man string' to get a list of string functions. One of them can be used to do this exercise.
    • Due: 2018/Jue/07 00:05
      You have to put your program under ~/c-teach-1062/exer9