0% found this document useful (0 votes)
39 views9 pages

Array & Matrix Stage-2

The document contains 6 programming problems related to arrays. The problems cover inserting and deleting elements from arrays, counting duplicates, removing duplicates, searching arrays, and sorting arrays in ascending or descending order using bubble sort.

Uploaded by

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

Array & Matrix Stage-2

The document contains 6 programming problems related to arrays. The problems cover inserting and deleting elements from arrays, counting duplicates, removing duplicates, searching arrays, and sorting arrays in ascending or descending order using bubble sort.

Uploaded by

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

Array & Matrix Stage-2

1.Write a program to insert an new element in an array at the mentioned


position and also update a size of an array.

#include <stdio.h>

int main()
{

int i, x, pos, n;
printf("Enter size of the array : ");
scanf("%d", &n);
int a[n];
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
x = 50;
pos = 5;
n++;
for (i = n-1; i >= pos; i--)
a[i] = a[i - 1];
a[pos - 1] = x;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");

2.Write a program to delete an element from an array at specified position.

#include <stdio.h>

int main()
{
int i, x, pos, n;
printf("Enter size of the array : ");
scanf("%d", &n);
int a[n];
printf("Enter elements in array : ");

for(i=0; i<n; i++)


{
scanf("%d", &a[i]);
}
printf("Enter the element position to delete : ");
scanf("%d", &pos);
if(pos < 0 || pos > n)
{
printf("position wrong");
}
else
{

for(i=pos-1; i<n-1; i++)


{
a[i] = a[i + 1];
}

n--;
}
printf("\nElements of array after delete are : ");
for(i=0; i<n; i++)
{
printf("%d\t", a[i]);
}
}

3.Write a program to count the total number of duplicate elements in an


array.

#include<stdio.h>
int main()
{
int i, n,j,count =0;
printf("Enter size of the array : ");
scanf("%d", &n);
int a[n];
printf("Enter elements in array : ");

for(i=0; i<n; i++)


{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if(a[i]==a[j+1])
{
count++;
}
}
}
printf("\n number of duplicates %d",count);
}

4. Write a program to delete all duplicate elements from an array.

2 #include<sedio.h>
3. int main()
art
5 int 4, n,j,count =0:
6 printf("Enter size of the array : *
7 scanf("%d", Sn);
8 int an];
9 printf("Enter elements in array : ")
10
" for(i=0; isn;
ot
13 scanf("%d", Bal i]);
14 y
15
16 for(i=0;i<n;i--)
at
18 for(j=i;j<n-1:-+)
19° {
20 if(alil==afj-11)
ai {
22 alj-1]-0)
2B ?
24 >
25 y
26 for (4=0;i<n;i--)
27 i
28 printf(*\n array of %d\t = é",,afi1):
29 y
5.Write a program to search an element in an array

#include<stdio.h>
int main()
{
int i, n,j,value;
printf("Enter size of the array : ");
scanf("%d", &n);
int a[n];
printf("Enter value to be search : ");
scanf("\n%d", &value);
printf("Enter elements in array : ");

for(i=0; i<n; i++)


{
scanf("%d", &a[i]);
}

for(i=0;i<n;i++)
{
if(a[i]==value )
printf("\n array index value = %d",i);

}
}
6. Write a program to sort array elements in ascending or descending
order.
Bubble sort
#include<stdio.h>
int main()
{
int n, i, j, swap;
printf("\nEnter number of elements");
scanf("%d", &n);
int a[n];
printf("Enter %d Numbers:", n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0 ; i < n - 1; i++)
{
for(j = 0 ; j < n-i-1; j++)
{
if(a[j] > a[j+1])
{
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;
}
}
}
printf("Sorted Array:");
for(i = 0; i < n; i++)
printf("\n%d", a[i]);
return 0;
}

You might also like