[go: up one dir, main page]

0% found this document useful (0 votes)
12 views10 pages

Lab 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

1. Write a program to find max and min elements in a 1-D array.

CODE:

#include<stdio.h>
#include<time.h>

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

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

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

int maxelement=arr[0],minelement=arr[0];

for(int i=0;i<n;i++){
if(arr[i]>maxelement){
maxelement=arr[i];
}
if(arr[i]<minelement){
minelement=arr[i];
}
}

printf("Maximum Element in the Array is : %d\n",maxelement);


printf("Minimum Element in the Array is : %d\n",minelement);
clock_t end=clock();
double executiontime=(double)(end-start)/CLOCKS_PER_SEC;
printf("exection time : %f",executiontime);
return 0;
}
OUTPUT:
2. Write a program to create a 1-D Integer Array using dynamic memory allocation. Enter the values
of Array elements using the keyboard. Perform the following operations on it:

a) Traverse the Array from first to last.

b) Traverse the Array from last to first.

c) Search a particular number in the Array.

CODE:

#include<stdio.h>
#include<time.h>

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;
}

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

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

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

//traversal from first to last


printf("\ntraversal from first to last : ");
for(int i=0;i<n;i++){
printf("%d\t",arr[i]);
}

//traversal from last to first


printf("\ntraversal from last to first : ");
for(int i=n-1;i>=0;i--){
printf("%d\t",arr[i]);
}

//search for an element


int key;
printf("\n\nEnter 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\n",key,idx);
}
else{
printf("Element not found in the array\n");
}
clock_t end=clock();
double executiontime=(double)(end-start)/CLOCKS_PER_SEC;
printf("exection time : %f",executiontime);
return 0;
}
OUTPUT:
3. Write a program to create a 1-D Integer array. Perform the following operations on it:

a) Insert an element at a given position

b) Delete an element present at a given position.

CODE:

#include<stdio.h>
#include<time.h>
#define size 100
//insert an element at a index in array
void insert(int arr[],int n,int idx,int key){
if(idx<0 && idx<size){
printf("given wrong index to be inserted\n");
return;
}
for(int i=n;i>idx;i--){
arr[i]=arr[i-1];
}
arr[idx]=key;
}

//delete the element in array

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 idx,key;
printf("Enter the number to be inserted : ");
scanf("%d",&key);
printf("Enter the index where the element should be inserted :");
scanf("%d",&idx);
insert(arr,n,idx,key);

if(n<idx){
n=idx;
}
n++;
printf("Array after insertion : ");
for(int i=0;i<n;i++){
printf("%d\t",arr[i]);
}
printf("\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:

You might also like