Lecture 5

Transcription

Lecture 5
C 프로그래밍
C언어 (CSE2035)
(Chap9. Pointer Applications)
컴퓨터공학과
C 프로그래밍
Practice 1.
1. Write a program that uses below function.
The function copies a one-dimensional array of n elements
into a two-dimensional array of j rows and k columns. The
resulting array will be placed in the heap. The data will be
inserted into the array in column order; that is, the first j
items will be placed in column 0, the second j items in column
1, and so forth until all columns have been filled.
If j * k is greater than n, the other elements are filled as 0,
and if j * k is less than n, the function returns a null pointer.
컴퓨터공학과
2
C 프로그래밍
Practice 1.
The program receives n, j, k, and values of an array, and
prints copied array( j rows, k columns).
int** copyArray(int*, int, int, int);
Constraint
Use dynamic memory allocation for the space of Pascal’s
triangle, but do not allocate more than necessary.
Use local variables only in the program.
컴퓨터공학과
3
C 프로그래밍
Practice 1.
Example ( Italic characters are not printed. )
Input: 6 2 3
123456
Output: 1 3 5
246
Input: 7 2 3
1234567
Output: Original data is lost!!
컴퓨터공학과
4
C 프로그래밍
Practice 2.
2. Write a program that creates the structure shown as below.
1
1
1
1
2
1
1
3
3
1
1
4
6
4
...
컴퓨터공학과
5
1
C 프로그래밍
Practice 2.
The program receives a size of Pascal’s triangle, and prints
Pascal’s triangle as given size. Write below functions for this
program.
void genTriangle(int**, int);
// Generate Pascal’s triangle
void printTriangle(int**, int);
// Print generated Pascal’s triangle.
컴퓨터공학과
6
C 프로그래밍
Practice 2.
Constraint
Use dynamic memory allocation for the space of Pascal’s
triangle, but do not allocate more than necessary.
Use local variables only in the program.
Example ( Italic characters are not printed. )
Input : 3
Output : __1
_1_1
1_2_1
컴퓨터공학과
(Underline characters represent space
characters.)
7
C 프로그래밍
Practice 3.
3. Write a program that creates a two-dimensional array in
the heap and then analyzes it to determine the minimum,
maximum, and average of each column.
The data are to be read from a file. The first two elements
are the number of rows in the array and the number of
columns in each row. The example for a 4 * 3 array are shown
in next page.
컴퓨터공학과
8
C 프로그래밍
Practice 3.
File data (each column is separated by white space character.)
43
237 128 322
38
271
67
918
39
210
573 199 753
Execution
> ./exec input.dat
컴퓨터공학과
9
C 프로그래밍
Practice 3.
Output: (Numbers are left aligned for each column.
If you need to print floating point number,
round off it to two decimal places.)
38
39
67
918
271
753
441.50 159.25 338
컴퓨터공학과
10