Advertisement
Guest User

Untitled

a guest
Aug 7th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. /*
  2.  
  3.   C Primer Plus
  4.   Chapter 10: Arrays and Pinters.
  5.   Programming Exercise 7:
  6.  
  7.   Write a program that initializes a two-dimensional array-of- double and uses one of the
  8.   copy functions from exercise 2 to copy it to a second two-dimensional array. (Because a
  9.   two-dimensional array is an array of arrays, a one-dimensional copy function can be used
  10.   with each subarray.)
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15. #define SIZE_1 3 //COLS
  16. #define SIZE_2 2 //ROWS
  17.  
  18. void copy_arr_1D (int , double [], double []);
  19. void copy_arr_2D (int, int , double [][SIZE_1], double [][SIZE_1]);
  20.  
  21. void print_arr_1D (int , double []);
  22. void print_arr_2D (int , int, double [][SIZE_1]);
  23.  
  24. int main(void) {
  25.  
  26.   double target[SIZE_2][SIZE_1] = {0};
  27.   double source[SIZE_2][SIZE_1] = {
  28.                                     {1.1, 1.2, 1.3},
  29.                                     {2.1, 2.2, 2.3}
  30.                                   };
  31.  
  32.   copy_arr_2D(SIZE_2, SIZE_1, target, source);
  33.   print_arr_2D(SIZE_2, SIZE_1, target);
  34.  
  35.   return 0;
  36. }
  37.  
  38. //---------------------------------------------------------------
  39. void copy_arr_1D (int size_1, double target[], double source[]) {
  40.   for (int i = 0; i < size_1; i++) {
  41.     target[i] = source[i];
  42.   }
  43. }
  44.  
  45. //---------------------------------------------------------------
  46. void copy_arr_2D (int size_2, int size_1, double target[][size_1], double source[][size_1]) {
  47.   for (int i = 0; i < size_2; i++) {
  48.     copy_arr_1D(size_1, target[i], source[i]);
  49.   }
  50. }
  51.  
  52. //---------------------------------------------------------------
  53. void print_arr_1D (int size_1, double arr[]) {
  54.   for (int i = 0; i < size_1; i++) {
  55.     printf("%f\t", arr[i]);
  56.   }
  57. }
  58.  
  59. //---------------------------------------------------------------
  60. void print_arr_2D (int size_2, int size_1, double arr[][size_1]) {
  61.   for (int i = 0; i < size_2; i++) {
  62.     print_arr_1D(size_1, arr[i]);
  63.     printf("\n");
  64.   }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement