📖 Scenario: You are helping a small store keep track of the number of items sold each day over a week. You will use a one-dimensional array to store these daily sales numbers.
🎯 Goal: Build a simple C program that stores daily sales in an array, calculates the total sales for the week, and prints the total.
📋 What You'll Learn
Create an integer array named daily_sales with exactly 7 elements representing sales for each day of the week.
Create an integer variable named total_sales to hold the sum of all sales.
Use a for loop with the variable i to iterate over the daily_sales array.
Add each element of daily_sales to total_sales inside the loop.
Print the value of total_sales using printf.
💡 Why This Matters
🌍 Real World
Stores and small businesses often track daily sales to understand their performance and plan inventory.
💼 Career
Knowing how to use arrays and loops is essential for programming tasks involving data collection and processing in many software development jobs.
Progress0 / 4 steps
1
Create the sales array
Create an integer array called daily_sales with these exact values: 5, 8, 6, 7, 10, 9, 4.
C
Hint
Use curly braces {} to list the values inside the array.
2
Create the total sales variable
Create an integer variable called total_sales and set it to 0.
C
Hint
Initialize total_sales to zero before adding values.
3
Sum the sales using a for loop
Use a for loop with the variable i to iterate from 0 to 6 inclusive. Inside the loop, add daily_sales[i] to total_sales.
C
Hint
Use total_sales += daily_sales[i]; inside the loop to add each day's sales.
4
Print the total sales
Use printf to print the text Total sales: followed by the value of total_sales and a newline.
C
Hint
Use printf("Total sales: %d\n", total_sales); to print the result.
Practice
(1/5)
1. What is the starting index of a one-dimensional array in C?
easy
A. 0
B. 1
C. -1
D. Depends on the array size
Solution
Step 1: Understand array indexing in C
In C, arrays always start indexing from 0, meaning the first element is at index 0.
Step 2: Confirm the starting index
This is a fixed rule in C and does not depend on array size or other factors.
Final Answer:
0 -> Option A
Quick Check:
Array indexing starts at 0 [OK]
Hint: Remember: C arrays always start at zero index [OK]
Common Mistakes:
Thinking arrays start at 1 like in some other languages
Assuming negative indexes are valid
Believing index depends on array size
2. Which of the following is the correct way to declare an integer array of size 5 in C?
easy
A. int arr = 5;
B. int arr();
C. array int arr[5];
D. int arr[5];
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared by specifying the type, name, and size in square brackets, like int arr[5];
Step 2: Check each option
int arr[5]; matches the correct syntax. int arr(); uses parentheses which is for functions. int arr = 5; assigns a single int, not an array. array int arr[5]; uses invalid keyword 'array'.
Final Answer:
int arr[5]; -> Option D
Quick Check:
Array declaration uses square brackets [OK]
Hint: Use square brackets for array size in declaration [OK]
Common Mistakes:
Using parentheses instead of square brackets
Assigning a single value instead of declaring array
Using invalid keywords like 'array'
3. What will be the output of the following code?
int arr[3] = {10, 20, 30};
printf("%d", arr[1]);
medium
A. 10
B. 30
C. 20
D. Compilation error
Solution
Step 1: Identify the array elements and indexes
The array arr has elements: arr[0]=10, arr[1]=20, arr[2]=30.
Step 2: Understand the printf statement
printf prints the value at arr[1], which is 20.
Final Answer:
20 -> Option C
Quick Check:
arr[1] = 20 [OK]
Hint: Remember array index 1 is second element [OK]
Common Mistakes:
Confusing index 1 with index 0
Expecting output 10 or 30 incorrectly
Thinking code causes compilation error
4. Find the error in this code snippet:
int arr[4];
arr[4] = 10;
medium
A. Array index out of bounds
B. Missing semicolon
C. Wrong data type
D. No error
Solution
Step 1: Check array size and indexing
The array arr has size 4, so valid indexes are 0,1,2,3 only.
Step 2: Identify the invalid index usage
arr[4] accesses the fifth element, which is outside the array bounds causing undefined behavior.
Final Answer:
Array index out of bounds -> Option A
Quick Check:
Index must be less than array size [OK]
Hint: Index must be less than array size [OK]
Common Mistakes:
Using index equal to array size
Ignoring zero-based indexing
Assuming no error for out-of-bounds access
5. Given the array declaration int nums[5] = {1, 2, 3, 4, 5};, which code snippet correctly calculates the sum of all elements?
hard
A. int sum = 0;
for(int i = 0; i <= 5; i++) sum += nums[i];
B. int sum = 0;
for(int i = 0; i < 5; i++) sum += nums[i];
C. int sum = 0;
for(int i = 1; i < 5; i++) sum += nums[i];
D. int sum = 0;
for(int i = 0; i < 4; i++) sum += nums[i];
Solution
Step 1: Understand array size and valid indexes
The array nums has 5 elements with indexes 0 to 4.
Step 2: Analyze each loop condition
int sum = 0;
for(int i = 0; i <= 5; i++) sum += nums[i]; uses i <= 5 which accesses nums[5] (out of bounds). int sum = 0;
for(int i = 0; i < 5; i++) sum += nums[i]; uses i < 5 which correctly iterates from 0 to 4. int sum = 0;
for(int i = 1; i < 5; i++) sum += nums[i]; starts at 1, missing nums[0]. int sum = 0;
for(int i = 0; i < 4; i++) sum += nums[i]; stops at 4, missing nums[4].
Final Answer:
int sum = 0;
for(int i = 0; i < 5; i++) sum += nums[i]; -> Option B
Quick Check:
Loop from 0 to less than size sums all elements [OK]
Hint: Loop from 0 to less than array size for all elements [OK]
Common Mistakes:
Using <= size causing out-of-bounds access
Starting loop at 1 missing first element
Stopping loop early missing last element
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.