Lab 5


There are 3 exercises. When doing them, consider the following:

The files

Start by downloading the 4 files listed here.

To begin your testing, you can use the same input on all 3 exercises. And here is the expected output from the 3 programs Use diff to see if your programs produce the expected output.

The Questions

  1. Write a C program called q1.c. It should read from standard input and write to standard output. For input it expects a sequence of integers separated by whitespace. After it has read all the integers, it should output them in reverse order, i.e. opposite to the order in which they were input. In the output, each integer except the last one should be followed by a single blank/space. The last integer output should be followed by a newline character instead. There should be no other output. If there is no number input, there should be no output. You may assume that the input is well formed and that no input integer causes int overflow/underflow. And you may assume that there won't be more than 20 integers in the input.

    The following input:
       6   8
            9  2
    1
    3 8 8 2 3 
    1 9 4
    
    should produce this output:
    4 9 1 3 2 8 8 3 1 2 9 8 6
    
    Submit q1.c with the following command
        submit 2031 lab5 q1.c
    

  2. Write a C program called q2.c. It should read from standard input and write to standard output. For input it expects a sequence of integers separated by whitespace. After it has read all the integers, it should sort them using qsort() and then output them in nondecreasing order. In the output, each integer except the last one should be followed by a single blank/space. The last integer output should be followed by a newline character instead. There should be no other output. If there is no number input, there should be no output. You may assume that the input is well formed and that no input integer causes int overflow/underflow. And you may expect that there won't be more than 20 integers in the input.

    When sorting the integers, use normal integer comparison, except that any even number is considered larger than any odd number. For example, the input given in exercise 1, should produce this output:
    1 1 3 3 9 9 2 2 4 6 8 8 8
    
    Submit q2.c with the following command
        submit 2031 lab5 q2.c
    

  3. Write a C program called q3.c. It should be like q1.c, except that it should not assume any particular upper bound on the number of integers that will be input.

    NOTE: You may need to wait till you have learned about malloc() and realloc() to do this exercise.

    A restriction:When you allocate space to store the integers read, at no time should it have room for more than 4 additional integers. In other words, if you haven't yet read any integers, the maximum space that you can have allocated can't be more than enough to hold 4 ints. And if you've read 12 integers, then the allocated space at that time cannot be more than enough to hold 16 integers; and so on.

    The input given in exercise 1 should produce the output shown in exercise 1

    Note: Although it is true that q3.c could be used to replace q1.c, don't do this. Write 2 separate programs. Make q1.c appropriate to the description in exercise 1 and q3.c appropriate to the description in exercise 3.

    Submit q3.c with the following command
        submit 2031 lab5 q3.c
    

end of lab 5