4.1) Multidimensional Arrays in C
Table of Contents
What are Multidimensional Arrays?
Multidimensional arrays in C allow you to store data in a table-like structure with multiple rows and columns. These arrays are essentially arrays of arrays, allowing you to represent more complex data structures, such as matrices.
It’s like having a bunch of boxes arranged in rows and columns, and you can put numbers or other things inside those boxes.
Declaring and Using a 2D Array:
Multidimensional arrays are declared using the syntax:
data_type array_name[row_size][column_size];
You can initialize multidimensional arrays during declaration using nested curly braces {}.
Let’s say you want to make a 2D array to store students’ scores in different subjects. Here’s how you can do it:
#include <stdio.h>
int main() {
// Declaring and initializing a 2D array (2 rows, 3 columns)
int scores[2][3] = {
{90, 85, 78},
{67, 92, 88}
};
// Accessing elements using row and column indices
printf("Score at row 1, column 2: %d\n", scores[0][1]); // 85
return 0;
}
Explanation:
- We declare an array called
scores
with 2 rows and 3 columns to store students’ scores. - We put numbers in the array to represent the scores for each student and each subject.
- To access a score, we use
scores[row_index][column_index]
. For example,scores[0][1]
is the score in the first row (student 1) and second column (subject 2), which is 85.
Using Loops to Work with 2D Arrays:
Using loops can help us easily go through all the elements in a 2D array.
#include <stdio.h>
int main() {
int scores[2][3] = {
{90, 85, 78},
{67, 92, 88}
};
// Printing all scores using nested loops
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("Score at row %d, column %d: %d\n", i, j, scores[i][j]);
}
}
return 0;
}
Explanation:
- We use two nested loops, one for rows and one for columns, to go through each score in the array.
- The
i
loop variable represents the row, and thej
loop variable represents the column. - We use
scores[i][j]
to access the score at the current row and column.
Creating a 3D Array:
Think of a 3D array as a bunch of 2D arrays stacked on top of each other. You can use it to store more complex information, like in a game where you might have a grid of tiles for a 2D map, and you want multiple maps for different levels.
#include <stdio.h>
int main() {
// Declaring and initializing a 3D array (2 maps, 3 rows, 4 columns)
int gameMaps[2][3][4] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};
// Accessing elements using indices for each dimension
printf("Element at map 1, row 2, column 3: %d\n", gameMaps[0][1][2]); // 7
return 0;
}
Explanation:
- We declare a 3D array named
gameMaps
with 2 maps, 3 rows, and 4 columns. - Each map is like a 2D array. We can access elements using three indices:
gameMaps[map_index][row_index][column_index]
.
Remember:
- Multidimensional arrays are like tables with rows and columns.
- To access an element, use multiple indices like
array[row_index][column_index]
. - Loops are great for working with these arrays, especially nested loops for 2D arrays.
- 3D arrays are like having multiple 2D arrays stacked together.
Accessing Elements of a 3×3 Array Using Nested Loops
#include <stdio.h>
int main() {
// Declaring and initializing a 3x3 array
int grid[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing elements using nested loops
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", grid[i][j]);
}
printf("\n");
}
return 0;
}
Real-world example of a 2D array in C
We’ll create a program to manage a simple student gradebook using a 2D array.
The program will allow us to input and display student names and their corresponding grades for different subjects.
#include <stdio.h>
#define NUM_STUDENTS 3
#define NUM_SUBJECTS 5
int main() {
// Declare a 2D array to store student grades
int grades[NUM_STUDENTS][NUM_SUBJECTS];
// Input student names and grades
char studentNames[NUM_STUDENTS][50];
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", studentNames[i]);
printf("Enter grades for student %d (5 subjects):\n", i + 1);
for (int j = 0; j < NUM_SUBJECTS; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &grades[i][j]);
}
}
// Display student names and grades
printf("\nStudent Gradebook:\n");
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Name: %s\n", studentNames[i]);
printf("Grades: ");
for (int j = 0; j < NUM_SUBJECTS; j++) {
printf("%d ", grades[i][j]);
}
printf("\n\n");
}
return 0;
}
Explanation:
- We define
NUM_STUDENTS
andNUM_SUBJECTS
constants to specify the size of the 2D array. - We declare a 2D array
grades
to store student grades. It has dimensionsNUM_STUDENTS
byNUM_SUBJECTS
. - We declare another 2D array
studentNames
to store student names. Each row corresponds to a student. - We use nested loops to input student names and grades. The outer loop iterates over students, and the inner loop iterates over subjects.
- We use
scanf
to input student names and grades from the user. - After input, we use another set of nested loops to display the student names and grades. We print each student’s name and their corresponding grades for each subject.
Example Interaction:
Enter name of student 1: Alice
Enter grades for student 1 (5 subjects):
Subject 1: 90
Subject 2: 85
Subject 3: 78
Subject 4: 92
Subject 5: 88
Enter name of student 2: Bob
Enter grades for student 2 (5 subjects):
Subject 1: 75
Subject 2: 82
Subject 3: 95
Subject 4: 70
Subject 5: 68
Enter name of student 3: Carol
Enter grades for student 3 (5 subjects):
Subject 1: 88
Subject 2: 92
Subject 3: 85
Subject 4: 78
Subject 5: 90
Student Gradebook:
Name: Alice
Grades: 90 85 78 92 88
Name: Bob
Grades: 75 82 95 70 68
Name: Carol
Grades: 88 92 85 78 90
In this example, we’ve used a 2D array to store student grades and names. This demonstrates how 2D arrays can be used to represent and manage structured data in real-world scenarios, such as a gradebook management system.