0
0
Cprogramming~15 mins

Array size and bounds in C - Mini Project: Build & Apply

Choose your learning style9 modes available
Array size and bounds
📖 Scenario: You are working on a simple program that stores the ages of a small group of friends. You want to make sure you create an array with the right size and access its elements safely.
🎯 Goal: Create an array to hold exactly 5 ages, set a limit variable for the array size, use a loop to print each age, and display the ages one by one.
📋 What You'll Learn
Create an integer array called ages with exactly 5 elements: 21, 25, 30, 22, 28
Create an integer variable called size and set it to the number of elements in ages
Use a for loop with an index variable i from 0 to size - 1 to access each element in ages
Print each age inside the loop using printf
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of data like ages, scores, or measurements in many programs.
💼 Career
Understanding array size and bounds is essential for writing safe and efficient code in software development.
Progress0 / 4 steps
1
Create the array of ages
Create an integer array called ages with exactly 5 elements: 21, 25, 30, 22, and 28.
C
Need a hint?

Use the syntax int ages[5] = {value1, value2, value3, value4, value5}; to create the array.

2
Set the size variable
Create an integer variable called size and set it to 5, the number of elements in the ages array.
C
Need a hint?

Just write int size = 5; to store the array length.

3
Loop through the array
Use a for loop with an index variable i starting at 0 and running while i < size. Inside the loop, access each element of ages using ages[i].
C
Need a hint?

Use for (int i = 0; i < size; i++) and inside the loop use ages[i] to get each age.

4
Print each age
Inside the for loop, use printf to print each age in the format: Age: X where X is the current age from ages[i]. Then run the program to see the output.
C
Need a hint?

Use printf("Age: %d\n", ages[i]); inside the loop to print each age on its own line.