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

Program To Insert Element in Array

The document discusses several C/C++ programs for array operations like insertion, deletion, searching, union, intersection, matrix addition, stack implementation using arrays, and queues. It includes code snippets to insert an element into an array at a given position, delete an element from an array without position, search an element in an array, find union and intersection of two arrays, add two matrices, implement a stack using arrays, and implement a basic queue using arrays.

Uploaded by

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

Program To Insert Element in Array

The document discusses several C/C++ programs for array operations like insertion, deletion, searching, union, intersection, matrix addition, stack implementation using arrays, and queues. It includes code snippets to insert an element into an array at a given position, delete an element from an array without position, search an element in an array, find union and intersection of two arrays, add two matrices, implement a stack using arrays, and implement a basic queue using arrays.

Uploaded by

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

Program to insert element in array

if(pos > size+1 || pos <= 0)


{
printf("Invalid position! Please enter position between 1 to %d", size);
}
else
{
/* Make room for new array element by shifting to right */
for(i=size; i>=pos; i--)
{
arr[i] = arr[i-1];
}

/* Insert new element at given position and increment size */


arr[pos-1] = num;
size++;

// C program to implement insert operation in


// an sorted array.
#include <stdio.h>
  
// C program to implement insert operation in
// an sorted array without position
#include <stdio.h>

// Inserts a key in arr[] of given capacity. n is current


// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;

int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];

arr[i + 1] = key;

return (n + 1);
}

/* Driver program to test above function */


int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;

printf("\nBefore Insertion: ");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

// Inserting key
n = insertSorted(arr, n, key, capacity);

printf("\nAfter Insertion: ");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}  
delete int main()
{
   int array[100], position, c, n;

   printf("Enter number of elements in array\n");


   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to delete


element\n");
   scanf("%d", &position);

   if (position >= n+1)
      printf("Deletion not possible.\n");
   else
   {
      for (c = position - 1; c < n - 1; c++)
         array[c] = array[c+1];

      printf("Resultant array:\n");
      for (c = 0; c < n - 1; c++)
         printf("%d\n", array[c]);
   }

   return 0;
}

DELETE Without position


int main()

int array[100], value, c, n;

printf("Enter number of elements in array\n");

scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter the value delete element\n");

scanf("%d", &value);

for (c = n; c >= 0; c--)

if(array[c]==value)

printf(" it is present on %d ",c);

for (int j=c; j<n ; j++)

array[j]=array[j+1];
}

printf("Resultant array:\n");

for (c = 0; c < n - 1; c++)

printf("%d\n", array[c]);

return 0;

Search
 for (c = 0; c < n; c++)
  {
    if (array[c] == search)    /* If required element is found */
    {
      printf("%d is present at location %d.\n", search, c+1);
      break;
    }
  }
  if (c == n)
    printf("%d isn't present in the array.\n", search);

union & intersection

union
#include<stdio.h>
/* Function prints union of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
printf(" %d ", arr1[i++]);
else if (arr2[j] < arr1[i])
printf(" %d ", arr2[j++]);
else
{
printf(" %d ", arr2[j++]);
i++;
}
}

/* Print remaining elements of the larger array */


while(i < m)
printf(" %d ", arr1[i++]);
while(j < n)
printf(" %d ", arr2[j++]);
}

/* Driver program to test above function */


int main()
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printUnion(arr1, arr2, m, n);
getchar();
return 0;
}
intersection
// C program to find intersection of
// two sorted arrays
#include<stdio.h>

/* Function prints Intersection of arr1[] and arr2[]


m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else /* if arr1[i] == arr2[j] */
{
printf(" %d ", arr2[j++]);
i++;
}
}
}
Union

union
#include<stdio.h>

/* Function prints union of arr1[] and arr2[]


m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
printf(" %d ", arr1[i++]);
else if (arr2[j] < arr1[i])
printf(" %d ", arr2[j++]);
else
{
printf(" %d ", arr2[j++]);
i++;
}
}

/* Print remaining elements of the larger array */


while(i < m)
printf(" %d ", arr1[i++]);
while(j < n)
printf(" %d ", arr2[j++]);
}

/* Driver program to test above function */


int main()
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printUnion(arr1, arr2, m, n);
getchar();
return 0;
}
sum

for (int i=0; i<3; i++){for (int j=0; j<3; j++)


{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0; i <3; i++)
{
for(int j=0; j<3; j++)
{
if(a[i][j] != b[i][j])
{
printf("\n not identical");
return 0;
}
}
}

printf("\n identical");
return 0;

stack

/
********************************************
**********
QUEUE C++
AUTHOR
URI:https://mostafa.itnishi.com

********************************************
**********/
#include<iostream>
#include<conio.h>
using namespace std;
int top=-1, z[5];
void push(int value)
{
if(top==4)
{
cout<<"\n Stack is Full or
overflow!\n";
}
else
{
top++;
z[top]=value;
}
}
void pop()
{
if(top==-1)
{
cout<<"n stack is Empty or
underflow!\n";
}
else
{
top--;
}
}
void display()
{
if(top==-1)
{
cout<<"\n nothing to display !\n";
}
else
{
cout<< "\n array is : \n";
for(int i=0;i<=top;i++)
{
cout<<"\t"<<z[i];
}
}
}

int main()
{
int value, choice;
do{
cout<<"\n 1.push \n 2.pop \n
3.display \n 4.exit\n \n input choice: ";
cin>>choice;

if(choice==1)
{
cout<<"\n enter a value";
cin>> value;
push(value);
}
if(choice==2)
{
pop();
}
if(choice==3)
{
display();
}
}
while(choice!=4);
cout<<"\n\n\n Existing......\n";
getch();
return 0;
}

You might also like