void add2darraysint aCOLS int bCOLS int cCOLS int rows int cols void

# Void add2darraysint acols int bcols int ccols int

This preview shows page 11 - 12 out of 12 pages.

void add_2d_arrays(int a[][COLS], int b[][COLS], int c[][COLS], int rows, int cols); void print_2d_array(int a[][COLS], int rows, int cols); int main(void) { int i, j, a[ROWS][COLS], b[ROWS][COLS], c[ROWS][COLS]; int rows, cols; printf("Enter number of rows for Matrix 1: "); scanf("%d", &rows); printf("Enter number of columns for Matrix 1: "); scanf("%d", &cols); read_2d_array(a, rows, cols); // reading matrix a read_2d_array(b, rows, cols); // reading matrix a add_2d_arrays(a, b, c, rows, cols); /* Addition of two matrices */ printf("The sum of two matrices is: \n"); print_2d_array(c, rows, cols); /*Print sum of two matrices */ system("pause"); return 0; }
12 Example 2 … void read_2d_array(int a[][COLS], int rows, int cols) { int i, j; printf("Enter the %d elements of the 2-D array row-wise: \n", rows * cols); for(i=0; i<rows; i++) { for(j=0; j<cols; j++) scanf("%d", &a[i][j]); } } void add_2d_arrays(int a[][COLS], int b[][COLS], int c[][COLS], int rows, int cols) { int i, j; for (i=0; i<rows; i++) { for (j=0; j<cols; j++) c[i][j] = a[i][j] + b[i][j]; } } void print_2d_array(int a[][COLS], int rows, int cols) { int i, j; for(i=0; i<rows; i++) { for (j=0; j<cols; j++) printf("%5d ", a[i][j]); printf("\n"); } }

#### You've reached the end of your free preview.

Want to read all 12 pages?

• Spring '10
• baleh
• Array, #include, #define, The Elements, actual size, 2-d array