#include <stdio.h>
#include <stdlib.h>
struct MyArray
{
int TotalSize;
int UsedSize;
int *BaseAddress;
};
void star()
{
int j = 0;
printf("\n\n\t\t");
while (j <= 5)
{
printf("===============");
j++;
}
printf("\n\n");
}
void createArray(struct MyArray *a, int usize)
{
(*a).TotalSize = usize+10;
(*a).UsedSize = usize;
(*a).BaseAddress = (int *)malloc((usize+10) * sizeof(int));
}
void setArray(struct MyArray *a)
{
for (int i = 0; i < (*a).UsedSize; i++)
{
printf("\t\t\tEnter element %d: ", i + 1);
scanf("%d", &(*a).BaseAddress[i]);
}
}
void insertion(struct MyArray *a)
{
int j, position, value;
printf("\t\tEnter position of element to be inserted: ");
scanf("%d", &position);
if ((position > (*a).UsedSize + 1) || (position < 0) || ((*a).UsedSize > (*a).TotalSize))
printf("\t\tInsertiion not possible.");
else
{
printf("\t\tEnter value of element to be inserted: ");
scanf("%d", &value);
(*a).UsedSize++;
for (j = (*a).UsedSize; j >= position - 1; j--)
(*a).BaseAddress[j + 1] = (*a).BaseAddress[j];
(*a).BaseAddress[position - 1] = value;
}
}
void deletion(struct MyArray *a)
{
int j, position;
printf("\t\tEnter position of element to be deleted: ");
scanf("%d", &position);
if ((position > (*a).UsedSize) || (position < 0))
printf("\t\tDeletion not possible.");
else
{
for (j = position - 1; j < (*a).UsedSize; j++)
(*a).BaseAddress[j] = (*a).BaseAddress[j + 1];
(*a).UsedSize--;
}
}
void merging(struct MyArray *a, struct MyArray *b)
{
for (int j = 0; j < (*b).UsedSize; j++)
(*a).BaseAddress[(*a).UsedSize + j] = (*b).BaseAddress[j];
(*a).UsedSize += (*b).UsedSize;
}
void linear(struct MyArray *a)
{
int j, k = 0, item;
printf("\t\tEnter element to be searched: ");
scanf("%d", &item);
for (j = 0; j < (*a).UsedSize; j++)
{
if ((*a).BaseAddress[j] == item)
{
printf("\t\t%d is founded at position %d.", item, j + 1);
k++;
}
}
if (k == 0)
printf("\t\t%d is not founded.", item);
}
void sorting(struct MyArray *a)
{
int i, temp, k;
for (i = 1; i < (*a).UsedSize; i++)
{
temp = (*a).BaseAddress[i];
k = i - 1;
while (k >= 0 && temp <= (*a).BaseAddress[k])
{
(*a).BaseAddress[k + 1] = (*a).BaseAddress[k];
k--;
}
(*a).BaseAddress[k + 1] = temp;
}
}
void binary(struct MyArray *a)
{
int beg = 0, end = (*a).UsedSize, mid, j = 0, item;
printf("\n\t\tEnter element to be searched: ");
scanf("%d", &item);
while (beg <= end)
{
mid = (beg + end) / 2;
if ((*a).BaseAddress[mid] == item)
{
printf("\t\t%d is founded at position %d.", item, mid + 1);
j++;
break;
}
else if ((*a).BaseAddress[mid] < item)
beg = mid + 1;
else
end = mid - 1;
}
if (j == 0)
printf("\t\t%d not founded.", item);
}
void display(struct MyArray *a)
{
printf("\t\t\tPrinting the array...\n\t\t\t");
for (int i = 0; i < (*a).UsedSize; i++)
{
printf("%d ", (*a).BaseAddress[i]);
}
}
void main()
{
struct MyArray array1;
struct MyArray array2;
int size, choice, SArray;
star();
printf("\t\t\t *****CREATING AN ARRAY*****\n\n\t\t\tEnter the number of elements: ");
scanf("%d", &size);
createArray(&array1, size);
setArray(&array1);
star();
display(&array1);
while (1)
{
star();
printf("\n\t\t\t\t\tOPERATIONS IN ARRAY\n\n\t\t\tPress '1' to INSERT an element in an array\n\t\t\t
Press '2' to DELETE an element from an array\n\t\t\tPress '3' to MERGE two arrays\n\t\t\t
Press '4' to search an element using LINEAR SEARCH\n\t\t\tPress '5' to search an element
using SORT the array\n\t\t\tPress '6' to search an element using Binary SEARCH\n\n\t\t\t
Press '7' to DISPLAY the array\n\t\t\tPress 'any other key' to EXIT\n\t\t\tEnter your choice: ");
scanf("%d", &choice);
star();
if (choice == 1)
{
insertion(&array1);
star();
}
else if (choice == 2)
{
deletion(&array1);
star();
}
else if (choice == 3)
{
printf("\t\tEnter number of elements you want to merge: ");
scanf("%d", &SArray);
createArray(&array2, SArray);
setArray(&array2);
merging(&array1, &array2);
star();
}
else if (choice == 4)
{
linear(&array1);
star();
}
else if (choice == 5)
{
sorting(&array1);
star();
}
else if (choice == 6)
{
printf("\t\tSorting the array in ASCENDING order...\n");
sorting(&array1);
binary(&array1);
star();
}
else if (choice == 7)
{
display(&array1);
star();
}
else
{
printf("\t\t\tExiting...");
star();
exit(0);
}
}
}
Output :
==========================================================================================
*****CREATING AN ARRAY*****
Enter the number of elements: 5
Enter element 1: 12
Enter element 2: 5
Enter element 3: 21
Enter element 4: 1
Enter element 5: 43
==========================================================================================
Printing the array...
12 5 21 1 43
==========================================================================================
OPERATIONS IN ARRAY
Press '1' to INSERT an element in an array
Press '2' to DELETE an element from an array
Press '3' to MERGE two arrays
Press '4' to search an element using LINEAR SEARCH
Press '5' to search an element using SORT the array
Press '6' to search an element using Binary SEARCH
Press '7' to DISPLAY the array
Press 'any other key' to EXIT
Enter your choice: 1
==========================================================================================
Enter position of element to be inserted: 4
Enter value of element to be inserted: 20
==========================================================================================
==========================================================================================
OPERATIONS IN ARRAY
Press '1' to INSERT an element in an array
Press '2' to DELETE an element from an array
Press '3' to MERGE two arrays
Press '4' to search an element using LINEAR SEARCH
Press '5' to search an element using SORT the array
Press '6' to search an element using Binary SEARCH
Press '7' to DISPLAY the array
Press 'any other key' to EXIT
Enter your choice: 2
==========================================================================================
Enter position of element to be deleted: 1
==========================================================================================
==========================================================================================
OPERATIONS IN ARRAY
Press '1' to INSERT an element in an array
Press '2' to DELETE an element from an array
Press '3' to MERGE two arrays
Press '4' to search an element using LINEAR SEARCH
Press '5' to search an element using SORT the array
Press '6' to search an element using Binary SEARCH
Press '7' to DISPLAY the array
Press 'any other key' to EXIT
Enter your choice: 3
==========================================================================================
Enter number of elements you want to merge: 3
Enter element 1: 78
Enter element 2: 14
Enter element 3: 0
==========================================================================================
==========================================================================================
OPERATIONS IN ARRAY
Press '1' to INSERT an element in an array
Press '2' to DELETE an element from an array
Press '3' to MERGE two arrays
Press '4' to search an element using LINEAR SEARCH
Press '5' to search an element using SORT the array
Press '6' to search an element using Binary SEARCH
Press '7' to DISPLAY the array
Press 'any other key' to EXIT
Enter your choice: 7
==========================================================================================
Printing the array...
5 21 20 1 43 78 14 0
==========================================================================================
==========================================================================================
OPERATIONS IN ARRAY
Press '1' to INSERT an element in an array
Press '2' to DELETE an element from an array
Press '3' to MERGE two arrays
Press '4' to search an element using LINEAR SEARCH
Press '5' to search an element using SORT the array
Press '6' to search an element using Binary SEARCH
Press '7' to DISPLAY the array
Press 'any other key' to EXIT
Enter your choice: 4
==========================================================================================
Enter element to be searched: 20
20 is founded at position 3.
==========================================================================================
==========================================================================================
OPERATIONS IN ARRAY
Press '1' to INSERT an element in an array
Press '2' to DELETE an element from an array
Press '3' to MERGE two arrays
Press '4' to search an element using LINEAR SEARCH
Press '5' to search an element using SORT the array
Press '6' to search an element using Binary SEARCH
Press '7' to DISPLAY the array
Press 'any other key' to EXIT
Enter your choice: 6
==========================================================================================
Sorting the array in ASCENDING order...
Enter element to be searched: 20
20 is founded at position 5.
==========================================================================================
==========================================================================================
OPERATIONS IN ARRAY
Press '1' to INSERT an element in an array
Press '2' to DELETE an element from an array
Press '3' to MERGE two arrays
Press '4' to search an element using LINEAR SEARCH
Press '5' to search an element using SORT the array
Press '6' to search an element using Binary SEARCH
Press '7' to DISPLAY the array
Press 'any other key' to EXIT
Enter your choice: 8
==========================================================================================
Exiting...
==========================================================================================
Comments
Post a Comment