📖 Scenario: You are managing a list of daily temperatures recorded in an array. Sometimes, a new temperature reading arrives that needs to be inserted exactly in the middle of the current list to keep the order balanced.
🎯 Goal: Build a program that inserts a new temperature value into the middle of an existing array and then prints the updated array.
📋 What You'll Learn
Create an integer array called temps with exactly 5 values: 23, 25, 22, 24, 26
Create an integer variable called new_temp and set it to 27
Write code to insert new_temp into the middle index of temps, shifting elements to the right
Print the updated array elements separated by spaces
💡 Why This Matters
🌍 Real World
In real life, inserting data into the middle of a list is common when managing ordered records like temperature logs, event schedules, or priority queues.
💼 Career
Understanding how to manipulate arrays and insert elements is a fundamental skill 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 temps with these exact values: 23, 25, 22, 24, 26
DSA C
Hint
Use int temps[6] = {23, 25, 22, 24, 26}; to create the array with space for one extra element.
2
Add the new temperature variable
Create an integer variable called new_temp and set it to 27
DSA C
Hint
Declare int new_temp = 27; inside main().
3
Insert the new temperature at the middle index
Write code to insert new_temp into the middle index 2 of temps, shifting elements from index 2 to the right
DSA C
Hint
Shift elements from the end to index 3 to make space at index 2, then assign new_temp to temps[2].
4
Print the updated array
Use a for loop with variable i from 0 to 5 to print each element of temps separated by spaces
DSA C
Hint
Use printf inside a loop to print each element with a space, then print a newline.
Practice
(1/5)
1. What is the correct middle index to insert an element in an array of size 6?
easy
A. 5
B. 2
C. 4
D. 3
Solution
Step 1: Calculate middle index for even-sized array
For an array of size 6, middle index is size/2 = 6/2 = 3.
Step 2: Confirm insertion position
Inserting at index 3 places the element exactly in the middle.
Final Answer:
3 -> Option D
Quick Check:
Middle index = size/2 = 3 [OK]
Hint: Middle index = array size divided by 2 [OK]
Common Mistakes:
Using size-1 as middle index
Using size/2 - 1 for even size
Confusing zero-based indexing
2. Which of the following C code snippets correctly inserts an element at the middle index of an array arr of size n?
easy
A. int mid = n / 2; for(int i = 0; i < mid; i++) arr[i] = arr[i+1]; arr[mid] = x;
B. int mid = n / 2; for(int i = n; i > mid; i--) arr[i] = arr[i-1]; arr[mid] = x;
C. int mid = n / 2; arr[mid] = x;
D. int mid = n / 2; for(int i = mid; i < n; i++) arr[i] = arr[i+1]; arr[mid] = x;
Solution
Step 1: Identify correct shifting direction
To insert at middle, elements from middle to end must shift right to make space.
Step 2: Check code correctness
int mid = n / 2; for(int i = n; i > mid; i--) arr[i] = arr[i-1]; arr[mid] = x; shifts elements right from end to middle, then inserts x at mid. Others shift incorrectly or overwrite.
Final Answer:
int mid = n / 2; for(int i = n; i > mid; i--) arr[i] = arr[i-1]; arr[mid] = x; -> Option B
Quick Check:
Shift right from end to mid, then insert [OK]
Hint: Shift elements right from end to middle before insertion [OK]
Common Mistakes:
Shifting elements left instead of right
Not shifting elements before insertion
Overwriting elements without shifting
3. What will be the content of the array after inserting 99 at the middle index of arr = {10, 20, 30, 40, 50} using the correct insertion method?
medium
A. [10, 20, 30, 40, 99, 50]
B. [10, 20, 99, 30, 40, 50]
C. [10, 20, 30, 99, 40, 50]
D. [10, 20, 30, 40, 50, 99]
Solution
Step 1: Calculate middle index
Array size is 5, middle index = 5/2 = 2 (integer division).
Step 2: Shift elements right and insert 99
Shift elements from index 4 to 2 right by one position. Insert 99 at index 2.
Final Answer:
[10, 20, 99, 30, 40, 50] -> Option C
Quick Check:
Insert at index 2 shifts elements right [OK]
Hint: Middle index = size/2; shift right, then insert [OK]
Common Mistakes:
Inserting at wrong index
Not shifting elements before insertion
Overwriting elements
4. Identify the error in the following code snippet that inserts an element x at the middle of array arr of size n:
int mid = n / 2;
for(int i = mid; i < n; i++) {
arr[i+1] = arr[i];
}
arr[mid] = x;
medium
A. Loop should run backwards from n to mid
B. Index mid is incorrect for insertion
C. No need to shift elements before insertion
D. Array size n should be incremented before insertion
Solution
Step 1: Analyze loop direction
The loop shifts elements from mid to n forward, but does so from low to high index, causing overwrite.
Step 2: Correct loop direction
Loop must run backwards from n to mid to shift elements right safely without overwriting.
Final Answer:
Loop should run backwards from n to mid -> Option A
Quick Check:
Shift right must be done backwards [OK]
Hint: Shift elements right by looping backwards from end [OK]
Common Mistakes:
Looping forward causing data overwrite
Not updating array size after insertion
Incorrect middle index calculation
5. Given an array arr = {5, 10, 15, 20, 25, 30} of size 6, write the final array after inserting 99 at the middle index and then inserting 77 at the new middle index of the updated array.
hard
A. [5, 10, 15, 77, 99, 20, 25, 30]
B. [5, 10, 77, 99, 15, 20, 25, 30]
C. [5, 10, 99, 15, 77, 20, 25, 30]
D. [5, 10, 15, 99, 77, 20, 25, 30]
Solution
Step 1: Insert 99 at middle of original array
Original size = 6, middle index = 3. Insert 99 at index 3 shifting elements right: [5, 10, 15, 99, 20, 25, 30]
Step 2: Insert 77 at middle of updated array
New size = 7, middle index = 7/2 = 3. Insert 77 at index 3 shifting elements right: [5, 10, 15, 77, 99, 20, 25, 30]
Final Answer:
[5, 10, 15, 77, 99, 20, 25, 30] -> Option A
Quick Check:
Insert twice at middle recalculating index each time [OK]
Hint: Recalculate middle after each insertion before next insert [OK]
Common Mistakes:
Not recalculating middle index after first insertion
Overwriting elements without shifting
Inserting at wrong index second time
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.