Blog Details
#include<stdio.h>
#define SIZE 10
// Function to print an array
void printArray( int arr[] )
{
int i;
for(i=0; i<SIZE; i++)
{
printf("%d\t", arr[i]);
}
}
// Function to insert an array
void insertArray( int elm, int size, int pos, int arr[] )
{
int i;
for(i=size-1; i>=pos; i--)
{
arr[i+1] = arr[i];
}
arr[pos] = elm;
size++;
}
// function to get the array from user
void getArray(int arr[])
{
int i;
for(i=0; i<SIZE; i++)
{
printf("Enter the element of array %d:", (i+1));
scanf("%d", &arr[i]);
}
}
// Function to find the frequency of every element
void findFrequency(int arr[], int n) {
int freq[n];
int visited[n];
// Initialize freq and visited arrays
for (int i = 0; i < n; i++) {
freq[i] = 0;
visited[i] = 0;
}
// Calculate frequency
for (int i = 0; i < n; i++) {
if (visited[i] == 1)
continue;
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = 1;
}
}
freq[i] = count;
}
// Print frequency
printf("Element | Frequency\n");
for (int i = 0; i < n; i++) {
if (visited[i] == 0) {
printf("%d | %d \n", arr[i], freq[i]);
}
}
}
void main()
{
// int arr[SIZE] = {10, 20, 30, 40, 50, 60, 60, 70, 80, 90, 90};
int arr[SIZE];
int n = sizeof(arr) / sizeof(arr[0]);
int choice, pos, elm;
// insertArray(77, SIZE, 5, arr);
getArray(arr); // Get the array from user
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the position where to insert: ");
scanf("%d", &pos);
printf("Enter the element to insert: ");
scanf("%d", &elm);
insertArray(elm, SIZE, pos, arr);
printf("Done! inserted the element.");
break;
default:
printf("invalid Input!");
break;
}
findFrequency(arr, n);
// Now print the array using printArray function
printArray(arr);
}