OPERATIONS ON ARRAYS:
There are a number of operations that can be preformed on arrays. These operations include:
- Traversing an array
- Inserting an element in an array
- Searching an element in an array
- Deleting an element from an array
- Merging two arrays
- Sorting an array in ascending or descending order
- Traversing an array:
Traversing the data elements of an array A can include printing every element, counting the total number of elements, or performing any process on these elements.
Algorithm for array traversal
Programming Examples:
Inserting an Element in an Array:
Algorithm to Insert an Element in the Middle of an Array
The algorithm INSERT will be declared as INSERT (A, N, POS, VAL). The arguments are
(a) A, the array in which the element has to be inserted
(b) N, the number of elements in the array
(c) POS, the position at which the element has to be inserted
(d) VAL, the value that has to be inserted
#include <stdio.h>
void main()
{
int array[100];
int i, n, x, pos;
printf("Enter the number of elements in the array \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{scanf("%d", &array[i]);}
printf("Input array elements are: \n");
for (i = 0; i < n; i++)
{printf("%d ", array[i]);}
printf("\nEnter the new element to be inserted: ");
scanf("%d", &x);
printf("Enter the position where element is to be inserted: ");
scanf("%d", &pos);
//shift all elements 1 position forward from the place
//where element needs to be inserted
n=n+1;
for(i = n-1; i >= pos; i--)
array[i]=array[i-1];array[pos-1]=x;
//Insert the element x on the specified position
//print the new array
for (i = 0; i < n; i++)
{printf("%d ", array[i]);}
}
Algorithm to delete an element from the middle of
an array
The algorithm DELETE will be declared as DELETE(A, N, POS). The arguments are:
(a) A, the array from which the element has to be deleted
(b) N, the number of elements in the array
(c) POS, the position from which the element has to be deleted
1. Create an array of some size, and fill up its elements.
2. Take a value as input from users, which needs to be deleted.
3. Using for loop, check whether the value is present in the array or not.
4. If the value is present, then save its location and if its not present, some appropriate message get printed.
5. Again, the loop runs from that saved position till the end of array, causing each element to shift left by one step. This, way the value get deleted.
6. Print the array.
2. Take a value as input from users, which needs to be deleted.
3. Using for loop, check whether the value is present in the array or not.
4. If the value is present, then save its location and if its not present, some appropriate message get printed.
5. Again, the loop runs from that saved position till the end of array, causing each element to shift left by one step. This, way the value get deleted.
6. Print the array.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, n, index, arr[10];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (i = 0; i < n; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
if (index >= n+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
for (i = index; i < n - 1; i++)
arr[i] = arr[i + 1];
printf("The array after deleting the element is: ");
for (i = 0; i < n - 1; i++)
printf("%d ", arr[i]);
return 0;
}
}
1. Write a program to read and display n numbers using an array
#include <stdio.h>
int main()
{
int i, n, arr[20];printf("\n Enter the number of elements in the array : ");scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);return 0;
}
2. Write a program to find the mean of n numbers using arrays.
#include <stdio.h>
int main()
{
int i, n, arr[20], sum =0;float mean = 0.0;printf("\n Enter the number of elements in the array : ");scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
sum += arr[i];mean = (float)sum/n;printf("\n The sum of the array elements = %d", sum);printf("\n The mean of the array elements = %.2f", mean);return 0;
}
3. Write a program to print the position of the smallest number of n numbers using arrays.
#include <stdio.h>
int main()
{
int i, n, arr[20], small, pos;
printf("\n Enter the number of elements in the array : ");scanf("%d", &n);printf("\n Enter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);small = arr[0];pos =0;
for(i=1;i<n;i++)
{
if(arr[i]<small)
{
small = arr[i];pos = i;
}
printf("\n The smallest element is : %d", small);}
printf("\n The position of the smallest element in the array is : %d", pos);
return 0;
}
4. Write a program to find the second largest of n numbers using an array.
#include <stdio.h>
int main()
{
int i, n, arr[20], large, second_large;printf("\n Enter the number of elements in the array : ");scanf("%d", &n);printf("\n Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);large = arr[0];
for(i=1;i<n;i++)
{
if(arr[i]>large)large = arr[i];
}
second_large = arr[1];
for(i=0;i<n;i++)
{
if(arr[i] != large)
{
if(arr[i]>second_large)second_large = arr[i];
}
}
printf("\n The numbers you entered are : ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);printf("\n The largest of these numbers is : %d",large);printf("\n The second largest of these numbers is : %d",second_large);return 0;
}
5. Write a program to enter n number of digits. Form a number using these digits.
#include <stdio.h>
#include <math.h>
int main()
{
int number=0, digit[10], numofdigits,i;
printf("\n Enter the number of digits : ");scanf("%d", &numofdigits);
for(i=0;i<numofdigits;i++)
{
printf("\n Enter the digit at position %d:", i+1);scanf("%d", &digit[i]);
}
i=0;
while(i<numofdigits)
{number = number + digit[i] * pow(10,i);i++;}
printf("\n The number is : %d", number);
return 0;
}
6. Write a program to find whether the array of integers contains a duplicate number.
#include <stdio.h>
#include <math.h>
int main()
{
int array[10], i, n, j, flag =0;
printf("\n Enter the size of the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n array[%d] = ", i);
scanf("%d", &array[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(array[i] == array[j] && i!=j)
{
flag =1;
printf("\n Duplicate numbers found at locations %d and %d", i, j);
}
}
}
if(flag==0)
printf("\n No Duplicates Found");
return 0;
}
No comments:
Post a Comment