📖 Scenario: You are managing a list of daily temperatures recorded in a week. You want to insert a new temperature reading exactly in the middle of the existing list to keep the data organized.
🎯 Goal: Build a program that inserts a new temperature value into the middle of an existing array of temperatures.
📋 What You'll Learn
Create an integer array called temperatures with exactly 5 values: 22, 24, 19, 23, 25
Create an integer variable called newTemperature and set it to 21
Write code to insert newTemperature into the middle index of temperatures
Print the updated array elements separated by spaces
💡 Why This Matters
🌍 Real World
Inserting data into the middle of a list is common when managing ordered records like daily temperatures, stock prices, or event logs.
💼 Career
Understanding array manipulation is fundamental for software developers working with fixed-size data collections and preparing for more advanced data structures.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called temperatures with these exact values: 22, 24, 19, 23, 25
DSA C#
Hint
Use curly braces to list the values inside the array.
2
Add the new temperature variable
Create an integer variable called newTemperature and set it to 21
DSA C#
Hint
Use int newTemperature = 21; to create the variable.
3
Insert the new temperature in the middle
Create a new integer array called updatedTemperatures that has one more element than temperatures. Insert newTemperature exactly in the middle index of temperatures and copy the other elements around it.
DSA C#
Hint
Calculate the middle index using temperatures.Length / 2. Use two loops to copy elements before and after the middle.
4
Print the updated array
Use a for loop with variable i to print all elements of updatedTemperatures separated by spaces on one line.
DSA C#
Hint
Use Console.Write inside the loop to print each element followed by a space.
Practice
(1/5)
1. What happens when you insert an element at the middle index of an array in C#?
easy
A. The array size decreases by one.
B. Elements after the middle index shift right to make space for the new element.
C. The new element replaces the element at the middle index without shifting.
D. The array elements remain unchanged.
Solution
Step 1: Understand array insertion behavior
When inserting at the middle, elements from that index onward must move right to avoid overwriting.
Step 2: Effect on array size and elements
The array size increases by one, and elements after the middle index shift right to make space.
Final Answer:
Elements after the middle index shift right to make space for the new element. -> Option B
Quick Check:
Insertion shifts elements right = A [OK]
Hint: Insertion shifts elements right from middle index [OK]
Common Mistakes:
Thinking the array size decreases
Replacing element without shifting
Assuming no change in elements
2. Which of the following is the correct syntax to insert an element at the middle index of an array in C# (assuming you use a List<int> named 'numbers')?
easy
A. numbers.AddAtMiddle(10);
B. numbers.Add(10, numbers.Count / 2);
C. numbers.InsertAtMiddle(10);
D. numbers.Insert(numbers.Count / 2, 10);
Solution
Step 1: Identify List insertion method
List<T> in C# uses Insert(index, value) to insert at any position.
Step 2: Calculate middle index and insert
Middle index is numbers.Count / 2; so Insert(numbers.Count / 2, 10) inserts 10 at middle.
Final Answer:
numbers.Insert(numbers.Count / 2, 10); -> Option D
Quick Check:
Insert(index, value) syntax = B [OK]
Hint: Use Insert(index, value) with middle index [OK]
Common Mistakes:
Using non-existent methods like AddAtMiddle
Confusing Add and Insert parameters
Wrong method names
3. What will be the output of the following C# code?
A. Error: Cannot insert in fixed-size array; use List<int> and Insert method.
B. No error; code works fine.
C. Error: Index out of range; middle index calculation is wrong.
D. Error: Assignment operator is invalid here.
Solution
Step 1: Understand array size and insertion
Arrays in C# have fixed size; you cannot insert elements to increase size.
Step 2: Fix by using List<int> and Insert method
Use List<int> which supports dynamic insertion with Insert(index, value).
Final Answer:
Error: Cannot insert in fixed-size array; use List<int> and Insert method. -> Option A
Quick Check:
Fixed-size arrays can't insert = A [OK]
Hint: Arrays fixed size; use List for insertion [OK]
Common Mistakes:
Thinking assignment inserts element
Misunderstanding array size limits
Wrong index calculation assumption
5. You have an array of integers with an even length. You want to insert a new element exactly in the middle so that the array size increases by one and elements after the middle shift right. Which approach is best in C#?
hard
A. Use a List<int>, insert at middle index, then convert back to array if needed.
B. Directly assign the new element at middle index in the existing array.
C. Create a new array with size +1, copy elements before middle, insert new element, then copy remaining elements.
D. Use Array.Resize to increase size and then assign at middle index.
Solution
Step 1: Understand array limitations
Arrays have fixed size; direct insertion is not possible without creating a new array.
Step 2: Use List for dynamic insertion
List<int> allows insertion at any index easily; after insertion, convert back to array if needed.
Final Answer:
Use a List<int>, insert at middle index, then convert back to array if needed. -> Option A
Quick Check:
List supports insertion; arrays do not [OK]
Hint: Use List for easy insertion, then convert to array [OK]
Common Mistakes:
Trying to assign directly in fixed array
Using Array.Resize incorrectly
Manually copying arrays without List
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.