📖 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
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
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
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
Hint
Make sure your printf prints each price on its own line with two decimals.
Practice
(1/5)
1. Which of the following correctly declares and initializes an integer array of size 3 with values 1, 2, and 3 in C?
easy
A. int arr = {1, 2, 3};
B. int arr[3] = {1, 2, 3};
C. int arr[3] = (1, 2, 3);
D. int arr[] = 1, 2, 3;
Solution
Step 1: Understand array declaration syntax
In C, arrays are declared with a type, name, and size in square brackets, e.g., int arr[3].
Hint: Uninitialized array elements default to zero if partially initialized [OK]
Common Mistakes:
Assuming uninitialized elements have garbage values
Expecting compilation error for partial init
Confusing array size with initializer count
4. Identify the error in the following C code snippet:
int data[3];
data = {1, 2, 3};
medium
A. Missing semicolon after array declaration.
B. Array size must be omitted when initializing.
C. Arrays cannot be assigned values after declaration like this.
D. Braces should be parentheses for assignment.
Solution
Step 1: Understand array assignment rules
In C, arrays cannot be assigned values after declaration using = and braces.
Step 2: Check other options
Semicolon is present, size can be specified, braces are correct for initialization but not assignment.
Final Answer:
Arrays cannot be assigned values after declaration like this. -> Option C
Quick Check:
Array assignment after declaration is invalid [OK]
Hint: Arrays must be initialized at declaration, not assigned later [OK]
Common Mistakes:
Trying to assign array with braces after declaration
Confusing array assignment with pointer assignment
Using parentheses instead of braces
5. You want to create an integer array of size 6 where the first two elements are 10 and 20, and the rest are zeros. Which of the following is the best way to initialize it in C?
hard
A. int arr[6] = {0}; arr[0] = 10; arr[1] = 20;
B. int arr[6] = {10, 20, 0, 0, 0, 0};
C. int arr[6] = {10, 20};
D. int arr[6]; arr[0] = 10; arr[1] = 20;
Solution
Step 1: Analyze partial initialization
int arr[6] = {10, 20}; partially initializes first two elements; rest are zero by default, so it works.
Step 2: Compare with explicit zero initialization and assignment
int arr[6] = {0}; arr[0] = 10; arr[1] = 20; explicitly zeroes all elements then assigns first two, ensuring clarity and no garbage.
Step 3: Evaluate best practice
int arr[6] = {0}; arr[0] = 10; arr[1] = 20; is best for clarity and safety; int arr[6] = {10, 20}; works but less explicit; int arr[6] = {10, 20, 0, 0, 0, 0}; is verbose; int arr[6]; arr[0] = 10; arr[1] = 20; leaves uninitialized elements.
Final Answer:
int arr[6] = {0}; arr[0] = 10; arr[1] = 20; -> Option A
Quick Check:
Explicit zero init then assign first elements [OK]
Hint: Zero initialize first, then assign specific elements for clarity [OK]
Common Mistakes:
Assuming partial init always safe without explicit zero
Not initializing array before assignment
Overwriting zeros with uninitialized values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.