0% found this document useful (0 votes)
8 views3 pages

Program 1

Uploaded by

Akash Baghel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Program 1

Uploaded by

Akash Baghel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program 1: Write a program to perform basic operations on 1D array.

#include<stdio.h>
#include<stdlib.h>

//Creating a 1d array....

void create(int ar[],int n)


{
int i;
printf("The elements of array are:\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
}

//Printing a 1d array....

void print(int ar[],int n)


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

//Copying an array....

void copy(int ar[],int n)


{
int b[10],i;
for(i=0;i<n;i++)
{
b[i]=ar[i];
}
printf("Copied array become\n");
print(b,n);
}

//Deleting an element....

void delete(int ar[],int n)


{
int loc,i;
printf("\nEnter the location:");
scanf("%d",&loc);
for(i=loc-1;i<n;i++)
{
ar[i]=ar[i+1];
}
printf("\nArray after deletion:\n ");
print(ar,n);
}

//Inserting an element.....

void insert(int ar[],int n)


{
int i,x,loc;
printf("Enter the element:\n");
scanf("%d",&x);
printf("Enter the location\n");
scanf("%d",&loc);
for(i=n;i>=loc-1;i--)
{
ar[i]=ar[i-1];
}
ar[loc-1]=x;
n=n+1;
printf("\nArray after insertion\n");
print(ar,n);
}

//Merging two sorted arrays....

void merge(int ar[],int b[],int x,int y)


{
int i,j,k,c[20];
i=j=k=0;
while(i<x&&j<y)
{
if(ar[i]<=b[j])
{
c[k]=ar[i];
i++;
k++;
}
else
{
c[k]=b[j];
j++;
k++;
}
}
if(i>=x)
{
while(j<y)
{
c[k]=b[j];
j++;
k++;
}
}
for(i=0;i<x+y;i++)
{
printf("%d\t",c[i]);
}
}
void main()
{
int ch,a[10],x;
printf("Enter number of elements:\n");
scanf("%d",&x);
create(a,x);
printf("The array is\n");
print(a,x);
while(1)
{
printf("\nEnter your choice\n1.Copy\n2.Delete\n3.Insert\n4.Merge\n5.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
copy(a,x);
break;
case 2:
delete(a,x);
break;
case 3:
insert(a,x);
break;
case 4:
int b[10],y;
printf("Enter number of elements for another array\n");
scanf("%d",&y);
create(b,y);
printf("Another array is\n");
print(b,y);
printf("\nThe merged array is\n");
merge(a,b,x,y);
break;
case 5:
exit(0);
break;
}
}

You might also like