Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Computer note

Uploaded by

sheriffalao2
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)
3 views

Computer note

Uploaded by

sheriffalao2
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/ 4

Name: Alao Sheriff Olumide

Matric Number: 22/01DL064


Course: CSC 317
Question: Run the code for testing the algorithm for Insertion, Deletion, Update, and Search
Operations

1. Insertion Operation

#include <stdio.h>

int main()
{
int LA[] = {1, 3, 5, 7, 8};
int item = 10;
int k = 3;
int n = 5;
int i, j = n;

printf("The original array elements are:\n");


for (i = 0; i < n; i++)
{
printf("LA[%d] = %d\n", i, LA[i]);
}

n = n + 1;
while (j >= k)
{
LA[j + 1] = LA[j];
j = j - 1;
}
LA[k] = item;

printf("\nThe array elements after insertion:\n");


for (i = 0; i < n; i++)
{
printf("LA[%d] = %d\n", i, LA[i]);
}

return 0;
}

2. Deletion Operation
#include <stdio.h>

int main()
{
int LA[] = {1, 3, 5, 7, 8};
int k = 3;
int n = 5;
int i, j;

printf("The original array elements are:\n");


for (i = 0; i < n; i++)
{
printf("LA[%d] = %d\n", i, LA[i]);
}

j = k;
while (j < n)
{
LA[j - 1] = LA[j];
j = j + 1;
}
n = n - 1;

printf("\nThe array elements after deletion:\n");


for (i = 0; i < n; i++)
{
printf("LA[%d] = %d\n", i, LA[i]);
}

return 0;
}

3. Update Operation

#include <stdio.h>

int main()
{
int LA[] = {1, 3, 5, 7, 8};
int k = 3;
int item = 10;
int n = 5;
int i;

printf("The original array elements are:\n");


for (i = 0; i < n; i++)
{
printf("LA[%d] = %d\n", i, LA[i]);
}

LA[k - 1] = item;

printf("\nThe array elements after update:\n");


for (i = 0; i < n; i++)
{
printf("LA[%d] = %d\n", i, LA[i]);
}

return 0;
}

4. Search Operation

#include <stdio.h>

int main()
{
int LA[] = {1, 3, 5, 7, 8};
int item = 5;
int n = 5;
int i, j = 0;

printf("The original array elements are:\n");


for (i = 0; i < n; i++)
{
printf("LA[%d] = %d\n", i, LA[i]);
}

while (j < n)
{
if (LA[j] == item)
{
break;
}
j = j + 1;
}

if (j < n)
{
printf("\nFound element %d at position %d\n", item, j + 1);
}
else
{
printf("\nElement %d not found in the array\n", item);
}

return 0;
}

You might also like