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

Week 2 Operations in Linear Array With Implementation in C

The document discusses operations on linear arrays in C including initialization, accessing elements, insertion, deletion, searching, and updating. It provides code examples and explanations for implementing each operation on a linear array in C.

Uploaded by

pillisaiganesh79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Week 2 Operations in Linear Array With Implementation in C

The document discusses operations on linear arrays in C including initialization, accessing elements, insertion, deletion, searching, and updating. It provides code examples and explanations for implementing each operation on a linear array in C.

Uploaded by

pillisaiganesh79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Operations in Linear Array with implementation in C

Topic 2
Array Initialization
Accessing Elements
Insertion
Deletion
Searching
Updating
Learning Objectives

By the end of this chapter, you will be able to:

• Discuss the Linear Array Initialization


• Explain the Concepts of Accessing Elements in Linear Array
•Explain the Concepts of Element Insertion, Deletion, Searching, Updating in Linear Array
Linear Array in C
A linear array is a collection of elements of the same data type arranged in a
sequential manner. Each element in the array can be accessed using its index,
which starts from 0 for the first element and goes up to the size of the array
minus 1 for the last element.
Let's cover the following operations:
Array Initialization
Accessing Elements
Insertion
Deletion
Searching
Updating
Array Initialization
#include <stdio.h>
#include<conio.h>
#define MAX_SIZE 100
void main() {
int arr[MAX_SIZE]; // Declaration of the array
int size; // Number of elements in the array
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
◦ printf("Array initialized successfully!\n");
getch();
}
Array Initialization
#include <stdio.h>
#include<conio.h>
#define MAX_SIZE 100
void main() {
int arr[MAX_SIZE]; // Declaration of the array
int size; // Number of elements in the array
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
◦ printf("Array initialized successfully!\n");
getch();
}
Accessing Elements
#include <stdio.h> printf("Enter the index of the element you want to access: ");
#define MAX_SIZE 100 scanf("%d", &index);
void main() { if (index >= 0 && index < size) {
int arr[MAX_SIZE]; printf("Element at index %d: %d\n", index, arr[index]);
int size, index; }
printf("Enter the size of the array: "); else {
scanf("%d", &size); printf("Invalid index! Please enter an index between 0 and
%d.\n", size - 1);
printf("Enter the elements of the array:\n");
}
for (int i = 0; i < size; i++) {
getch();
scanf("%d", &arr[i]);
}
}
Insertion
#include <stdio.h>
#define MAX_SIZE 100 if (index < 0 || index > size) {
int main() { printf ("Invalid index! Please enter an index
int arr[MAX_SIZE]; between 0 and %d.\n", size);
int size, index, value; }
printf("Enter the size of the array: "); else {
scanf("%d", &size); // Shift elements to the right to make space for
printf("Enter the elements of the array:\n"); the new element
for (int i = 0; i < size; i++) { for (int i = size - 1; i >= index; i--) {
scanf("%d", &arr[i]); arr[i + 1] = arr[i];
} }
printf("Enter the index where you want to arr[index] = value;
insert the element: "); size++; // Increase the size of the array
scanf("%d", &index); printf("Element inserted successfully!\n");
printf("Enter the value to be inserted: "); }
scanf("%d", &value); getch();
}
Deletion
#include <stdio.h> if (index < 0 || index >= size) {
#define MAX_SIZE 100 printf("Invalid index! Please enter an index between 0
and %d.\n", size - 1);
void main() { }
int arr[MAX_SIZE]; else {
int size, index; // Shift elements to the left to overwrite the element
printf("Enter the size of the array: "); to be deleted
scanf("%d", &size); for (int i = index; i < size - 1; i++) {
printf("Enter the elements of the array:\n"); arr[i] = arr[i + 1];
for (int i = 0; i < size; i++) { }
scanf("%d", &arr[i]); size--; // Decrease the size of the array
} printf("Element deleted successfully!\n");
printf("Enter the index of the element you }
want to delete: "); getch();
scanf("%d", &index); }
Searching
#include <stdio.h>
int found = 0;
#define MAX_SIZE 100 int index = -1;
int main() { for (int i = 0; i < size; i++) {
if (arr[i] == value)
void arr[MAX_SIZE]; {
int size, value; found = 1;
index = i;
printf("Enter the size of the array: "); break;
scanf("%d", &size); }
}
printf("Enter the elements of the array:\n");
if (found) {
for (int i = 0; i < size; i++) { printf("Value found at index %d.\n", index);
}
scanf("%d", &arr[i]);
else {
} printf("Value not found in the array.\n");
printf("Enter the value to be searched: "); }
getch();
scanf("%d", &value); }
Updating
#include <stdio.h> printf("Enter the index of the element you want to update: ");

#define MAX_SIZE 100 scanf("%d", &index);


if (index >= 0 && index < size) {
void main() {
printf("Enter the new value: ");
int arr[MAX_SIZE];
scanf("%d", &value);
int size, index, value;
arr[index] = value;
printf("Enter the size of the array: "); printf("Element updated successfully!\n");
scanf("%d", &size); }
printf("Enter the elements of the array:\n"); else {

for (int i = 0; i < size; i++) { printf("Invalid index! Please enter an index between 0 and
%d.\n", size - 1);
scanf("%d", &arr[i]); }
} getch();
}

You might also like