📖 Scenario: Imagine you run a small bakery. Every day, you bake several types of bread. You want to keep track of how many loaves of each type you bake.
🎯 Goal: You will create a program that stores the number of loaves baked for different bread types using an array. This will help you see why arrays are useful to hold multiple related values together.
📋 What You'll Learn
Create an array to store the number of loaves baked for 3 types of bread
Use a variable to hold the total number of loaves baked
Use a loop to add up all the loaves from the array
Print the total number of loaves baked
💡 Why This Matters
🌍 Real World
Bakeries, stores, and many businesses need to track counts or measurements for multiple items daily.
💼 Career
Understanding arrays is fundamental for programming jobs that involve data storage, processing lists, or handling collections of information.
Progress0 / 4 steps
1
Create an array with bread loaves counts
Create an array called loaves of type int with length 3. Set the values to 10, 15, and 7 representing loaves baked for three bread types.
Go
Hint
Use the syntax varName := [length]type{values} to create an array with fixed size.
2
Create a variable to hold total loaves
Create an integer variable called total and set it to 0. This will hold the sum of all loaves baked.
Go
Hint
Use total := 0 to create and initialize the variable.
3
Add all loaves using a for loop
Use a for loop with the variable i from 0 to 2 to add each element of loaves to total. Use total += loaves[i] inside the loop.
Go
Hint
Use a classic for loop: for i := 0; i < 3; i++ { total += loaves[i] }
4
Print the total number of loaves baked
Use fmt.Println(total) to print the total number of loaves baked. Remember to import the fmt package at the top.
Go
Hint
Use fmt.Println(total) to show the total on the screen.
Practice
(1/5)
1. Why do we need arrays in Go programming?
easy
A. To store multiple values of the same type together in one variable
B. To store only one value at a time
C. To perform mathematical calculations faster
D. To create functions that run automatically
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many values of the same type in one place, making it easier to manage related data.
Step 2: Compare options with array purpose
Only To store multiple values of the same type together in one variable correctly describes storing multiple values together; others describe unrelated concepts.
Final Answer:
To store multiple values of the same type together in one variable -> Option A
Quick Check:
Arrays store multiple values = A [OK]
Hint: Arrays hold many same-type items in one variable [OK]
Common Mistakes:
Thinking arrays store different types together
Confusing arrays with single variables
Assuming arrays speed up math automatically
2. Which of the following is the correct way to declare an array of 3 integers in Go?
easy
A. var arr [3]int
B. arr := []int{3}
C. var arr int[3]
D. arr = array(3)int
Solution
Step 1: Recall Go array declaration syntax
In Go, arrays are declared with var name [size]type, for example var arr [3]int.
Step 2: Check each option
var arr [3]int matches correct syntax. arr := []int{3} is a slice, not an array. Options A and D are invalid syntax.
Final Answer:
var arr [3]int -> Option A
Quick Check:
Array declaration uses var name [size]type = C [OK]
Hint: Remember: var name [size]type declares an array [OK]