# Arrays | GATE Computer Science Engineering(CSE) 2024 Mock Test Series - Computer Science Engineering (CSE) PDF Download

 Table of contents What is an Array? Array Representation Basic Operations Insertion Operation Search Operation Update Operation

## What is an Array?

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.
• Element − Each item stored in an array is called an element.

• Index − Each location of an element in an array has a numerical index, which is used to identify the element.

## Array Representation

Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

As per the above illustration, following are the important points to be considered.

• Index starts with 0.

• Array length is 10 which means it can store 10 elements.

• Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.

## Basic Operations

Following are the basic operations supported by an array.

• Traverse − print all the array elements one by one.

• Insertion − Adds an element at the given index.

• Deletion − Deletes an element at the given index.

• Search − Searches an element using the given index or by the value.

• Update − Updates an element at the given index.

In C, when an array is initialized with size, then it assigns defaults values to its elements in following order.

 Data Type Default Value bool false char 0 int 0 float 0.0 double 0.0f void wchar_t 0

## Insertion Operation

Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.

Here, we see a practical implementation of insertion operation, where we add data at the end of the array −

### Algorithm

Let Array be a linear unordered array of MAX elements.

### Example

Result

Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA −

`1. Start2. Set J = N3. Set N = N+14. Repeat steps 5 and 6 while J >= K5. Set LA[J+1] = LA[J]6. Set J = J-17. Set LA[K] = ITEM8. Stop`

### Example

Following is the implementation of the above algorithm −

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

When we compile and execute the above program, it produces the following result −

### Output

`The original array elements are :LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after insertion :LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 10 LA[4] = 7 LA[5] = 8 `

Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all elements of an array.

### Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kthposition of LA.

`1. Start2. Set J = K3. Repeat steps 4 and 5 while J < N4. Set LA[J-1] = LA[J]5. Set J = J+16. Set N = N-17. Stop`

### Example

Following is the implementation of the above algorithm −

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

When we compile and execute the above program, it produces the following result −

### Output

`The original array elements are :LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after deletion :LA[0] = 1 LA[1] = 3 LA[2] = 7 LA[3] = 8 `

## Search Operation

You can perform a search for an array element based on its value or its index.

### Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search.

`1. Start2. Set J = 03. Repeat steps 4 and 5 while J < N4. IF LA[J] is equal ITEM THEN GOTO STEP 65. Set J = J +16. PRINT J, ITEM7. Stop`

### Example

Following is the implementation of the above algorithm −

`#include <stdio.h>main() {   int LA[] = {1,3,5,7,8};   int item = 5, n = 5;   int i = 0, j = 0;      printf("The original array elements are :");	   for(i = 0; i<n; i++) {      printf("LA[%d] = %d ", i, LA[i]);   }       while( j < n){      if( LA[j] == item ) {         break;      }		      j = j + 1;   }	   printf("Found element %d at position %d", item, j+1);}`

When we compile and execute the above program, it produces the following result −

### Output

`The original array elements are :LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 Found element 5 at position 3`

## Update Operation

Update operation refers to updating an existing element from the array at a given index.

### Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA.

`1. Start2. Set LA[K-1] = ITEM3. Stop`

### Example

Following is the implementation of the above algorithm −

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

When we compile and execute the above program, it produces the following result −

### Output

`The original array elements are :LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after updation :LA[0] = 1 LA[1] = 3 LA[2] = 10 LA[3] = 7 LA[4] = 8 `

The document Arrays | GATE Computer Science Engineering(CSE) 2024 Mock Test Series - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course GATE Computer Science Engineering(CSE) 2024 Mock Test Series.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

## GATE Computer Science Engineering(CSE) 2024 Mock Test Series

150 docs|216 tests

## FAQs on Arrays - GATE Computer Science Engineering(CSE) 2024 Mock Test Series - Computer Science Engineering (CSE)

 1. What is an array and how does it work?
Ans. An array is a data structure that stores a fixed-size sequence of elements of the same type. It works by allocating a contiguous block of memory to store the elements, which can be accessed using an index. The index starts from 0 for the first element and increases by 1 for each subsequent element.
 2. What are the basic operations that can be performed on an array?
Ans. The basic operations that can be performed on an array include: - Accessing an element: You can access any element in an array by using its index. - Inserting an element: You can insert an element at a specific position in the array. - Deleting an element: You can delete an element from a specific position in the array. - Sorting the array: You can arrange the elements of the array in a specific order, such as ascending or descending. - Finding the length of the array: You can determine the number of elements in the array.
 3. How does the search operation work on an array?
Ans. The search operation on an array involves iterating through each element of the array and comparing it with the target element. If a match is found, the index of the element is returned. If the target element is not found, a special value (such as -1) can be returned to indicate the absence of the element.
 4. How can an element be updated in an array?
Ans. To update an element in an array, you can simply assign a new value to the specific index of the element. By accessing the element using its index, you can modify its value as needed.
 5. Can an array store elements of different types?
Ans. In most programming languages, arrays are designed to store elements of the same type. However, some languages, such as Python, allow arrays to store elements of different types by using a concept called "arrays of arrays" or "nested arrays." In such cases, each nested array can store elements of a different type.

## GATE Computer Science Engineering(CSE) 2024 Mock Test Series

150 docs|216 tests

### Up next

 Explore Courses for Computer Science Engineering (CSE) exam

### Top Courses for Computer Science Engineering (CSE)

Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;