Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- C Primer Plus
- Chapter 10: Arrays and Pinters.
- Programming Exercise 7:
- Write a program that initializes a two-dimensional array-of- double and uses one of the
- copy functions from exercise 2 to copy it to a second two-dimensional array. (Because a
- two-dimensional array is an array of arrays, a one-dimensional copy function can be used
- with each subarray.)
- */
- #include <stdio.h>
- #define SIZE_1 3 //COLS
- #define SIZE_2 2 //ROWS
- void copy_arr_1D (int , double [], double []);
- void copy_arr_2D (int, int , double [][SIZE_1], double [][SIZE_1]);
- void print_arr_1D (int , double []);
- void print_arr_2D (int , int, double [][SIZE_1]);
- int main(void) {
- double target[SIZE_2][SIZE_1] = {0};
- double source[SIZE_2][SIZE_1] = {
- {1.1, 1.2, 1.3},
- {2.1, 2.2, 2.3}
- };
- copy_arr_2D(SIZE_2, SIZE_1, target, source);
- print_arr_2D(SIZE_2, SIZE_1, target);
- return 0;
- }
- //---------------------------------------------------------------
- void copy_arr_1D (int size_1, double target[], double source[]) {
- for (int i = 0; i < size_1; i++) {
- target[i] = source[i];
- }
- }
- //---------------------------------------------------------------
- void copy_arr_2D (int size_2, int size_1, double target[][size_1], double source[][size_1]) {
- for (int i = 0; i < size_2; i++) {
- copy_arr_1D(size_1, target[i], source[i]);
- }
- }
- //---------------------------------------------------------------
- void print_arr_1D (int size_1, double arr[]) {
- for (int i = 0; i < size_1; i++) {
- printf("%f\t", arr[i]);
- }
- }
- //---------------------------------------------------------------
- void print_arr_2D (int size_2, int size_1, double arr[][size_1]) {
- for (int i = 0; i < size_2; i++) {
- print_arr_1D(size_1, arr[i]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement