0% found this document useful (0 votes)
40 views

1d Array

The document contains C code snippets that demonstrate various operations on arrays: 1) Shifting elements left or right in the array. 2) Inserting an element into a specific position in the array. 3) Deleting an element from a specific position in the array. 4) Rotating the elements of the array left or right, including rotating by a specified number of positions. 5) Finding duplicate elements in an array.

Uploaded by

Avishek Rauniyar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

1d Array

The document contains C code snippets that demonstrate various operations on arrays: 1) Shifting elements left or right in the array. 2) Inserting an element into a specific position in the array. 3) Deleting an element from a specific position in the array. 4) Rotating the elements of the array left or right, including rotating by a specified number of positions. 5) Finding duplicate elements in an array.

Uploaded by

Avishek Rauniyar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Shift right

#include <stdio.h>

int main() {
int a[5];
for(int i=0;i<4;i++)
scanf("%d",&a[i]);
for(int i=4;i>0;i--)
a[i]=a[i-1];
printf("Right shift ");
for(int i=1;i<5;i++)
printf("%d ",a[i]);
return 0;
}

Shift left
#include <stdio.h>

int main() {
int a[5];
for(int i=1;i<5;i++)
scanf("%d",&a[i]);
for(int i=0;i<4;i++)
a[i]=a[i+1];
printf("Left shift ");
for(int i=0;i<4;i++)
printf("%d ",a[i]);
return 0;
}

Insert an element in position k;


#include <stdio.h>

int main() {
int a[5],c,g;
printf("Enter array ");
for(int i=0;i<4;i++)
scanf("%d",&a[i]);
printf("enter a index ");
scanf("%d",&c);
printf("Enter a value for index ");
scanf("%d",&g);
for(int i=0;i<c;i++)
{
a[i]=a[i];
}
for(int i=5;i>c;i--)
{
a[i]=a[i-1];
}
a[c]=g;
printf("New array ");
for(int i=0;i<5;i++)
printf("%d ",a[i]);
return 0;
}

Delete an element from the position

#include <stdio.h>

int main() {
int a[5],c,g;
printf("Enter array ");
for(int i=0;i<5;i++)
scanf("%d",&a[i]);
printf("enter a index ");
scanf("%d",&c);
for(int i=0;i<4;i++)
{
if(i<c)
a[i]=a[i];
else
a[i]=a[i+1];
}
printf("New array ");
for(int i=0;i<4;i++)
printf("%d ",a[i]);
return 0;
}
Rotate Left
#include <stdio.h>
//Left Rotate
int main() {
int a[5],c,g;
printf("Enter array ");
for(int i=0;i<4;i++)
scanf("%d",&a[i]);
c=a[0];
a[4]=c;
printf("New array ");
for(int i=1;i<5;i++)
printf("%d ",a[i]);
return 0;
}

Rotate Right
#include <stdio.h>
//Right Rotate
int main() {
int a[5],c,g;
printf("Enter array ");
for(int i=1;i<5;i++)
scanf("%d",&a[i]);
c=a[4];
a[0]=c;
printf("New array ");
for(int i=0;i<4;i++)
printf("%d ",a[i]);
return 0;
}

Rotate Right ‘k ‘ times


#include<stdio.h>
int main() {
int a[5],c,k;
printf("Enter array ");
for(int i=0;i<4;i++)
scanf("%d",&a[i]);
printf("Enter value of k ");
scanf("%d",&k);
for(int j=0;j<k;j++)
{
c=a[3];
a[3]=a[2];
a[2]=a[1];
a[1]=a[0];
a[0]=c;
}
printf("New array ");
for(int i=0;i<4;i++)
printf("%d ",a[i]);
return 0;
}
Find all duplicate elements in a array
#include<stdio.h>
int main() {
int k;
printf("Enter size of array ");
scanf("%d",&k);
int a[k];
printf("Enter array ");
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
for(int j=0;j<k;j++)
{
for(int i=j+1;i<k;i++)
{
if (a[j]==a[i])
{
printf("%d %d\n",a[j],a[i]);
}
}
}
return 0;
}

You might also like