Back to C Programming
2026-07-126 min read

Example 1: Two-dimensional array to store and print values (C Programming)

Learn Example 1: Two-dimensional array to store and print values (C Programming) step by step with clear examples and exercises.

Here's the revised C programming lesson on "Example 1: Two-dimensional array to store and print values (C Programming)" that meets the required standards:

Why This Matters

In this lesson, we will learn how to create and manipulate two-dimensional arrays in C programming. Two-dimensional arrays are essential for handling data organized in a table-like structure, such as matrices, tables, or grids of data. They are useful in various applications like game development, image processing, and scientific simulations.

Two-dimensional arrays can help you solve problems that require dealing with multiple sets of data, making them an important tool for programmers. Understanding how to work with two-dimensional arrays will enable you to tackle more complex programming tasks and prepare you for real-world coding scenarios.

Prerequisites

Before diving into two-dimensional arrays, it is crucial to have a good understanding of the following topics:

  1. Basic data types (int, char, float, etc.)
  2. Variables and assignment operators
  3. Arrays and pointers
  4. Control structures (if, else, for, while)
  5. Input/Output functions (scanf(), printf())
  6. Understanding memory allocation in C

Core Concept

Declaring a Two-dimensional Array

A two-dimensional array in C is declared using two sets of square brackets, representing the number of rows and columns:

int arr[3][4]; // This declares an array with 3 rows and 4 columns, each element being an integer.

Initializing a Two-dimensional Array

You can initialize the elements of a two-dimensional array during declaration using curly braces:

int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

Accessing Elements in a Two-dimensional Array

To access an element in a two-dimensional array, use the indexes for both dimensions:

printf("%d", arr[1][1]); // Output: 6

Printing a Two-dimensional Array

You can print a two-dimensional array using nested loops to iterate through all its elements:

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

Worked Example

Let's create a program that takes a two-dimensional array as input, calculates the sum of each row and column, and prints the results.

#include <stdio.h>

int main() {
int arr[3][4];
int rows = 3;
int cols = 4;

printf("Enter elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}

// Calculate row sums
int rowSums[3];
for (int i = 0; i < rows; i++) {
rowSums[i] = 0;
for (int j = 0; j < cols; j++) {
rowSums[i] += arr[i][j];
}
}

// Calculate column sums
int colSums[4];
for (int j = 0; j < cols; j++) {
colSums[j] = 0;
for (int i = 0; i < rows; i++) {
colSums[j] += arr[i][j];
}
}

// Print the original array, row sums, and column sums
printf("\nOriginal Array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

printf("\nRow Sums:\n");
for (int i = 0; i < rows; i++) {
printf("Row %d: %d\n", i + 1, rowSums[i]);
}

printf("\nColumn Sums:\n");
for (int j = 0; j < cols; j++) {
printf("Column %d: %d\n", j + 1, colSums[j]);
}

return 0;
}

Common Mistakes

  1. Forgetting to initialize the row sums and column sums arrays before calculating their values.
  2. Using incorrect indexes when accessing elements in a two-dimensional array.
  3. Not properly handling user input, which may lead to unexpected behavior or runtime errors.
  4. Incorrectly formatting the input/output statements, causing incorrect results or program crashes.
  5. Failing to allocate memory dynamically for multi-dimensional arrays when necessary.
  6. Assuming that two-dimensional arrays can be passed as function arguments without proper handling (e.g., using pointers).
  7. Not understanding the difference between a two-dimensional array and a pointer to a one-dimensional array.

Practice Questions

  1. Write a program that finds the maximum element in a two-dimensional array of integers.
  2. Write a program that checks if a given two-dimensional array is symmetric (i.e., arr[i][j] == arr[j][i] for all i and j).
  3. Write a program that finds the transpose of a given two-dimensional array (swapping rows with columns).
  4. Write a program that multiplies two two-dimensional arrays using element-wise multiplication.
  5. Write a program that calculates the determinant of a 2x2 matrix represented as a two-dimensional array.
  6. Write a program that sorts a two-dimensional array based on the values in one column (e.g., sorting by the first column).
  7. Write a program that finds the average value for each row and each column in a two-dimensional array of floating-point numbers.
  8. Write a program that calculates the product of all elements in a two-dimensional array of integers.
  9. Write a program that checks if a given two-dimensional array contains any duplicate values (i.e., same element appears more than once).
  10. Write a program that finds the minimum and maximum values in each row and each column of a two-dimensional array of integers.

FAQ

  1. How can I declare a three-dimensional array in C?

A three-dimensional array is declared using three sets of square brackets:

int arr[2][3][4]; // This declares an array with 2 layers of arrays, each containing 3 arrays with 4 elements.
  1. How do I pass a two-dimensional array as a function argument in C?

To pass a two-dimensional array as a function argument, you can either pass it by value or by reference. Passing by value means that the entire array is copied to the function's local memory, while passing by reference allows the function to modify the original array. Here's an example of passing by reference:

void printArray(int arr[][4], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

int main() {
int arr[3][4] = {...};
printArray(arr, 3); // Passing the array by reference
}
  1. How can I dynamically allocate memory for a two-dimensional array in C?

To dynamically allocate memory for a two-dimensional array, you can use two pointers and allocate memory for each row separately:

int **arr; // Declare a pointer to a pointer (double pointer)
int rows, cols; // Get the number of rows and columns from user input

arr = (int *)malloc(rows * sizeof(int *)); // Allocate memory for pointers
for (int i = 0; i < rows; i++) {
arr[i] = (int *)malloc(cols * sizeof(int)); // Allocate memory for each row
}
  1. What is the difference between a two-dimensional array and a pointer to a one-dimensional array in C?

A two-dimensional array is an array of arrays, while a pointer to a one-dimensional array is a single pointer that points to the first element of a one-dimensional array. In C, you can treat a two-dimensional array as a pointer to a one-dimensional array by using pointer arithmetic:

int arr[3][4]; // Two-dimensional array
int *ptr = arr; // Pointer to the first element of the two-dimensional array (i.e., arr[0][0])
  1. How do I find the size of a two-dimensional array in C?

The size of a two-dimensional array is determined by the number of elements it contains, which can be calculated using the following formula: sizeof(arr) / (sizeof(arr[0][0]) * rows * cols). However, Note that that this approach may not work for dynamically allocated arrays. In such cases, you should keep track of the number of rows and columns separately.

  1. How do I sort a two-dimensional array in C?

Sorting a two-dimensional array can be achieved using various algorithms like bubble sort, quicksort, or merge sort. You can choose an appropriate algorithm based on the size of your data and the required performance. Here's an example of sorting a two-dimensional array using bubble sort:

void bubbleSort(int arr[][4], int rows, int cols) {
for (int i = 0; i < rows - 1; i++) {
for (int j = 0; j < cols - 1; j++) {
if (arr[i][j] > arr[i + 1][j]) {
// Swap elements if they are in the wrong order
int temp = arr[i][j];
arr[i][j] = arr[i + 1][j];
arr[i + 1][j] = temp;
}
}
}
}