Arrays
Arrays
#include <stdio.h>
int main() {
int a[] = {25, 50, 75, 100};
printf("%d\n", a[0]);
printf("%d\n", a[1]);
printf("%d\n", a[2]);
printf("%d\n", a[3]);
printf("%d\n", a[4]);
return 0;
}
Output:
25
50
75
100
45
#include <stdio.h>
int main()
{
int a[] = {25, 50, 75, 100, 45};
a[3] = 60;
printf("%d\n", a[3]);
return 0;
}
Output: 60
Traversing an Array in C
To traverse an array, for loop is used.
#include <stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=90;//initialization of array
marks[1]=80;
marks[2]=70;
marks[3]=95;
marks[4]=85;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
Output:
90
80
70
95
85
// Program to take values of the array from the user and print the array
#include <stdio.h>
int main()
{
int a[5];
printf("Enter the values of an integer array:\n ");
Advantages of an Array in C
Efficient memory usage: Arrays in C use contiguous memory
locations to store the elements, thus making it efficient to allocate and
access data.
Easy access to elements: Elements in an array can be accessed using
their index, making it easy to retrieve specific data from the array.
Better performance: Accessing elements in an array using an index is
faster than using other data structures like linked lists or trees. This is
because the index provides direct access to the memory location where
the element is stored.
Flexibility: Arrays in C can be used to store different types of data,
including integers, characters, and strings.
Easy to implement algorithms: Many algorithms in computer science
use arrays, making it easy to implement these algorithms in C.
Compatible with other data structures: Arrays can be used in
conjunction with other data structures in C, such as stacks and queues, to
implement complex data structures and algorithms.
Easy to pass to functions: Arrays can be easily passed as arguments
to functions in C, making it easy to manipulate large amounts of data
efficiently.
Disadvantages of an Array in C
Fixed-size: Arrays in C have a fixed size determined at the time of
declaration. There is no dynamic increase in the size of an array.
Memory allocation: Arrays in C are allocated in contiguous memory
blocks. If the array is very large, there may not be enough contiguous
memory available for allocation.
No bounds checking: C does not perform any bounds checking on
arrays, so it is possible to access memory outside of the bounds of the
array. This can lead to segmentation faults or other memory-related
errors.
Limited flexibility: Arrays in C have limited flexibility due to fixed size
and dimensions. This makes it difficult to implement certain algorithms
and data structures that require dynamic resizing and manipulation.
Inefficient for insertion and deletion: Inserting or deleting elements
from an array in C can be inefficient, as it requires shifting all the
elements after the insertion or deletion point to make room or fill in the
gap. This can be time-consuming, especially for large arrays.
Not suitable for non-numeric data: While arrays in C are well-suited
for storing numeric data, they aren’t ideal for storing non-numeric data,
such as strings or complex data structures. This is because C does not
provide built-in support for these types of data structures.
#include <stdio.h>
void modifyArray(int arr[], int size) // Pass the array and its size
{
for (int i = 0; i < size; i++) {
arr[i] *= 4; // Multiply each element by 4 // MODIFYING THE ARRAY
ELEMENTS
}
}
int main() {
int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(my_array) / sizeof(my_array[0]);
return 0;
}
✅ Final Output:
Original array: 1 2 3 4 5 6 7 8 9 10
Modified array: 4 8 12 16 20 24 28 32 36 40
🔍 Tracing Step-by-Step
1. 🧠 Memory before main runs:
Nothing is allocated yet.
2. 🏁 Inside main():
➤ int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Index my_array[i]
0 1
1 2
2 3
3 4
Index my_array[i]
4 5
5 6
6 7
7 8
8 9
9 10
These values are stored in memory in a contiguous block.