0
0
DSA C++programming~30 mins

Linear Search Algorithm in DSA C++ - Build from Scratch

Choose your learning style9 modes available
Linear Search Algorithm
📖 Scenario: You have a list of product IDs in a store's inventory. You want to find if a specific product ID is available by checking each product one by one.
🎯 Goal: Build a simple linear search program in C++ that looks for a given product ID in a list and tells if it is found or not.
📋 What You'll Learn
Create an array called products with these exact integers: 101, 203, 405, 607, 809
Create an integer variable called target and set it to 405
Use a for loop with variable i to iterate over the products array
Inside the loop, use an if statement to check if products[i] equals target
Create a boolean variable called found to track if the target is found
Print "Found" if the target is in the array, otherwise print "Not Found"
💡 Why This Matters
🌍 Real World
Stores and inventory systems often need to check if a product is available by searching through lists of product IDs.
💼 Career
Understanding linear search is a fundamental skill for software developers working with data lookup and simple search tasks.
Progress0 / 4 steps
1
Create the product list
Create an integer array called products with these exact values: 101, 203, 405, 607, 809
DSA C++
Hint

Use int products[] = {101, 203, 405, 607, 809}; to create the array.

2
Set the target product ID
Create an integer variable called target and set it to 405
DSA C++
Hint

Use int target = 405; to set the product ID to search for.

3
Search for the target using a loop
Create a boolean variable called found and set it to false. Use a for loop with variable i to iterate over the products array. Inside the loop, use an if statement to check if products[i] equals target. If yes, set found to true and use break to stop the loop.
DSA C++
Hint

Use a for loop from 0 to 4 and check each product. Set found to true and break if you find the target.

4
Print the search result
Use an if statement to print "Found" if found is true. Otherwise, print "Not Found".
DSA C++
Hint

Use if (found) to print "Found" and else to print "Not Found".