📖 Scenario: Imagine you are organizing a bookshelf. You want to keep your books in a neat row so you can find any book quickly by its position. This is similar to how arrays work in programming.
🎯 Goal: You will create a simple array to store a list of book IDs, set a limit on how many books you can store, add some books to the array, and then print the list of books. This will show why arrays are useful for storing multiple items in order.
📋 What You'll Learn
Create an array of integers called books with space for exactly 5 items
Create an integer variable called max_books and set it to 5
Add exactly 3 book IDs to the books array at the first three positions
Print all the book IDs stored in the books array using a for loop
💡 Why This Matters
🌍 Real World
Arrays are used in many programs to store lists like scores, names, or sensor readings in order.
💼 Career
Understanding arrays is essential for programming jobs because they are a basic way to organize and manage data efficiently.
Progress0 / 4 steps
1
Create an array to hold book IDs
Create an integer array called books with space for exactly 5 items.
DSA C
Hint
Use the syntax int array_name[size]; to create an array of integers.
2
Set the maximum number of books
Create an integer variable called max_books and set it to 5.
DSA C
Hint
Use int max_books = 5; to create and set the variable.
3
Add book IDs to the array
Add exactly 3 book IDs to the books array at positions 0, 1, and 2. Use these values: 101, 102, and 103 respectively.
DSA C
Hint
Use the syntax array[index] = value; to assign values to array positions.
4
Print all book IDs in the array
Use a for loop with an integer variable i from 0 to less than max_books to print all the book IDs stored in the books array. Use printf to print each book ID followed by a space.
DSA C
Hint
Remember that uninitialized array elements contain zero by default in this example.
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 variable
B. To store different types of data in one variable
C. To perform mathematical calculations automatically
D. To create functions that run faster
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many values of the same type together, making data management easier.
Step 2: Compare with other options
Options B, C, and D describe features unrelated to arrays' main purpose.
Final Answer:
To store multiple values of the same type in a single variable -> Option A
Quick Check:
Arrays store same-type values together = A [OK]
Hint: Arrays group same-type values for easy access [OK]