11286 - Matrix Format Practice   

Description

A matrix is a two-dimensional object, but the memory is one-dimensional.  Also, the ways to allocate memory are very different from each other, e.g. malloc v.s. static declaration.  Thus, how to store a matrix in the memory and how to allocate required memory are important issues.  

In this exercise, you will learn how to store and manipulate given matrices in different types of variables.  The memory format of interests are basically the following,

  1. Static two-dimensional array
    char STDA[MAXROW][MAXCOL];
  2. Dynamic two-dimensional array - type 1
    char (*DTDA1)[MAXCOL];
    This is a pointer to a character array which has length MAXCOL
  3. Dynamic two-dimensional array - type 2
    char *DTDA2[MAXROW];
    This is an array of pointers to characters
  4. Dynamic two-dimensional array - type 3
    char **DTDA3;
    This is a pointer to a pointer to a character

The Basic Flow of This Program

NOTE: This is a partial judge problem, so you should finish the unimplemented functions.  Please read the given source and header carefully.

  1. get the dimension of the matrix (provided)

    1. if the given dimension is 0-by-0, see the final section "Special Input." Answer the questions and return.  These questions help you construct the concept of pointers and their usage.  It is recommended that you finish the general part first and then do this part. (you have to implement it)

  2. allocate memory for the three different types of dynamic formats (one provided; you have to implement the rest two)

  3. read the input matrix into type-3 variable, DTDA3, for the matrix (you have to implement it)

  4. do a flip upside-down(上下顛倒) operation on DTDA3 and store the result to DTDA2 (you have to implement it)

    1. print DTDA2 (you have to implement it)

    2. print a split line (provided)

  5. do a flip left-right(左右翻轉) operation on DTDA2 and store the result to DTDA1 (you have to implement it)

    1. print DTDA1 (you have to implement it)

    2. print a split line (provided)

  6. do a rotation operation of 180 degrees on DTDA1 and store the result to STDA (you have to implement it)

  7. free the three dynamic variables (all provided)

  8. print STDA (provided)

 

Input

The input contains all the information you need, a.k.a dimension and data, to store a complete matrix.

The first line has two positive integers that are less than 10 and separated by a space.  They represent nrow, the number of rows and ncol, the number of columns.

The following nrow lines contain the data, ncol characters per line, with one newline('\n') character at the end of each line.  The characters are alphabets(A-Z, a-z), numbers(0-9), and punctuations(,./?[}...).

Testcase Generator (You don't need this in your code)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<ctype.h>

int main(int argc, char *argv[]){
        if(argc != 3)
                return -1; 

        srand(time(NULL));
        int nrow = atoi(argv[1]);
        int ncol = atoi(argv[2]);
        printf("%d %d\n", nrow, ncol);

        for(int i = 0; i < nrow; i++){
                for(int j = 0; j < ncol; j++){
                        char ch; 
                        ch = rand()%128;
                        do{ 
                                ch = rand()%128;
                        }while(!isalnum(ch) && !ispunct(ch));
                        putchar(ch);
                }   
                putchar('\n');
        }
}

In order to use this program, you should first build it into an executable gen.exe, and then run it in the command line prompt(終端機命令列) with the nrow and ncol you want to test.  For example:

C:\Document and Settings\Users\ABC> gen.exe 3 5

or you can directly generate an input testcase file using redirection:

C:\Document and Settings\Users\ABC> gen.exe 3 5 > input.txt

Output

The program should output totally three matrices separated by split lines (see example).  All the three matrices are in the same format as the input matrix: nrow lines and ncol characters plus one newline at the end per line.  The split line is ncol '=' characters and a newline at the end of it.  The final output should be the same character matrix as the input one.

Special Input

There will be one special test case, which gives you only the first line: 0 and 0.  In this case, you have to answer the following multiple-choice and yes-or-no questions.  Assuming nrow = 3 and ncol = 5 in all the questions.

For multiple-choice questions, you should output ONLY the number of the option and then a newline character, there will be only one answer among all the choices.

For yes-or-no questions, you should output "yes" or "no" following by a newline.

  1. How many bytes does malloc() allocate in the function alloc_DTDA1()?
    1. 15
    2. 48
    3. 80
    4. 256
  2. How many times should the malloc() be called in the function alloc_DTDA2()?
    1. 3
    2. 5
    3. 15
    4. 16
  3. How many times should the malloc() be called in the function alloc_DTDA3()?
    1. 3
    2. 4
    3. 5
    4. 6
  4. The number that malloc() is called in alloc_DTDA3 shoule be the same as the number that free() is called in free_DTDA3.
  5. The function flipUD_DTDA3_to_DTDA2 can be applied not only from DTDA3 to DTDA2, but also from DTDA2 to DTDA3. (Hint for 5~7, just try to compile & run it!)
  6. The function flipLR_DTDA2_to_DTDA1 can be applied not only from DTDA2 to DTDA1, but also from DTDA1 to DTDA2.
  7. The function rotate180_DTDA1_to_STDA can be applied not only from DTDA1 to STDA, but also from STDA to DTDA1.
  8. If one change the prototype from "void show_DTDA2(char *DTDA2[MAXROW], int nrow, int ncol)" to "void show_DTDA2(char *DTDA2[], int nrow, int ncol)" in function.h, the original implementation of show_DTDA2 does NOT work.(Hint for 8~10, just try it! And notice that in English you should say "yes, it works" or "no, it doesn't work")
  9. If one change the prototype from "void show_DTDA2(char *DTDA2[MAXROW], int nrow, int ncol)" to "void show_DTDA2(char **DTDA2, int nrow, int ncol)" in function.h, the original implementation of show_DTDA2 does NOT work.
  10. If one change the prototype from "void show_DTDA1(char (**pDTDA1)[MAXCOL], int nrow, int ncol)" to "void show_DTDA1(char (**pDTDA1)[10], int nrow, int ncol)" in function.h, the original implementation of show_DTDA1 does NOT work.
  11. There are 15 elements in the given 3-by-5 matrix.  Which variable holds all the elements in a continues area?
    1. STDA
    2. DTDA1
    3. DTDA2
    4. DTDA3
    5. all of them
    6. none of them
  12. Assume that the given matrix is of dimension MAXROW-by-MAXCOL.  Which variable holds all the elements in a continues area?
    1. STDA and DTDA1
    2. DTDA2 and DTDA3
    3. both of them
    4. none of them

Notice that you should provide the output of exactly 12 lines to the special case. 

Sample Input  Download

Sample Output  Download

Partial Judge Code

11286.c

Partial Judge Header

11286.h

Tags




Discuss