📖 Scenario: You are managing a list of daily temperatures recorded in a small weather station. Sometimes, a new temperature reading arrives late and needs to be inserted exactly in the middle of the existing list to keep the order balanced.
🎯 Goal: Build a program that inserts a new temperature value exactly at the middle index of an existing array of temperatures.
📋 What You'll Learn
Create an integer array called temperatures with exactly 5 values: 23, 25, 22, 24, 26
Create an integer variable called new_temp and set it to 27
Insert new_temp into the temperatures array exactly at the middle index (index 2)
Print the updated array elements separated by spaces
💡 Why This Matters
🌍 Real World
Inserting data in the middle of arrays is common in real-time data processing, like updating sensor readings or managing ordered lists.
💼 Career
Understanding array manipulation is fundamental for software developers working with low-level data structures, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called temperatures with these exact values: 23, 25, 22, 24, 26
DSA C++
Hint
Use a fixed size array with 6 elements to allow space for insertion later.
2
Add the new temperature variable
Create an integer variable called new_temp and set it to 27
DSA C++
Hint
Just declare a simple integer variable for the new temperature.
3
Insert the new temperature at the middle index
Shift the elements from index 2 to the right by one position in the temperatures array, then insert new_temp at index 2
DSA C++
Hint
Start shifting from the last element to avoid overwriting values.
4
Print the updated array
Use a for loop with variable i from 0 to 5 to print each element of temperatures separated by spaces
DSA C++
Hint
Print all 6 elements with a space after each number.
Practice
(1/5)
1. What does inserting an element at the middle index of an array mean?
easy
A. Adding a new element exactly in the center, shifting others right
B. Replacing the middle element with a new one
C. Adding a new element at the end of the array
D. Removing the middle element from the array
Solution
Step 1: Understand middle index insertion
Inserting at the middle means placing a new element at the center position, pushing existing elements right.
Step 2: Differentiate from other operations
Replacing or removing elements is not insertion; adding at the end is not middle insertion.
Final Answer:
Adding a new element exactly in the center, shifting others right -> Option A
Quick Check:
Middle insertion means insert and shift right [OK]
Hint: Middle insertion shifts elements right from center [OK]
Common Mistakes:
Thinking insertion replaces the middle element
Confusing insertion with appending at the end
Assuming middle means removing element
2. Which of the following C++ code snippets correctly inserts an element `x` at the middle index of array `arr` of size `n`?
easy
A. arr[n] = x; for(int i = n/2; i < n; i++) arr[i] = arr[i+1];
B. for(int i = 0; i < n/2; i++) arr[i] = arr[i+1]; arr[n/2] = x;
C. arr[n/2] = x; for(int i = n/2; i < n; i++) arr[i+1] = arr[i];
D. for(int i = n; i > n/2; i--) arr[i] = arr[i-1]; arr[n/2] = x;
Solution
Step 1: Shift elements right from middle index
Loop from end (n) down to middle (n/2 + 1) shifting elements right to make space.
Step 2: Insert element at middle index
Assign new element x at index n/2 after shifting.
Final Answer:
for(int i = n; i > n/2; i--) arr[i] = arr[i-1]; arr[n/2] = x; -> Option D
Quick Check:
Shift right then insert at middle [OK]
Hint: Shift elements right from end to middle before insert [OK]
Common Mistakes:
Shifting elements left instead of right
Inserting before shifting elements
Incorrect loop direction causing overwrite
3. What is the output array after inserting 99 at the middle index of arr = {10, 20, 30, 40, 50} using the correct insertion method?