📖 Scenario: You are managing a list of daily temperatures recorded in a week. Sometimes, you need to remove the temperature recorded in the middle of the week to analyze the data better.
🎯 Goal: Build a program that deletes the middle temperature value from an array of temperatures.
📋 What You'll Learn
Create an integer array called temperatures with exactly 7 values
Create an integer variable called middleIndex to find the middle position
Remove the element at middleIndex from the temperatures array
Print the updated array elements separated by spaces
💡 Why This Matters
🌍 Real World
Deleting an element from the middle of a list is common when cleaning or updating data, such as removing an outlier temperature reading.
💼 Career
Understanding array manipulation is essential for software developers working with data processing, algorithms, and system programming.
Progress0 / 4 steps
1
Create the temperatures array
Create an integer array called temperatures with these exact values: 23, 25, 22, 20, 24, 26, 21
DSA C#
Hint
Use int[] temperatures = { ... }; to create the array with the exact values.
2
Find the middle index
Create an integer variable called middleIndex and set it to the middle index of the temperatures array using its length
DSA C#
Hint
Use temperatures.Length / 2 to find the middle index of the array.
3
Delete the middle element
Create a new integer array called updatedTemperatures that contains all elements from temperatures except the one at middleIndex
DSA C#
Hint
Use a for loop to copy all elements except the one at middleIndex into updatedTemperatures.
4
Print the updated array
Print all elements of updatedTemperatures separated by spaces on one line
DSA C#
Hint
Use a for loop and Console.Write to print each element followed by a space.
Practice
(1/5)
1. What happens when you delete the middle element from an array in C#?
easy
A. The element is removed and all elements after it shift left by one position.
B. The array size automatically decreases by one without shifting elements.
C. The middle element is replaced by zero but array size stays the same.
D. The array throws an error because arrays have fixed size.
Solution
Step 1: Understand array behavior on deletion
In C#, arrays have fixed size, so to delete an element, we shift elements after it left to overwrite it.
Step 2: Effect on array elements
After shifting, the last element is duplicated or ignored logically, but the middle element is effectively removed from the sequence.
Final Answer:
The element is removed and all elements after it shift left by one position. -> Option A
Quick Check:
Deletion shifts elements left [OK]
Hint: Deleting means shifting elements left to fill the gap [OK]
Common Mistakes:
Thinking array size changes automatically
Assuming deletion replaces element with zero
Believing arrays throw error on deletion
2. Which of the following C# code snippets correctly deletes the middle element from an integer array arr of odd length?
easy
A. int mid = arr.Length / 2; arr[mid] = 0;
B. int mid = arr.Length / 2; for(int i = mid; i < arr.Length - 1; i++) arr[i] = arr[i + 1];
C. int mid = arr.Length / 2; arr.RemoveAt(mid);
D. int mid = arr.Length / 2; Array.Delete(arr, mid);
Solution
Step 1: Identify valid deletion method in arrays
Arrays in C# do not have RemoveAt or Delete methods; we must shift elements manually.
Step 2: Check code correctness
int mid = arr.Length / 2; for(int i = mid; i < arr.Length - 1; i++) arr[i] = arr[i + 1]; calculates middle index and shifts elements left from middle to end-1, correctly deleting the middle element.
Final Answer:
int mid = arr.Length / 2; for(int i = mid; i < arr.Length - 1; i++) arr[i] = arr[i + 1]; -> Option B
Quick Check:
Manual shift after mid index [OK]
Hint: Use loop to shift elements left from middle index [OK]
Common Mistakes:
Using non-existent array methods like RemoveAt
Just setting middle element to zero without shifting
Trying to delete without shifting elements
3. What is the output of the following C# code?
int[] arr = {10, 20, 30, 40, 50};
int mid = arr.Length / 2;
for(int i = mid; i < arr.Length - 1; i++)
arr[i] = arr[i + 1];
for(int i = 0; i < arr.Length - 1; i++)
Console.Write(arr[i] + " ");
Loop condition causes index out of range error. -> Option D
Quick Check:
Loop index out of bounds [OK]
Hint: Loop must stop before last index to avoid out of range [OK]
Common Mistakes:
Using <= instead of < in loop condition
Ignoring array bounds when accessing i+1
Misunderstanding middle index for even length
5. You have an array arr = {5, 10, 15, 20, 25, 30, 35}. Write the steps to delete the middle element and print the resulting array with correct size handling.
hard
A. Create a list from arr; remove element at mid; convert back to array; print.
B. Calculate mid = arr.Length / 2; shift elements left from mid; print first arr.Length - 1 elements.
C. Use Array.RemoveAt(mid); print arr.
D. Set arr[mid] = 0; print arr as is.
Solution
Step 1: Convert array to list for flexible size
Arrays have fixed size, so convert to List<int> to remove element easily.
Step 2: Remove middle element and convert back
Remove element at mid index, then convert list back to array for printing.
Step 3: Print resulting array
Print elements of new array which has one less element.
Final Answer:
Create a list from arr; remove element at mid; convert back to array; print. -> Option A
Quick Check:
Use List for deletion, then convert back [OK]
Hint: Use List to delete middle element easily, then convert back [OK]
Common Mistakes:
Trying to resize arrays directly
Only shifting elements without resizing
Using non-existent Array.RemoveAt method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.