📖 Scenario: You are helping a small shop owner keep track of daily sales for different products over a week. The sales data is stored in a table where each row represents a product and each column represents a day of the week.
🎯 Goal: Create a program that stores sales data in a two-dimensional array, calculates the total sales for each product, and then prints the totals.
📋 What You'll Learn
Create a two-dimensional array called sales with 3 rows and 7 columns to store sales numbers.
Create an integer variable called total to hold the sum of sales for each product.
Use a for loop with variable i to go through each product (row).
Use a nested for loop with variable j to go through each day (column) for the current product.
Calculate the total sales for each product by adding the daily sales.
Print the total sales for each product using printf.
💡 Why This Matters
🌍 Real World
Two-dimensional arrays are useful for storing tables of data like sales, schedules, or pixel colors in images.
💼 Career
Understanding how to work with two-dimensional arrays is important for jobs involving data analysis, software development, and any task that requires managing grid-like data.
Progress0 / 4 steps
1
Create the sales data array
Create a two-dimensional integer array called sales with 3 rows and 7 columns. Initialize it with these exact values: first row {10, 12, 11, 14, 13, 15, 16}, second row {8, 9, 7, 10, 11, 12, 13}, third row {5, 6, 5, 7, 8, 6, 7}.
C
Hint
Use curly braces to group each row's values inside the array initialization.
2
Create a variable for total sales
Create an integer variable called total and set it to 0. This will hold the sum of sales for each product.
C
Hint
Remember to specify the type int and initialize total to zero.
3
Calculate total sales for each product
Use a for loop with variable i from 0 to 2 to go through each product (row). Inside it, set total to 0. Then use a nested for loop with variable j from 0 to 6 to go through each day (column). Add sales[i][j] to total inside the inner loop.
C
Hint
Reset total to zero for each product before adding daily sales.
4
Print total sales for each product
Inside the outer for loop, after the inner loop, use printf to print the total sales for product i. Use this exact format: Product %d total sales: %d\n, where the first %d is i + 1 and the second %d is total.
C
Hint
Use printf("Product %d total sales: %d\n", i + 1, total); to print the results.
Practice
(1/5)
1. What is a two-dimensional array in C?
easy
A. An array of arrays, storing data in rows and columns
B. A single list of numbers stored in a line
C. A function that returns multiple values
D. A variable that holds only one value
Solution
Step 1: Understand array structure
A two-dimensional array stores data in a grid format with rows and columns.
Step 2: Compare options
Only An array of arrays, storing data in rows and columns describes this structure as an array of arrays.
Final Answer:
An array of arrays, storing data in rows and columns -> Option A
Quick Check:
2D array = array of arrays [OK]
Hint: Think of a table with rows and columns [OK]
Common Mistakes:
Confusing 2D arrays with single arrays
Thinking 2D arrays are functions
Assuming 2D arrays hold only one value
2. Which of the following is the correct way to declare a 3x3 integer two-dimensional array in C?
easy
A. int arr(3,3);
B. int arr[3][3];
C. int arr[3,3];
D. int arr{3}{3};
Solution
Step 1: Recall C array declaration syntax
In C, arrays use square brackets with sizes inside, like arr[rows][columns].
Step 2: Check each option
Only int arr[3][3]; uses correct syntax: int arr[3][3];
Final Answer:
int arr[3][3]; -> Option B
Quick Check:
Use square brackets for array sizes [OK]
Hint: Use square brackets for array sizes, not parentheses [OK]
Common Mistakes:
Using parentheses instead of square brackets
Using commas inside brackets
Using curly braces for declaration
3. What will be the output of the following C code?
int arr[2][2] = {{1, 2}, {3, 4}};
printf("%d", arr[1][0]);
medium
A. 3
B. 1
C. 2
D. 4
Solution
Step 1: Understand array initialization
The array arr has two rows: first row {1, 2}, second row {3, 4}.
Step 2: Access element arr[1][0]
Row index 1 is second row, column index 0 is first element in that row, which is 3.
Final Answer:
3 -> Option A
Quick Check:
arr[1][0] = 3 [OK]
Hint: Remember indexing starts at 0 for rows and columns [OK]
Common Mistakes:
Confusing row and column indexes
Starting index at 1 instead of 0
Mixing up elements in initialization
4. Identify the error in the following C code snippet:
int matrix[2][3];
matrix[2][1] = 5;
medium
A. Wrong data type for matrix
B. No error, code is correct
C. Syntax error due to missing semicolon
D. Index out of bounds error on matrix[2][1]
Solution
Step 1: Check array size and valid indexes
matrix is declared with 2 rows (0 and 1) and 3 columns (0 to 2).
Step 2: Analyze matrix[2][1]
Row index 2 is invalid because valid rows are 0 and 1 only, causing out of bounds error.
Final Answer:
Index out of bounds error on matrix[2][1] -> Option D
Quick Check:
Row index must be less than 2 [OK]
Hint: Array indexes start at 0 and go to size-1 [OK]
Common Mistakes:
Using index equal to array size
Ignoring zero-based indexing
Assuming no error for out-of-range access
5. Given the 2D array declaration int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};, which code snippet correctly calculates the sum of all elements?
hard
A. int sum=0; for(int i=1;i<3;i++) for(int j=1;j<3;j++) sum += grid[i][j]; printf("%d", sum);
B. int sum=0; for(int i=0;i<=3;i++) for(int j=0;j<=3;j++) sum += grid[i][j]; printf("%d", sum);
C. int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum += grid[i][j]; printf("%d", sum);
D. int sum=0; for(int i=0;i<3;i++) sum += grid[i]; printf("%d", sum);
Solution
Step 1: Understand array size and loops
grid has 3 rows and 3 columns, so indexes go from 0 to 2.
Step 2: Check loop bounds and summation
int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum += grid[i][j]; printf("%d", sum); uses correct loop bounds (i<3, j<3) and sums all elements correctly.
Step 3: Analyze other options
int sum=0; for(int i=0;i<=3;i++) for(int j=0;j<=3;j++) sum += grid[i][j]; printf("%d", sum); uses <=3 causing out-of-bounds access; the option starting loops at 1 misses first row and column elements; the option trying sum += grid[i] is invalid for 2D arrays.
Final Answer:
int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum += grid[i][j]; printf("%d", sum); -> Option C
Quick Check:
Use correct loop bounds and access grid[i][j] [OK]
Hint: Loop from 0 to size-1 for both rows and columns [OK]
Common Mistakes:
Using <= instead of < in loops
Starting loops at 1 instead of 0
Trying to add entire row instead of elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.