Bird
0
0
DSA Cprogramming~30 mins

Array Insertion at Beginning in DSA C - Build from Scratch

Choose your learning style9 modes available
Array Insertion at Beginning
📖 Scenario: You are managing a list of daily temperatures recorded in the morning. You want to add a new temperature reading at the start of the list to keep the latest data first.
🎯 Goal: Build a program that inserts a new temperature value at the beginning of an existing array of temperatures.
📋 What You'll Learn
Create an array with exact initial temperature values
Create a variable for the new temperature to insert
Shift existing array elements to the right to make space
Insert the new temperature at the beginning
Print the updated array elements separated by spaces
💡 Why This Matters
🌍 Real World
In real life, adding new data at the start of a list is common in logs, recent activity feeds, or sensor data streams.
💼 Career
Understanding array manipulation is fundamental for programming jobs, especially when working with low-level data or performance-critical applications.
Progress0 / 4 steps
1
Create the initial array of temperatures
Create an integer array called temps with these exact values: 23, 25, 22, 20, 24. Also create an integer variable size and set it to 5.
DSA C
Hint

Use int temps[10] to allow space for new elements.

2
Create the new temperature variable
Create an integer variable called new_temp and set it to 26.
DSA C
Hint

Just declare and assign the new temperature value.

3
Shift elements and insert new temperature
Use a for loop with variable i starting from size - 1 down to 0 to shift elements in temps one position to the right. Then insert new_temp at index 0. Finally, increase size by 1.
DSA C
Hint

Loop backwards to avoid overwriting data when shifting.

4
Print the updated array
Use a for loop with variable i from 0 to size - 1 to print each element of temps separated by spaces.
DSA C
Hint

Use printf("%d ", temps[i]) inside the loop to print each element.