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

Array Operation Programs

The document contains 5 C programs related to arrays: 1) deleting an element from a specified location, 2) deleting duplicate elements, 3) inserting an element, 4) searching for an element, 5) copying all elements from one array to another.

Uploaded by

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

Array Operation Programs

The document contains 5 C programs related to arrays: 1) deleting an element from a specified location, 2) deleting duplicate elements, 3) inserting an element, 4) searching for an element, 5) copying all elements from one array to another.

Uploaded by

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

1.

C Program for deletion of an element from the specified location from an Array

#include<stdio.h>
int main()
{
int arr[30], num, i, loc;
printf("\nEnter no of elements:");
scanf("%d", &num);
//Read elements in an array
printf("\nEnter %d elements :", num);
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
//Read the location
printf("\nLocation of the element to be deleted :");
scanf("%d", &loc);
/* loop for the deletion */
while(loc < num)
{
arr[loc-1] = arr[loc];
loc++;
}
num--; // No of elements reduced by 1
//Print Array
for (i = 0; i < num; i++)
{
printf("\n %d", arr[i]);
}
return (0);
}

Output: Enter no of elements:3

Enter 3 elements :5
6
7

Location of the element to be deleted :2

5
7

2. C Program to delete duplicate elements from an array

#include<stdio.h>
int main()
{
int arr[20], i, j, k, size;
printf("\nEnter array size: ");
scanf("%d", &size);
printf("\nAccept Numbers: ");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nArray with Unique list: ");
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size;)
{
if (arr[j] == arr[i])
{
for (k = j; k < size; k++)
{
arr[k] = arr[k + 1];
}
size--;
}
else
j++;
}
}
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return (0);
}

3. C Program to insert an element in an array


#include<stdio.h>
int main() {
int arr[30], element, num, i, location;
printf("\nEnter no of elements:");
scanf("%d", &num);
for (i = 0; i < num; i++) {
scanf("%d", &arr[i]); }
printf("\nEnter the element to be inserted:");
scanf("%d", &element);
printf("\nEnter the location");
scanf("%d", &location);
//Create space at the specified location
for (i = num; i >= location; i--)
{
arr[i] = arr[i - 1];
}
num++;
arr[location - 1] = element;
//Print out the result of insertion
for (i = 0; i < num; i++)
printf("\n %d", arr[i]);
return (0);
}

4. C Program to search an element in an array


#include<stdio.h>
int main()
{
int a[30], ele, num, i;
printf("\nEnter no of elements:");
scanf("%d", &num);
printf("\nEnter the values :");
for (i = 0; i < num; i++)
{
scanf("%d", &a[i]);
}
//Read the element to be searched
printf("\nEnter the elements to be searched :");
scanf("%d", &ele);
//Search starts from the zeroth location
i = 0;
while (i < num && ele != a[i])
{
i++;
}
//If i < num then Match found
if (i < num)
{
printf("Number found at the location = %d", i + 1);
}
else
{
printf("Number not found");
}
return (0);
}

//5. C Program to copy all elements of an array into another array


#include<stdio.h>
int main() {
int arr1[30], arr2[30], i, num;
printf("\nEnter no of elements:");
scanf("%d", &num);
//Accepting values into Array
printf("\nEnter the values:");
for (i = 0; i < num; i++)
{
scanf("%d", &arr1[i]);
}
/* Copying data from array 'a' to array 'b */
for (i = 0; i < num; i++)
{
arr2[i] = arr1[i];
}
//Printing of all elements of array
printf("The copied array is:");
for (i = 0; i < num; i++)
printf("\narr2[%d] = %d", i, arr2[i]);
return (0);
}

You might also like