Bird
0
0
DSA Cprogramming~15 mins

Array Declaration and Initialization in DSA C - Build from Scratch

Choose your learning style9 modes available
Array Declaration and Initialization
📖 Scenario: You are working on a simple program that stores the ages of five friends. You want to keep these ages in an array so you can use them later for calculations or display.
🎯 Goal: Create an array to hold the ages of five friends, set a variable for the number of friends, then print all the ages in order.
📋 What You'll Learn
Declare an integer array called ages with exactly 5 elements
Initialize the ages array with the values 21, 25, 19, 30, and 22 in that order
Create an integer variable called num_friends and set it to 5
Use a for loop with the variable i to iterate from 0 to num_friends - 1
Print each age followed by a space on the same line
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of data like scores, ages, or measurements in many programs.
💼 Career
Understanding arrays is essential for programming jobs because they are a basic way to organize and access multiple values efficiently.
Progress0 / 4 steps
1
Declare and initialize the array
Declare an integer array called ages with 5 elements and initialize it with these exact values: 21, 25, 19, 30, 22.
DSA C
Hint

Use the syntax int arrayName[size] = {values}; to declare and initialize an array in C.

2
Set the number of friends
Create an integer variable called num_friends and set it to 5.
DSA C
Hint

Use int num_friends = 5; to create and set the variable.

3
Loop through the array
Use a for loop with the variable i to iterate from 0 to num_friends - 1.
DSA C
Hint

Use for (int i = 0; i < num_friends; i++) to loop through the array indices.

4
Print the ages
Inside the for loop, print each age from the ages array followed by a space. After the loop, print a newline character.
DSA C
Hint

Use printf("%d ", ages[i]); inside the loop and printf("\n"); after the loop.