Copy two-dimensional to a second two-dimensional array - C Array

C examples for Array:Multidimensional Arrays

Description

Copy two-dimensional to a second two-dimensional array

Demo Code

#include <stdio.h>

#define ROWS 3/* www.  ja  v  a2  s .  com*/
#define COLUMNS 5

void copy2DimensioalArray(int rows, int cols, double source[ROWS][COLUMNS],
   double target[ROWS][COLUMNS]);
void print2DimensioalArray(int rows, int cols, double arr[ROWS][COLUMNS]);

int main(void)
{
   double array1[ROWS][COLUMNS] = { { 14.3, 15.17, 21.11, 62.63, 3.8 },
   { 5.16, 23.51, 73.2, 12.33, 123 },
   { 2.1, 35.31, 16.35, 0.132, 11.1 } };
   double array2[ROWS][COLUMNS];

   // copy array1 to array2
   copy2DimensioalArray(ROWS, COLUMNS, array1, array2);

   // print contents of arrays
   printf("Array 1:\n");
   print2DimensioalArray(ROWS, COLUMNS, array1);
   putchar('\n');

   printf("Array2:\n");
   print2DimensioalArray(ROWS, COLUMNS, array2);

   return 0;
}
// copy one two-dimensional array to another
void copy2DimensioalArray(int rows, int cols, double source[ROWS][COLUMNS], double target[ROWS][COLUMNS]) {
   for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
         target[i][j] = source[i][j];
      }
   }
}
// print the contents of a two-dimensional array
void print2DimensioalArray(int rows, int cols, double arr[ROWS][COLUMNS]) {
   for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++)
         printf(" %10.3f ", arr[i][j]);
      putchar('\n');
   }
}

Result


Related Tutorials