Bird
0
0
DSA Cprogramming~30 mins

Arrays vs Other Data Structures When to Choose Arrays in DSA C - Build Both Approaches

Choose your learning style9 modes available
Arrays vs Other Data Structures: When to Choose Arrays
📖 Scenario: You are working on a simple inventory system for a small bookstore. You need to store the prices of books in a way that allows quick access by position because the store owner often asks for the price of a book by its shelf number.
🎯 Goal: Build a program that uses an array to store book prices, then calculates how many books are priced below a certain threshold. This will help the store owner quickly find affordable books.
📋 What You'll Learn
Create an array called book_prices with exactly these 5 prices: 120, 85, 150, 60, 90
Create an integer variable called price_limit and set it to 100
Use a for loop with variable i to count how many books have prices less than price_limit
Print the count of affordable books using printf
💡 Why This Matters
🌍 Real World
Arrays are used in many real-world applications where you need to store a fixed number of items and access them quickly by their position, like storing prices, scores, or sensor readings.
💼 Career
Understanding arrays is fundamental for programming jobs because they are the building blocks for more complex data structures and algorithms.
Progress0 / 4 steps
1
Create the array of book prices
Create an integer array called book_prices with these exact values: 120, 85, 150, 60, 90
DSA C
Hint

Use the syntax int array_name[size] = {values}; to create the array.

2
Set the price limit
Create an integer variable called price_limit and set it to 100
DSA C
Hint

Use int price_limit = 100; to create the variable.

3
Count books priced below the limit
Create an integer variable called count and set it to 0. Then use a for loop with variable i from 0 to 4 to check each price in book_prices. Inside the loop, if book_prices[i] is less than price_limit, increase count by 1.
DSA C
Hint

Initialize count to zero before the loop. Use for (int i = 0; i < 5; i++) to loop through the array.

4
Print the count of affordable books
Use printf to print the value of count with the message: "Number of affordable books: %d\n"
DSA C
Hint

Use printf("Number of affordable books: %d\n", count); to print the result.