0
0
DSA C++programming~30 mins

Search in 2D Matrix in DSA C++ - Build from Scratch

Choose your learning style9 modes available
Search in 2D Matrix
📖 Scenario: You have a grid of numbers arranged in rows and columns, like seats in a theater. You want to find if a specific number is present in this grid.
🎯 Goal: Build a program that searches for a given number in a 2D matrix and tells if it is found or not.
📋 What You'll Learn
Create a 2D matrix with exact values
Create a variable for the target number to search
Use nested loops to search the target in the matrix
Print 'Found' if the target is in the matrix, else print 'Not Found'
💡 Why This Matters
🌍 Real World
Searching in grids or tables is common in games, maps, and spreadsheets.
💼 Career
Understanding how to search in 2D data structures is important for software development and data analysis.
Progress0 / 4 steps
1
Create the 2D matrix
Create a 2D vector called matrix with these exact rows and columns:
{ {1, 3, 5}, {7, 9, 11}, {13, 15, 17} }
DSA C++
Hint

Use std::vector> to create a 2D matrix with the exact rows and columns.

2
Set the target number to search
Create an integer variable called target and set it to 9
DSA C++
Hint

Use int target = 9; to create the target number.

3
Search the target in the matrix
Use nested for loops with variables i and j to iterate over matrix. Create a boolean variable found set to false. If matrix[i][j] equals target, set found to true and break both loops.
DSA C++
Hint

Use two for loops to check each element. Use found to remember if target is found and break loops early.

4
Print the search result
Print "Found" if found is true, otherwise print "Not Found"
DSA C++
Hint

Use if (found) to decide what to print. Use std::cout to print the result.