0
0
Cprogramming~15 mins

Array initialization in C - Mini Project: Build & Apply

Choose your learning style9 modes available
Array initialization
📖 Scenario: You are helping a small store keep track of the prices of five popular fruits. You want to store these prices in a simple list so you can use them later for calculations.
🎯 Goal: Create an array to hold the prices of five fruits, then print all the prices one by one.
📋 What You'll Learn
Create an array called prices with exactly five float values: 1.20, 0.50, 0.75, 2.00, and 1.50
Create an integer variable called i for looping
Use a for loop with i from 0 to 4 to print each price on its own line using printf
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of related data like prices, scores, or measurements in many programs.
💼 Career
Understanding arrays and loops is essential for programming tasks in software development, data processing, and embedded systems.
Progress0 / 4 steps
1
Create the array of prices
Create a float array called prices with these exact values: 1.20, 0.50, 0.75, 2.00, and 1.50
C
Need a hint?

Use the syntax: float prices[5] = {value1, value2, value3, value4, value5};

2
Add a loop counter variable
Add an integer variable called i to use as a counter for looping
C
Need a hint?

Write int i; inside main() before the loop.

3
Use a for loop to print each price
Use a for loop with i from 0 to 4 to print each price from the prices array on its own line using printf
C
Need a hint?

Use for (i = 0; i < 5; i++) and inside the loop printf("%.2f\n", prices[i]);

4
Print the prices to see the output
Run the program to print all the prices stored in the prices array, each on its own line
C
Need a hint?

Make sure your printf prints each price on its own line with two decimals.