0% found this document useful (0 votes)
4 views30 pages

Array Operations in C Programming

The document contains multiple C programs demonstrating array operations such as insertion at the beginning, end, and a specific position, as well as deletion at the beginning, end, and a specific position. It also includes linear and binary search implementations, along with bubble sort. Each section provides sample outputs to illustrate the functionality of the code.

Uploaded by

Pranay Paswan
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)
4 views30 pages

Array Operations in C Programming

The document contains multiple C programs demonstrating array operations such as insertion at the beginning, end, and a specific position, as well as deletion at the beginning, end, and a specific position. It also includes linear and binary search implementations, along with bubble sort. Each section provides sample outputs to illustrate the functionality of the code.

Uploaded by

Pranay Paswan
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

CREATING AN ARRAY AND INSERTION AT

BEGINNING
#include<stdio.h>

int arr[100];
int n = 0;

void InsertAtBeginning(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
for (int i = n; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
InsertAtBeginning(x);
Print();
}
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
5
Array is: 5
Enter the number
4
Array is: 4 5
Enter the number
3
Array is: 3 4 5
Enter the number
2
Array is: 2 3 4 5
Enter the number
1
Array is: 1 2 3 4 5
CREATING AN ARRAY AND INSERTION AT ENDING
#include<stdio.h>

int arr[100];
int n = 0;

void InsertAtEnd(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
InsertAtEnd(x);
Print();
}
return 0;
}
OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
1
Array is: 1
Enter the number
2
Array is: 1 2
Enter the number
3
Array is: 1 2 3
Enter the number
4
Array is: 1 2 3 4
Enter the number
5
Array is: 1 2 3 4 5
CREATING AN ARRAY AND INSERTION AT A GIVEN
POSITION
#include<stdio.h>

int arr[100];
int n = 0;

void InsertAtPosition(int x, int pos) {


if (n >= 100) {
printf("Array is full.\n");
return;
}
if (pos < 0 || pos > n) {
printf("Invalid position.\n");
return;
}
for (int i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x, pos;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
InsertAtPosition(x, n);
Print();
}
printf("Do you want to insert more numbers? (1/0)\n");
int choice;
scanf("%d", &choice);
while (choice == 1) {
printf("Enter the number and position \n");
scanf("%d %d", &x, &pos);
InsertAtPosition(x, pos);
Print();
printf("Do you want to insert more numbers? (1/0)\n");
scanf("%d", &choice);
}
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
1
Array is: 1
Enter the number
2
Array is: 1 2
Enter the number
3
Array is: 1 2 3
Enter the number
4
Array is: 1 2 3 4
Enter the number
6
Array is: 1 2 3 4 6
Do you want to insert more numbers? (1/0)
1
Enter the number and position
5
4
Array is: 1 2 3 4 5 6
Do you want to insert more numbers? (1/0)
0
CREATING AN ARRAY AND DELETION AT
BEGINNING
#include<stdio.h>

int arr[100];
int n = 0;

void DeleteAtBeginning() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
for (int i = 0; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
}

void Insert(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Print();
}
printf("Deleting node at beginning...\n");
DeleteAtBeginning();
Print();
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
5
Array is: 5
Enter the number
1
Array is: 5 1
Enter the number
2
Array is: 5 1 2
Enter the number
3
Array is: 5 1 2 3
Enter the number
4
Array is: 5 1 2 3 4
Deleting node at beginning...
Array is: 1 2 3 4
CREATING AN ARRAY AND DELETION AT ENDING
#include<stdio.h>

int arr[100];
int n = 0;

void DeleteAtEnd() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
n--;
}

void Insert(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Print();
}
printf("Deleting node at end...\n");
DeleteAtEnd();
Print();
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
1
Array is: 1
Enter the number
2
Array is: 1 2
Enter the number
3
Array is: 1 2 3
Enter the number
4
Array is: 1 2 3 4
Enter the number
6
Array is: 1 2 3 4 6
Deleting node at end...
Array is: 1 2 3 4
CREATING AN ARRAY AND DELETION AT A
POSITION
#include<stdio.h>

int arr[100];
int n = 0;

void DeleteAtPosition(int pos) {


if (n == 0) {
printf("Array is empty.\n");
return;
}
if (pos < 0 || pos >= n) {
printf("Invalid position.\n");
return;
}
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
}

void Insert(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Print();
}
printf("Enter the position to delete: ");
int pos;
scanf("%d", &pos);
DeleteAtPosition(pos);
Print();
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
1
Array is: 1
Enter the number
2
Array is: 1 2
Enter the number
3
Array is: 1 2 3
Enter the number
5
Array is: 1 2 3 5
Enter the number
4
Array is: 1 2 3 5 4
Enter the position to delete: 3
Array is: 1 2 3 4
CREATING AN ARRAY AND PERFORM LINEAR
SEARCH
#include<stdio.h>

int arr[100];
int n = 0;

int LinearSearch(int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}

void Insert(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Print();
}
printf("Enter the number to search: ");
scanf("%d", &x);
int index = LinearSearch(x);
if (index == -1) {
printf("Number not found.\n");
} else {
printf("Number found at index %d.\n", index);
}
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
1
Array is: 1
Enter the number
2
Array is: 1 2
Enter the number
3
Array is: 1 2 3
Enter the number
4
Array is: 1 2 3 4
Enter the number
5
Array is: 1 2 3 4 5
Enter the number to search: 4
Number found at index 3.
CREATING AN ARRAY AND PERFORM BINARY
SEARCH
#include<stdio.h>

int arr[100];
int n = 0;

int BinarySearch(int x) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}

void Insert(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
for (int i = n - 1; i > 0; i--) {
if (arr[i] < arr[i - 1]) {
int temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
} else {
break;
}
}
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Print();
}
printf("Enter the number to search: ");
scanf("%d", &x);
int index = BinarySearch(x);
if (index == -1) {
printf("Number not found.\n");
} else {
printf("Number found at index %d.\n", index);
}
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
1
Array is: 1
Enter the number
2
Array is: 1 2
Enter the number
3
Array is: 1 2 3
Enter the number
4
Array is: 1 2 3 4
Enter the number
5
Array is: 1 2 3 4 5
Enter the number to search: 4
Number found at index 3.
CREATING AN ARRAY AND PERFORM BUBBLE SORT
#include<stdio.h>

int arr[100];
int n = 0;

void BubbleSort() {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void Insert(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
}

void Print() {
if (n == 0) {
printf("Array is empty.\n");
return; }
printf("Array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Print();
}
printf("Sorting array...\n");
BubbleSort();
Print();
return 0;
}

OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
5
Array is: 5
Enter the number
1
Array is: 5 1
Enter the number
2
Array is: 5 1 2
Enter the number
4
Array is: 5 1 2 4
Enter the number
3
Array is: 5 1 2 4 3
Sorting array...
Array is: 1 2 3 4 5
CREATING AN ARRAY AND PERFORM TRAVERSAL
#include<stdio.h>

int arr[100];
int n = 0;

void Traverse() {
if (n == 0) {
printf("Array is empty.\n");
return;
}
printf("Array elements are: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

void Insert(int x) {
if (n >= 100) {
printf("Array is full.\n");
return;
}
arr[n] = x;
n++;
}

int main() {
printf("Pranay Paswan\n");
printf("How many numbers?\n");
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
printf("Enter the number \n");
scanf("%d", &x);
Insert(x);
Traverse();
}
return 0;
}
OUTPUT:
Pranay Paswan
How many numbers?
5
Enter the number
1
Array elements are: 1
Enter the number
2
Array elements are: 1 2
Enter the number
3
Array elements are: 1 2 3
Enter the number
4
Array elements are: 1 2 3 4
Enter the number
5
Array elements are: 1 2 3 4 5

You might also like