-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise09.c
65 lines (52 loc) · 1.68 KB
/
exercise09.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// C Primer Plus
// Chapter 10 Exercise 9:
// Write a program that initializes a two-dimensional 3×5 array-of-double and
// uses a VLA- based function to copy it to a second two-dimensional array.
// Also provide a VLA-based function to display the contents of the two arrays.
// The two functions should be capable, in general, of processing arbitrary N×M
// arrays. (If you don’t have access to a VLA-capable compiler, use the
// traditional C approach of functions that can process an N×5 array).
#include <stdio.h>
#define ROWS 3
#define COLUMNS 5
void copy_2dim_arr(int rows, int cols, double source[rows][cols],
double target[rows][cols]);
void print_2dim_arr(int rows, int cols, double arr[rows][cols]);
int main(void)
{
double array1[ROWS][COLUMNS] = {{4.3, 5.7, 2.1, 6.6, .8},
{5.6, 23.5, 73.2, 12.3, 123},
{22.1, 35.3, 6.35, 0.132, 11.1}};
double array2[ROWS][COLUMNS];
// copy array1 to array2
copy_2dim_arr(ROWS, COLUMNS, array1, array2);
// print contents of arrays
printf("Array 1:\n");
print_2dim_arr(ROWS, COLUMNS, array1);
putchar('\n');
printf("Array2:\n");
print_2dim_arr(ROWS, COLUMNS, array2);
return 0;
}
void copy_2dim_arr(int rows, int cols, double source[rows][cols],
double target[rows][cols])
{
// copy one two-dimensional array to another
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
target[i][j] = source[i][j];
}
}
}
void print_2dim_arr(int rows, int cols, double arr[rows][cols])
{
// print the contents of a two-dimensional array
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf(" %10.3f ", arr[i][j]);
putchar('\n');
}
}