📖 Scenario: Imagine you run a small bookstore. You want to keep track of the prices of exactly 5 popular books you sell every day. You need a simple way to store these prices so you can quickly find and update them.
🎯 Goal: You will create an array to store the prices of 5 books. Then, you will set a price limit and find which books cost less than or equal to that limit. Finally, you will print those prices.
📋 What You'll Learn
Create an array called bookPrices with exactly these 5 prices: 120, 85, 150, 60, 90
Create an integer variable called priceLimit and set it to 100
Use a for loop with variable i to check each price in bookPrices
Inside the loop, add prices less than or equal to priceLimit to a new list called affordableBooks
Print the affordableBooks list prices separated by spaces
💡 Why This Matters
🌍 Real World
Stores and manages lists of items like prices, scores, or measurements in many apps.
💼 Career
Understanding arrays is essential for programming jobs that handle collections of data efficiently.
Progress0 / 4 steps
1
Create the array of book prices
Create an array called bookPrices with these exact prices: 120, 85, 150, 60, 90
DSA C#
Hint
Use int[] bookPrices = { ... }; to create the array with the exact prices.
2
Set the price limit
Create an integer variable called priceLimit and set it to 100
DSA C#
Hint
Use int priceLimit = 100; to create the variable.
3
Find affordable books using a loop
Create a list of integers called affordableBooks. Use a for loop with variable i to check each price in bookPrices. Inside the loop, if the price is less than or equal to priceLimit, add it to affordableBooks
DSA C#
Hint
Remember to create affordableBooks as a list before the loop. Use for (int i = 0; i < bookPrices.Length; i++) to loop through the array.
4
Print the affordable book prices
Use Console.WriteLine to print all prices in affordableBooks separated by spaces
DSA C#
Hint
Use Console.WriteLine(string.Join(" ", affordableBooks)); to print the list separated by spaces.
Practice
(1/5)
1. Why do arrays exist in programming languages like C#?
easy
A. To store multiple values of the same type in a single, fixed-size structure
B. To store different types of data in one variable
C. To perform mathematical calculations faster
D. To replace all other data structures
Solution
Step 1: Understand the purpose of arrays
Arrays are designed to hold multiple values of the same type together in one place.
Step 2: Recognize the fixed size nature
Arrays have a fixed size, meaning the number of elements is set when created.
Final Answer:
To store multiple values of the same type in a single, fixed-size structure -> Option A
Quick Check:
Arrays = fixed-size same-type storage [OK]
Hint: Arrays hold same-type items in fixed size [OK]
Common Mistakes:
Thinking arrays can store different data types
Believing arrays can change size dynamically
Confusing arrays with lists or other collections
2. Which of the following is the correct way to declare an integer array of size 5 in C#?
easy
A. int numbers = new int[5];
B. int[] numbers = new int[5];
C. int[] numbers = int[5];
D. array int numbers = new int(5);
Solution
Step 1: Recall C# array declaration syntax
In C#, arrays are declared with the type followed by square brackets and initialized with 'new' keyword and size in brackets.
Step 2: Match syntax with options
int[] numbers = new int[5]; matches the correct syntax: int[] numbers = new int[5];
Final Answer:
int[] numbers = new int[5]; -> Option B
Quick Check:
C# array declaration = int[] name = new int[size]; [OK]
Hint: Use type[], new keyword, and size in brackets [OK]
Common Mistakes:
Missing square brackets after type
Using parentheses instead of brackets for size
Declaring array without 'new' keyword
3. What will be the output of the following C# code?
int[] arr = new int[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
for(int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
medium
A. Error: Index out of range
B. 0 0 0
C. 10 20 30
D. 10 20 30 0
Solution
Step 1: Analyze array initialization and assignments
The array of size 3 is created with default values 0, then elements at indexes 0,1,2 are assigned 10, 20, 30 respectively.
Step 2: Trace the for loop output
The loop prints each element separated by space: 10 20 30
Final Answer:
10 20 30 -> Option C
Quick Check:
Print assigned values in order [OK]
Hint: Arrays print assigned values in order [OK]
Common Mistakes:
Assuming default values print instead of assigned
Off-by-one errors in loop causing index errors
Confusing output format with commas or no spaces
4. Identify the error in this C# code snippet that tries to create an array and assign values: