You are on page 1of 56

1

1.PROGRAM TO TRAVERSE AN ARRAY.


#include <stdio.h>
int main() {
// Define an array
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the
array
// Traverse the array and print its elements
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
2

2.PROGRAM TO INSERTION OF AN ARRAY.


#include <stdio.h>
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;
int element;

printf("Original Array: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Enter element to insert at the beginning: ");
scanf("%d", &element);
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = element;
size++;
printf("Modified Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
3

printf("\n");
return 0;
}
4

3.PROGRAM TO INSERT AN ELEMENT IN ARRAY


ON LAST LOCATION.
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10], i, element;
printf("Enter 5 Array Elements: ");
for(i=0; i<5; i++)
scanf("%d", &arr[i]);
printf("\nEnter Element to Insert: ");
scanf("%d", &element);
arr[i] = element;
printf("\nThe New Array is:\n");
for(i=0; i<6; i++)
printf("%d ", arr[i]);
getch();
return 0;
}
5

4.PROGRAM TO INSERT AN ARRAY ON FIRST


LOCATION IN AN ARRAY.
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;
int element;

printf("Original Array: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Enter element to insert at the beginning: ");
scanf("%d", &element);
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = element;
size++;
printf("Modified Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
6

}
printf("\n");
return 0;
}
7

5.PROGRAM TO INSERT AN ELEMENT AT THE


GIVEN LOCATION.
#include <stdio.h>
int main() {
int arr[100] = {1, 2, 3, 4, 5};
int n = 5;
int element = 10;
int position = 2;
for (int i = n; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
n++;
printf("Array after insertion: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
8

6.PROGRAM TO DELETE AN ARRAY FROM GIVEN


LOCATION IN ARRAY.
#include <iostream>
using namespace std;
void deleteElement(int arr[], int& size, int index) {
if (index < 0 || index >= size) {
cout << "Invalid index!" << endl;
return;
}
for (int i = index; i < size - 1; ++i) {
arr[i] = arr[i + 1];
}
size--;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int indexToDelete = 2;
cout << "Array before deletion:" << endl;
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
9

deleteElement(arr, size, indexToDelete);


cout << "Array after deletion:" << endl;
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
10

7.PROGRAM TO SEARCH A GIVEN ELEMENT


FROM ARRAY.
#include<stdio.h>
int main()
{
int a[100], n, element, pos=0;
int i;
printf("Enter array size [1-100]: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(i=0; i<n; i++)scanf("%d", &a[i]);
printf("Enter element to search: ");
scanf("%d",&element);
for(i=0; i<n; i++)
{
if(a[i]==element)
{
printf("%d found at position %d", element, i+1);
return 0;
}
}
printf("%d not found.", element);
return 0;
}
11
12

8.PROGRAM TO DELETE A GIVEN ELEMENT FROM


ARRAY.
#include <stdio.h>
void deleteElement(int arr[], int size, int index) {
if (index < 0 || index >= size) {
printf("Invalid index\n");
return;
}
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
// Decrease the size of the array
size--;
}
int main() {
int size, index;
printf("Enter the size of the array: ");
scanf("%d", &size);

int arr[size];
printf("Enter elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
13

printf("Enter the index of the element to delete: ");


scanf("%d", &index);

deleteElement(arr, size, index);


printf("Array after deletion:\n");
for (int i = 0; i < size - 1; i++) {
printf("%d ", arr[i]);
}
return 0;
}
14

9.PROGRAM TO INSERT ELEMENT TO MULTI-


DIMENSIONAL ARRAY AS MATRIX.
#include <stdio.h>
#define ROWS 3
#define COLS 3
void insertElement(int matrix[ROWS][COLS], int row, int col, int
value) {
matrix[row][col] = value;
}
void displayMatrix(int matrix[ROWS][COLS]) {
printf("Matrix:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int matrix[ROWS][COLS] = {{0}};
insertElement(matrix, 0, 0, 1);
insertElement(matrix, 0, 1, 2);
insertElement(matrix, 0, 2, 3);
insertElement(matrix, 1, 0, 4);
insertElement(matrix, 1, 1, 5);
15

insertElement(matrix, 1, 2, 6);
insertElement(matrix, 2, 0, 7);
insertElement(matrix, 2, 1, 8);
insertElement(matrix, 2, 2, 9);
displayMatrix(matrix);
return 0;
}
16

10.PROGRAM TO ADD TWO MATRICES.


#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
// add the matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
17

}
// print the result
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
18

11.PROGRAM TO SUBTRACT TWO MATRIX.


#include < stdio.h >
int main()
{
int m, n, c, d, first[10][10], second[10][10], difference[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", & m, & n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & second[c][d]);
printf("Difference of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
difference[c][d] = first[c][d] - second[c][d];
printf("%d\t", difference[c][d]);
}
printf("\n");
}
return 0;
19

}
20

12.PROGRAM TO MULTIPLY TWO MATRIX.


#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
printf("Enter first 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat1[i][j]);
}
printf("Enter second 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat2[i][j]);
}
printf("\nMultiplying two matrices...");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
21

sum=0;
for(k=0; k<3; k++)
sum = sum + mat1[i][k] * mat2[k][j];
mat3[i][j] = sum;
}
}
printf("\nMultiplication result of the two given Matrix is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d\t", mat3[i][j]);
printf("\n");
}
getch();
return 0;
}
22

13.PROGRAM TO FIND THE LARGEST ELEMENT


FROM ARRAY.
#include <stdio.h>
int main()
{
int arr[] = {25, 11, 7, 75, 56};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Initialize max with first element of array.
int max = arr[0];
//Loop through the array
for (int i = 0; i < length; i++) {
//Compare elements of array with max
if(arr[i] > max)
max = arr[i];
}
printf("Largest element present in given array: %d\n", max);
return 0;
}
23

14.PROGRAM TO FIND THE SMALLEST ELEMENT


FROM ARRAY.
include <stdio.h>
int main()
{
//Initialize array
int arr[] = {25, 11, 7, 75, 56};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Initialize min with first element of array.
int min = arr[0];
//Loop through the array
for (int i = 0; i < length; i++) {
if(arr[i] < min)
min = arr[i];
}
printf("Smallest element present in given array: %d\n", min);
return 0;
}
24

15.PROGRAM TO TRAVERSE A STACK USING


ARRAY.
#include<iostream>
using namespace std;
#define MAX_SIZE 100
class Stack {
int top;
int arr[MAX_SIZE];
public:
Stack() {
top = -1;
}
bool isEmpty() {
if (top == -1) {
return true;
} else {
return false;
}
}
bool isFull() {
if (top == MAX_SIZE - 1) {
return true;
} else {
return false;
25

}
}
void push(int val) {
if (isFull()) {
cout << "Stack Overflow" << endl;
} else {
top++;
arr[top] = val;
}
}
void pop() {
if (isEmpty()) {
cout << "Stack Underflow" << endl;
} else {
top--;
}
}
void traverse() {
if (isEmpty()) {
cout << "Stack is Empty" << endl;
} else {
cout << "Elements in the stack are:" << endl;
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
26

cout << endl;


}
}
};
int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
s.traverse();
s.pop();
s.traverse();
return 0;
}
27

16.PROGRAM TO CREATE AND TRAVERSE A


LINKED LIST.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
void insertNode(Node* &head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
28

}
}
void traverse(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
Node* head = nullptr;
insertNode(head, 1);
insertNode(head, 2);
insertNode(head, 3);
insertNode(head, 4);
traverse(head);
return 0;
}
29

17.PROGRAM TO INSERT THE ELEMENT AT THE


BEGINNING OF LINKED LIST.
#include <iostream>
struct Node {
int data;
Node* next;
};
// Function to insert element at the beginning of linked list
Node* insertBeginning(Node* head, int newData) {
Node* newNode = new Node();
newNode->data = newData;
newNode->next = head;
return newNode;
}
// Function to print the linked list
void printList(Node* head) {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "nullptr" << std::endl;
}
30

int main() {
Node* head = nullptr;
head = insertBeginning(head, 5);
head = insertBeginning(head, 10);
std::cout << "Linked List: ";
printList(head);
return 0;
}
31

18.PROGRAM TO INSERT AN ELEMENT AT THE


END OF THE LINKED LIST.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
Node* head;
public:
LinkedList(){
head = NULL;
}
//Add new element at the end of the list
void push_back(int newElement) {
Node* newNode = new Node();
newNode->data = newElement;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
} else {
Node* temp = head;
32

while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
//display the content of the list
void PrintList() {
Node* temp = head;
if(temp != NULL) {
cout<<"The list contains: ";
while(temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
} else {
cout<<"The list is empty.\n";
}
}
};
int main() {
LinkedList MyList;
//Add three elements at the end of the list.
MyList.push_back(10);
MyList.push_back(20);
33

MyList.push_back(30);
MyList.PrintList();
return 0;
}
34

19.PROGRAM TO PUSH AN ELEMENT TO STACK.


#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
void display(void);
void main ()
{
int choice;
s.top = -1;
do
{
push();
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &choice);
}while(choice==1);
display();
}
35

void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}

void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
36

}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
}
37

20.PROGRAM TO POP AN ELEMENT FROM STACK.


#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
void display(void);
int pop(void);
void main ()
{
int choice;
s.top = -1;
do
{
push();
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &choice);
}while(choice==1);
pop();
38

display();
}
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to push\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
39

return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n Current stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
40

} printf ("\n");
}
41

21.PROGRAM TO TRAVERSE A QUEUE USING


ARRAY.
#include <iostream>
using namespace std;
#define MAX_SIZE 100
class Queue {
private:
int front, rear;
int arr[MAX_SIZE];
public:
Queue() {
front = -1;
rear = -1;
}
void enqueue(int value) {
if (rear == MAX_SIZE - 1) {
cout << "Queue is full" << endl;
} else {
if (front == -1) {
front = 0;
}
rear++;
arr[rear] = value;
}
42

}
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue is empty" << endl;
} else {
cout << "Dequeued element: " << arr[front] << endl;
front++;
}
}
void traverse() {
if (front == -1) {
cout << "Queue is empty" << endl;
} else {
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};

int main() {
Queue q;
43

q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.traverse();
q.dequeue();
q.traverse();
return 0;
}
44

22.PROGRAM TO INSERT AN ELEMENT IN QUEUE


USING ARRAY.
#include <iostream>
using namespace std;
#define SIZE 5
class Queue {
private:
int front, rear;
int arr[SIZE];
public:
Queue() {
front = -1;
rear = -1;
}
bool isFull() {
if (rear == SIZE - 1) {
return true;
}
return false;
}
bool isEmpty() {
if (front == -1 && rear == -1) {
return true;
}
45

return false;
}
void enqueue(int value) {
if (isFull()) {
cout << "Queue is full. Cannot insert element." << endl;
} else {
if (isEmpty()) {
front = 0;
}
rear++;
arr[rear] = value;
cout << value << " inserted into queue." << endl;
}
}
void display() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
} else {
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
46

};

int main() {
Queue q;

q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);

q.display();

return 0;
}
47

23.PROGRAM TO REMOVE AN ELEMENT FROM


QUEUE.
#include <iostream>
#include <queue>
using namespace std;
void removeElement(queue<int> &q, int element) {
queue<int> temp;
while (!q.empty()) {
if (q.front() != element) {
temp.push(q.front());
}
q.pop();
}
while (!temp.empty()) {
q.push(temp.front());
temp.pop();
}
}
int main() {
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
48

int element = 3;
removeElement(q, element);
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
return 0;
}
49

24.PROGRAM TO DO A SELECTION SORT OF


ARRAY ELEMENT.
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++) // finding minimum element (n-1)
times
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
50

}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
51

25.PROGRAM TO DO INSERTION SORT OF ARRAY


ELEMENT.
include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main()
{
int i;
int arr[] = {6,2,1,3,5};
int n = 5;
insertionSort(arr, n);
for (i = 0; i < n; i++)
printf("%d\n", arr[i]);
52

return 0;
}
53

26.PROGRAM OF LINEAR SEARCH.


#include <stdio.h>
int linearSearch(int arr[], int n, int target) {
int i;
for (i = 0; i< n; i++) {
if (arr[i] == target) {
return i; // Element found at index i
}
}
return -1; // Element not found
}
int main() {
int arr[] = {10, 2, 8, 5, 17};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 8;
int result = linearSearch(arr, n, target);
if (result == -1) {
printf("Element not found in the array.\n");
} else {
printf("Element found at index: %d\n", result);
}
return 0;
}
54
55

27.PROGRAM OF BINARY SEARCH.


#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
56

last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}

You might also like