📖 Scenario: You are organizing seating for a small movie theater. The theater has 3 rows and 4 seats in each row. You want to keep track of how many people are sitting in each seat.
🎯 Goal: Create a 2D array to represent the seats, fill it with some numbers, then calculate the total number of people seated in the theater.
📋 What You'll Learn
Create a 2D array called seats with 3 rows and 4 columns
Initialize the array with exact values for each seat
Create an integer variable called totalPeople to hold the sum
Use nested for loops with variables row and col to iterate over seats
Add the number of people in each seat to totalPeople
Print the total number of people using std::cout
💡 Why This Matters
🌍 Real World
Multi-dimensional arrays are used to represent grids, tables, or matrices such as seating charts, game boards, or pixel data in images.
💼 Career
Understanding multi-dimensional arrays is essential for software developers working with data structures, graphics, simulations, and many other fields.
Progress0 / 4 steps
1
Create the 2D array seats
Create a 2D array called seats with 3 rows and 4 columns. Initialize it with these exact values: first row {1, 0, 2, 1}, second row {0, 3, 1, 0}, third row {2, 1, 0, 1}.
C++
Hint
Use int seats[3][4] = { ... }; to create and initialize the array.
2
Create the variable totalPeople
Create an integer variable called totalPeople and set it to 0. This will hold the total number of people seated.
C++
Hint
Use int totalPeople = 0; to create and initialize the variable.
3
Sum all people using nested for loops
Use nested for loops with variables row and col to iterate over the seats array. Add each seat's value to totalPeople.
C++
Hint
Use two for loops: outer loop for rows, inner loop for columns. Add each seat value to totalPeople.
4
Print the total number of people
Use std::cout to print the text Total people seated: followed by the value of totalPeople.
C++
Hint
Use std::cout << "Total people seated: " << totalPeople << std::endl; to print the result.
Practice
(1/5)
1. What is a multi-dimensional array in C++?
easy
A. A variable that holds a single number
B. An array that stores only one value
C. A function that returns multiple values
D. An array with more than one index, like rows and columns
Solution
Step 1: Understand array dimensions
A multi-dimensional array uses multiple indexes to access elements, such as row and column in a table.
Step 2: Compare options
Only An array with more than one index, like rows and columns describes an array with more than one index, matching the concept of multi-dimensional arrays.
Final Answer:
An array with more than one index, like rows and columns -> Option D
Quick Check:
Multi-dimensional array = multiple indexes [OK]
Hint: Think of rows and columns in a table for multi-dimensional arrays [OK]
Common Mistakes:
Confusing arrays with single values
Thinking functions return multiple values
Mixing variables with arrays
2. Which of the following is the correct way to declare a 2D array of integers with 3 rows and 4 columns in C++?
easy
A. int arr[3][4];
B. int arr(3,4);
C. int arr[4][3];
D. int arr{3,4};
Solution
Step 1: Recall C++ 2D array syntax
The correct syntax uses square brackets with sizes: int arr[rows][columns];
Step 2: Check each option
int arr[3][4]; uses int arr[3][4]; which means 3 rows and 4 columns, correct syntax. Others are invalid or reversed.
Final Answer:
int arr[3][4]; -> Option A
Quick Check:
Use square brackets for dimensions [OK]
Hint: Use square brackets for rows and columns in declaration [OK]