0% found this document useful (0 votes)
6 views8 pages

Lab 2

The document discusses creating 1-D integer arrays in C and performing search and deletion operations on the arrays. It includes code snippets to create arrays with both unique and repeating elements, and functions to search for and delete specific values from the arrays.
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)
6 views8 pages

Lab 2

The document discusses creating 1-D integer arrays in C and performing search and deletion operations on the arrays. It includes code snippets to create arrays with both unique and repeating elements, and functions to search for and delete specific values from the arrays.
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/ 8

1.

Write a program to create a 1-D array of unique Integers and perform the following

a) Search a particular key value

b) Delete a particular key value

CODE:

#include<stdio.h>
#include<time.h>
#define size 100

int search_in_array(int arr[],int n,int key){


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

void delete_in_arry(int arr[],int n,int key){


int i;
for(i=0;i<n;i++){
if(arr[i]==key){
break;
}
}
for(i;i<n;i++){
arr[i]=arr[i+1];
}
}

int main(){
clock_t start=clock();

int n;
printf("Enter the size of the array :");
scanf("%d",&n);

int arr[100];
for(int i=0;i<n;i++){
printf("Enter element %d :",i+1);
scanf("%d",&arr[i]);
}
//undeclared elements
for(int i=n;i<size;i++){
arr[i]=0;
}
int key;
printf("enter the element to be searched :");
scanf("%d",&key);

int idx=search_in_array(arr,n,key);
if(idx!=-1){
printf("%d is found at index %d in the array\n",key,idx);
}
else{
printf("%d is not found in the array\n");
}

int delkey;
printf("Enter the number to be deleted : ");
scanf("%d",&delkey);
delete_in_arry(arr,n,delkey);
n--;

//array after deletion


printf("Array after deletion : ");
for(int i=0;i<n;i++){
printf("%d\t",arr[i]);
}
clock_t end=clock();
double executiontime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\nexection time : %f",executiontime);
return 0;
}
OUTPUT:
2. Write a program to create a 1-D array of Integers (containing repetition) and perform the following

a) Search a particular key value

b) Delete a particular key value

CODE:

#include<stdio.h>
#include<time.h>
#define size 100

void search_in_array(int arr[],int n,int key){


int found=0;
for(int i=0;i<n;i++){
if(arr[i]==key){
found++;
if(found==1){
printf("%d found at index: ",key);
}
printf(" %d",i);
}
}
printf("\n");
if(found==0){
printf("%d is not found in the array\n",key);
}
}

int delete_in_array(int arr[], int n, int key) {


int newsize = n;

for (int i = 0; i < newsize; i++) {


if (arr[i] == key) {
for (int j = i; j < newsize - 1; j++) {
arr[j] = arr[j + 1];
}
newsize--;
i--; // Recheck the current position after deletion
}
}

return newsize;
}

int main(){
clock_t start=clock();
int n;
printf("Enter the size of the array :");
scanf("%d",&n);

int arr[100];
for(int i=0;i<n;i++){
printf("Enter element %d :",i+1);
scanf("%d",&arr[i]);
}
//undeclared elements
for(int i=n;i<size;i++){
arr[i]=0;
}

int key;
printf("enter the element to be searched :");
scanf("%d",&key);

search_in_array(arr,n,key);

int delkey;
printf("Enter the number to be deleted : ");
scanf("%d",&delkey);
n=delete_in_array(arr,n,delkey);

//array after deletion


printf("Array after deletion : ");
for(int i=0;i<n;i++){
printf("%d\t",arr[i]);
}
clock_t end=clock();
double executiontime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\nexection time : %f",executiontime);
return 0;
}
OUTPUT:

You might also like