📖 Scenario: You are managing a list of daily temperatures recorded in a weather app. Each day, you add the new temperature at the end of the list.
🎯 Goal: Build a program that stores temperatures in an array and adds a new temperature at the end.
📋 What You'll Learn
Create an integer array called temperatures with the values 20, 22, 19, 23
Create an integer variable called newTemperature and set it to 25
Create a new array called updatedTemperatures that contains all elements of temperatures plus newTemperature at the end
Print the elements of updatedTemperatures separated by spaces
💡 Why This Matters
🌍 Real World
Adding new data points to a fixed-size array is common in systems that collect sensor data or logs where arrays are used for storage.
💼 Career
Understanding how to manually resize arrays and insert elements is fundamental for programming in languages like C# where arrays have fixed size, and it builds a foundation for learning dynamic data structures.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called temperatures with these exact values: 20, 22, 19, 23
DSA C#
Hint
Use int[] temperatures = {20, 22, 19, 23}; to create the array.
2
Add the new temperature variable
Create an integer variable called newTemperature and set it to 25
DSA C#
Hint
Use int newTemperature = 25; to create the variable.
3
Create the updated array with the new temperature at the end
Create a new integer array called updatedTemperatures that contains all elements of temperatures plus newTemperature at the end
DSA C#
Hint
Create a new array with size one more than temperatures. Copy all elements, then add newTemperature at the last index.
4
Print the updated array
Print all elements of updatedTemperatures separated by spaces using a for loop
DSA C#
Hint
Use a for loop to print each element followed by a space.
Practice
(1/5)
1. What happens when you insert an element at the end of a fixed-size array in C# without resizing?
easy
A. An error occurs because the array size is fixed
B. The array automatically resizes to fit the new element
C. The element replaces the last element if the array is full
D. The element is added at the first position
Solution
Step 1: Understand fixed-size array behavior
In C#, arrays have a fixed size once created and cannot grow automatically.
Step 2: Inserting at the end without resizing
Inserting at the end requires accessing index arr.Length, which is out of bounds and throws IndexOutOfRangeException.
Final Answer:
An error occurs because the array size is fixed -> Option A
Quick Check:
Fixed-size array insert at end causes error [OK]
Hint: Fixed arrays can't grow; insert at end causes error [OK]
Common Mistakes:
Assuming array resizes automatically
Thinking insertion shifts elements
Believing insertion adds new slot
2. Which of the following is the correct syntax to insert an element at the end of a List<int> named numbers in C#?
easy
A. numbers.Push(5);
B. numbers.InsertEnd(5);
C. numbers.Add(5);
D. numbers.Append(5);
Solution
Step 1: Identify List insertion method
In C#, List<T> uses the Add() method to insert elements at the end.
Step 2: Check other options
InsertEnd, Push, and Append are not valid List<T> methods in C#.
Final Answer:
numbers.Add(5); -> Option C
Quick Check:
List Add method inserts at end [OK]
Hint: Use Add() to insert at end of List [OK]
Common Mistakes:
Using non-existent methods like InsertEnd or Push
Confusing List with Stack methods
Trying Append which is for strings
3. What will be the output of the following C# code?