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,
NOTE: This is a partial judge problem, so you should finish the unimplemented functions. Please read the given source and header carefully.
get the dimension of the matrix (provided)
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)
allocate memory for the three different types of dynamic formats (one provided; you have to implement the rest two)
read the input matrix into type-3 variable, DTDA3, for the matrix (you have to implement it)
do a flip upside-down(上下顛倒) operation on DTDA3 and store the result to DTDA2 (you have to implement it)
print DTDA2 (you have to implement it)
print a split line (provided)
do a flip left-right(左右翻轉) operation on DTDA2 and store the result to DTDA1 (you have to implement it)
print DTDA1 (you have to implement it)
print a split line (provided)
do a rotation operation of 180 degrees on DTDA1 and store the result to STDA (you have to implement it)
free the three dynamic variables (all provided)
print STDA (provided)
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(,./?[}...).
#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
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.
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.
Notice that you should provide the output of exactly 12 lines to the special case.