Working with two-dimensional array at runtime in C programming

In C programming, working with two-dimensional arrays at runtime involves dynamically allocating memory and performing operations on the array elements. This approach allows for flexible array sizes determined during program execution.

Syntax

// Static array declaration
int array[rows][cols];

// Dynamic allocation for 2D arrays
int **array = (int**)malloc(rows * sizeof(int*));
for(int i = 0; i < rows; i++) {
    array[i] = (int*)malloc(cols * sizeof(int));
}

Problem

Write a C program to calculate sum and product of all elements in two-dimensional arrays using runtime operations.

Solution

  • Runtime operations involve processing array elements during program execution
  • We can perform mathematical operations like addition and multiplication on corresponding elements of two arrays
  • The program demonstrates element-wise operations on 2D arrays

Example: Array Operations at Runtime

This example shows how to perform sum and product operations on two 2D arrays −

#include <stdio.h>

int main() {
    // Declaring the arrays with fixed size for demonstration
    int A[2][3], B[2][3], sum[2][3], product[2][3];
    int i, j;
    
    // Initialize array A with sample values
    printf("Array A:<br>");
    int valuesA[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 3; j++) {
            A[i][j] = valuesA[i][j];
            printf("%d\t", A[i][j]);
        }
        printf("<br>");
    }
    
    // Initialize array B with sample values  
    printf("\nArray B:<br>");
    int valuesB[2][3] = {{2, 3, 4}, {5, 6, 7}};
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 3; j++) {
            B[i][j] = valuesB[i][j];
            printf("%d\t", B[i][j]);
        }
        printf("<br>");
    }
    
    // Calculating sum and printing output
    printf("\nSum array is:<br>");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 3; j++) {
            sum[i][j] = A[i][j] + B[i][j];
            printf("%d\t", sum[i][j]);
        }
        printf("<br>");
    }
    
    // Calculating product and printing output
    printf("\nProduct array is:<br>");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 3; j++) {
            product[i][j] = A[i][j] * B[i][j];
            printf("%d\t", product[i][j]);
        }
        printf("<br>");
    }
    
    return 0;
}
Array A:
1	2	3	
4	5	6	

Array B:
2	3	4	
5	6	7	

Sum array is:
3	5	7	
9	11	13	

Product array is:
2	6	12	
20	30	42	

Key Points

  • Element-wise addition: sum[i][j] = A[i][j] + B[i][j]
  • Element-wise multiplication: product[i][j] = A[i][j] * B[i][j]
  • Nested loops are used to traverse 2D arrays efficiently
  • Both arrays must have the same dimensions for element-wise operations

Conclusion

Working with 2D arrays at runtime allows performing mathematical operations on array elements during program execution. The nested loop structure efficiently handles element-wise operations like addition and multiplication between corresponding array elements.

Updated on: 2026-03-15T13:08:21+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements