📖 Scenario: You are working with a list of daily temperatures recorded over a week. You want to perform some common operations on this list to understand the data better.
🎯 Goal: Build a simple C++ program that stores daily temperatures in an array, calculates the average temperature, finds the highest temperature, and prints the results.
📋 What You'll Learn
Create an array with exactly 7 integer temperature values
Create a variable to hold the sum of temperatures
Use a for loop with index variable i to iterate over the array
Calculate the average temperature as a float
Find the highest temperature using a variable maxTemp
Print the average and highest temperature exactly as shown
💡 Why This Matters
🌍 Real World
Arrays are used to store collections of data like temperatures, sales numbers, or scores. Knowing how to process arrays helps analyze and summarize data.
💼 Career
Many programming jobs require working with arrays and loops to handle lists of information efficiently.
Progress0 / 4 steps
1
Create the temperature array
Create an integer array called temperatures with these exact values: 23, 25, 20, 22, 24, 26, 21
C++
Hint
Use the syntax int arrayName[size] = {values}; to create the array.
2
Set up sum and max variables
Create an integer variable sum and set it to 0. Create an integer variable maxTemp and set it to the first element of temperatures.
C++
Hint
Initialize sum to 0 and maxTemp to the first temperature value.
3
Calculate sum and find max temperature
Use a for loop with index variable i from 0 to 6 to add each temperature to sum. Inside the loop, update maxTemp if temperatures[i] is greater than maxTemp.
C++
Hint
Loop from 0 to 6, add each temperature to sum, and update maxTemp if needed.
4
Calculate average and print results
Create a float variable average and set it to sum divided by 7.0. Then print the average temperature with the text "Average temperature: " and the highest temperature with the text "Highest temperature: ".
C++
Hint
Divide sum by 7.0 to get a float average. Use std::cout to print the results.
Practice
(1/5)
1. What does the following C++ code do? int arr[5] = {1, 2, 3, 4, 5};
easy
A. Creates an array of 5 integers with values 1 to 5
B. Creates a vector of 5 integers with values 1 to 5
C. Creates a pointer to an integer array
D. Declares an uninitialized array of size 5
Solution
Step 1: Understand array declaration
The code declares an integer array named arr with size 5.
Step 2: Check initialization
The array is initialized with values 1, 2, 3, 4, 5 in order.
Final Answer:
Creates an array of 5 integers with values 1 to 5 -> Option A
Quick Check:
Array declaration with initialization = Creates array with values [OK]
Hint: Array size and values in braces means initialized array [OK]
Common Mistakes:
Confusing array with vector
Thinking it's a pointer declaration
Assuming array is uninitialized
2. Which of the following is the correct syntax to access the third element of an array named arr in C++?
easy
A. arr[3];
B. arr(3);
C. arr{3};
D. arr[2];
Solution
Step 1: Recall zero-based indexing
Arrays in C++ start at index 0, so the third element is at index 2.
Step 2: Identify correct syntax for access
Array elements are accessed using square brackets, so arr[2] is correct.
Final Answer:
arr[2]; -> Option D
Quick Check:
Third element index = 2, use square brackets [OK]
Hint: Remember arrays start at 0, so third element is index 2 [OK]
Common Mistakes:
Using parentheses instead of brackets
Using 1-based indexing
Using curly braces for access
3. What is the output of this C++ code?
int arr[4] = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
std::cout << sum;
medium
A. 100
B. 90
C. 40
D. 10
Solution
Step 1: Identify array elements and loop
The array has elements 10, 20, 30, 40 and the loop runs from i=0 to 3.
Step 2: Calculate sum of elements
sum = 10 + 20 + 30 + 40 = 100
Final Answer:
100 -> Option A
Quick Check:
Sum of all elements = 100 [OK]
Hint: Add all elements in loop to get sum [OK]
Common Mistakes:
Stopping loop early
Using wrong indices
Printing sum before loop
4. Find the error in this C++ code that tries to print all elements of an array:
int arr[3] = {5, 10, 15};
for(int i = 1; i <= 3; i++) {
std::cout << arr[i] << " ";
}
medium
A. Loop index starts at 1 instead of 0
B. Array size is incorrect
C. Using <= causes out-of-bounds access
D. Missing semicolon after cout statement
Solution
Step 1: Analyze loop indices
The loop runs i from 1 to 3 inclusive, so i=3 is included.
Step 2: Check array bounds
Array indices are 0,1,2. Accessing arr[3] is out-of-bounds and causes error.
Final Answer:
Using <= causes out-of-bounds access -> Option C
Quick Check:
Loop must use i < size, not <= [OK]
Hint: Use i < size, not i <= size in loops [OK]
Common Mistakes:
Starting loop at 1 instead of 0 (not error but unusual)
Ignoring out-of-bounds access
Assuming array size is wrong
5. You have an array int arr[6] = {2, 4, 6, 8, 10, 12};. How do you update the element with value 8 to 9 using a loop?