Dynamic Matrix

Medium Dynamic Memory Allocation
Solve in Playground →

Problem Statement

Write a C program to dynamically allocate a 2D matrix of size rows x cols using an array of pointers, read elements, and print them row by row.

Input Format

First line: rows and cols. Subsequent lines: matrix elements.

Output Format

Print matrix elements row by row separated by spaces.

Constraints

1 <= rows, cols <= 10

Sample Input

2 2
1 2
3 4

Sample Output

1 2
3 4

Explanation

Allocate an array of row pointers (`int**`), allocate memory for each row separately, populate data, print, and properly free all allocated memory.

Starter Code

#include <stdio.h>
#include <stdlib.h>

int main() {
    int rows, cols;
    if (scanf("%d %d", &rows, &cols) == 2) {
        // Write your code here to allocate and handle dynamic 2D matrix
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used