📖 Scenario: You are managing a list of daily temperatures recorded over a week. Sometimes, you need to check the temperature of a specific day or update it if there was a mistake in recording.
🎯 Goal: Build a simple program that creates an array of temperatures, accesses a temperature at a given index, updates the temperature at another index, and then prints the updated array.
📋 What You'll Learn
Create an integer array called temperatures with exactly 7 elements: 23, 25, 22, 20, 24, 26, 21
Create an integer variable called index_to_access and set it to 3
Create an integer variable called index_to_update and set it to 5
Create an integer variable called new_temperature and set it to 28
Access the temperature at index_to_access and store it in a variable called accessed_temperature
Update the temperature at index_to_update in the temperatures array to new_temperature
Print the accessed_temperature
Print the updated temperatures array elements separated by spaces
💡 Why This Matters
🌍 Real World
Arrays are used to store collections of data like daily temperatures, stock prices, or sensor readings where you need quick access and updates by position.
💼 Career
Understanding array access and update is fundamental for software development, data processing, and working with any indexed data structures.
Progress0 / 4 steps
1
Create the temperatures array
Create an integer array called temperatures with exactly 7 elements: 23, 25, 22, 20, 24, 26, 21
DSA C++
Hint
Use C++ array syntax with curly braces to initialize the array.
2
Create index and new temperature variables
Create an integer variable called index_to_access and set it to 3. Create an integer variable called index_to_update and set it to 5. Create an integer variable called new_temperature and set it to 28.
DSA C++
Hint
Declare each variable on its own line with the exact names and values.
3
Access and update the array
Access the temperature at index_to_access in the temperatures array and store it in an integer variable called accessed_temperature. Update the temperature at index_to_update in the temperatures array to new_temperature.
DSA C++
Hint
Use the array name with square brackets and the index variable to access and update elements.
4
Print the accessed temperature and updated array
Print the value of accessed_temperature on one line. Then print all elements of the temperatures array separated by spaces on the next line.
DSA C++
Hint
Use cout to print the accessed temperature and a loop to print the array elements separated by spaces.
Practice
(1/5)
1. What does the expression arr[2] do in an array named arr?
easy
A. Accesses the element at index 2 of the array
B. Accesses the element at index 0 of the array
C. Updates the element at index 2 to zero
D. Deletes the element at index 2
Solution
Step 1: Understand array indexing
Arrays use zero-based indexing, so arr[2] refers to the third element.
Step 2: Identify the operation
The expression arr[2] accesses (reads) the element at index 2 without changing it.
Final Answer:
Accesses the element at index 2 of the array -> Option A
Quick Check:
arr[2] means read element at index 2 [OK]
Hint: Remember arrays start at index 0, so arr[2] is third element [OK]
Common Mistakes:
Thinking arr[2] updates the element
Confusing index 2 with index 0
Assuming arr[2] deletes an element
2. Which of the following is the correct syntax to update the 4th element of an integer array nums to 10 in C++?
easy
A. nums(4) = 10;
B. nums[3] = 10;
C. nums[4] == 10;
D. nums{3} = 10;
Solution
Step 1: Recall zero-based indexing
The 4th element is at index 3 because counting starts from 0.
Step 2: Check correct syntax for assignment
Use square brackets for indexing and single equals sign for assignment: nums[3] = 10;.
Final Answer:
nums[3] = 10; -> Option B
Quick Check:
Use nums[3] = 10; to update 4th element [OK]
Hint: Use square brackets and single = to update array element [OK]
Common Mistakes:
Using parentheses instead of brackets
Using double equals (==) instead of single equals (=)
Using curly braces for indexing
3. What will be the output of the following C++ code?
Hint: Remember arr[3] is 4, so arr[1] becomes 6 [OK]
Common Mistakes:
Confusing arr[1] and arr[3] values
Forgetting to add 2
Printing old value of arr[1]
4. Find the error in the following code snippet that tries to update the 2nd element of array data to 100:
int data[3] = {10, 20, 30};
data(1) = 100;
medium
A. Cannot assign integer to array element
B. Array size is too small
C. Using parentheses instead of square brackets for indexing
D. Missing semicolon after array declaration
Solution
Step 1: Check array indexing syntax
Arrays use square brackets [] for indexing, not parentheses ().
Step 2: Identify the incorrect line
data(1) = 100; is invalid syntax; correct is data[1] = 100;.
Final Answer:
Using parentheses instead of square brackets for indexing -> Option C
Quick Check:
Use square brackets [] to access array elements [OK]
Hint: Use [] for array access, not () [OK]
Common Mistakes:
Using parentheses for indexing
Assuming array size is wrong
Missing semicolon errors
5. Given an array int scores[5] = {10, 20, 30, 40, 50};, which code snippet correctly doubles the value at index 2 and then updates index 4 to be the sum of the new index 2 value and index 0 value?
hard
A. scores[2] *= 2;
scores[4] = scores[4] + scores[0];