0% found this document useful (0 votes)
4 views

LAB1- Arrays Programming

Arrays in C are a powerful data structure that allows for the storage and manipulation of a collection of elements of the same data type. Basic operations include traversal, insertion, deletion, searching, and updating of elements, with arrays being declared using a specific syntax. While arrays offer efficient memory usage and easy access to elements, they have limitations such as fixed size and lack of bounds checking.

Uploaded by

rudralad2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LAB1- Arrays Programming

Arrays in C are a powerful data structure that allows for the storage and manipulation of a collection of elements of the same data type. Basic operations include traversal, insertion, deletion, searching, and updating of elements, with arrays being declared using a specific syntax. While arrays offer efficient memory usage and easy access to elements, they have limitations such as fixed size and lack of bounds checking.

Uploaded by

rudralad2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Arrays in C Programming

An Array in C programming language is a powerful data structure that allows users to store
and manipulate a collection of elements, all of the same data type in a single variable.
Simply, it is a collection of elements of the same data type.

Arrays are the derived data type in C that can store values of both - fundamental data
types like int, and char; and derived data types like pointers, and structure. The values
get stored at contagious memory locations that can be accessed with their index number.

Basic Array Operations

Following are the basic Array operations.

 Traverse − Print each element in the array one by one.


 Insertion − At the specified index, adds an element.
 Deletion − The element at the specified index is deleted.
 Search − Uses the provided index or the value to search for an element.
 Update − The element at the specified index is updated

Syntax

dataType arrayName[arraySize];

int a[5];
Here, an integer type array a is declared with the size 5. It can store 5 integer values.

Properties of an Array in C
 Declaration: Arrays in C are declared by specifying the data type of the elements and
the number of elements in the array.
 Indexing: Elements in an array are accessed using an index. The index of the first
element in an array is always 0.
 Memory allocation: Arrays in C are allocated contiguous memory locations at the
time of declaration. The amount of memory allocated for an array is equal to the
product of the number of elements and the size of each element.
 Initialization: Arrays in C can be initialized at the time of declaration or later.
 Size: The size of an array is the number of elements in the array. The size of an array
can be calculated using the sizeof() operator.
 Manipulation: Arrays in C can be manipulated using loops, functions, and other
programming constructs.

Types of Arrays in C
There are two types of array in C, which are:
1. Single-dimensional array: It is a collection of elements of the same data type that are
stored in a contiguous block of memory.
2. Multi-dimensional array: It is an array that contains one or more arrays as its
elements. We will see this in the next section multidimensional arrays in C.

There are various ways to do this:

 Initialize at the time of declaration using “{}”.

int a[5] = {1, 2, 3, 4, 5};

 Initialize an array without specifying its size at declaration time.

int a[] = {1, 2, 3, 4, 5};

Though we haven't specified the size, the compiler understands the size as 5 due to
the initialization of 5 elements.

Initialization by using the index of an element

int marks[5];

marks[0]=80; //the index of an array starts with 0.

marks[1]=60;

marks[2]=70;

marks[3]=85;

marks[4]=75;

Accessing Elements of an Array in C

To access the array elements, use the index number of the required element. The array index
starts with 0. The index of the last element is n-1

#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;

Changing the Elements of an array in C

To change the value of a specific element, refer to the index number:

#include <stdio.h>

int main()

int a[] = {25, 50, 75, 100, 45};

a[3] = 60;

printf("%d\n", a[3]);

return 0;

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;

Input and Output Array Elements in C

We can use the scanf() function to take inputs of an array from the user.

Example

// 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 ");

// taking input and storing it in an array

for(int i = 0; i < 5; ++i) {

scanf("%d", &a[i]);

}
printf("Displaying integers: ");

// printing elements of an array

for(int i = 0; i < 5; ++i) {

printf("%d\n", a[i]);

} return 0;

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.

Traverse Array Operations

The elements of an array are traversed during this procedure.


Example
The following program iterates through an array and outputs each element:

#include <stdio.h>
main() {
int Arr[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}

Insertion Array Operations

One or more data elements are added to an array using the insert array operations. An array
can have a new element inserted at any index, including the start, end, or middle, depending
on the need.
In this example, the insertion action is implemented practically by adding data to the end of
the array.
Example
The implementation of the above algorithm is shown below.

#include <stdio.h>
main() {
int Arr[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;

printf("The original array elements are:\n");


for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}

n = n + 1;

while( j >= k) {
Arr[j+1] = Arr[j];
j = j - 1;
}

Arr[k] = item;

printf("The array elements after insertion:\n");

for(i = 0; i<n; i++) {


printf("Arr[%d] = %d \n", i, Arr[i]);
}
}

Deletion Array Operations

Deletion is the process of eliminating an existing element from an array and rearranging all of
the array’s elements.

#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int m = 3, n = 5;
int i, j;

printf("The original array elements are:\n");

for(i = 0; i<n; i++) {


printf("Arr[%d] = %d \n", i, Arr[i]);
}

j = m;

while( j < n) {
Arr[j-1] = Arr[j];
j = j + 1;
}

n = n -1;
printf("The array elements after deletion:\n");

for(i = 0; i<n; i++) {


printf("Arr[%d] = %d \n", i, Arr[i]);
}
}

Search Array Operations

An array element can be found using either its value or its index.

#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;

printf("The original array elements are:\n");

for(i = 0; i<n; i++) {


printf("Arr[%d] = %d \n", i, Arr[i]);
}

while( j < n){


if( Arr[j] == item ) {
break;
}

j = j + 1;
}

printf("Found element %d at position %d\n", item, j+1);


}
Update Array Operations

Update array operations involve changing an existing array element at a certain index.

#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int m = 3, n = 5, item = 10;
int i, j;

printf("The original array elements are:\n");

for(i = 0; i<n; i++) {


printf("Arr[%d] = %d \n", i, Arr[i]);
}

Arr[m-1] = item;

printf("The array elements after updation:\n");

for(i = 0; i<n; i++) {


printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
Questions

1. What is the primary purpose of arrays?


2. Can arrays store elements of different data types?
3. How can I insert an element into an array at a specific position?
4. What is the time complexity for inserting an element into an array?
5. What are some common array operations?

You might also like