C Program to perform all the operations on a fixed size Array

 


// Program by Akash Tripathi (@proakash256)

#include <stdio.h>
#define max 100
int n;
char a = '0'd = '0';
int ar[max];
void Display()
{
    printf("\nThe Elements are : \n\n");
    for (int i = 0i < ni = i + 1)
        printf("%d -> %d\n", (i + 1), ar[i]);
    return;
}
void Delete()
{
    printf("\nEnter the Position of the Element
             you want to Delete : \n");
    unsigned short a;
    scanf("%hu", &a);
    a = a - 1;
    n = n - 1;
    for (int i = ai < ni = i + 1)
        ar[i] = ar[i + 1];
    a = '0';
    d = '0';
    Display();
    return;
}
void Insert()
{
    printf("\nEnter the Position at which
             you want to Insert the Element : \n");
    unsigned short a;
    scanf("%hu", &a);
    printf("Enter the Element : \n");
    int b;
    scanf("%d", &b);
    a = a - 1;
    n = n + 1;
    for (int i = n - 2i >= ai = i - 1)
        ar[i + 1] = ar[i];
    ar[a] = b;
    a = '0';
    d = '0';
    Display();
    return;
}
void Bubble()
{
start:
    printf("\nEnter 1 to Sort in Ascending Order\n");
    printf("Enter 2 to Sort in Descending Order\n");
    printf("Enter your Choice : ");
    char c;
    scanf(" %c", &c);
    if (c == '1')
    {
        for (int i = 0i < (n - 1); i = i + 1)
        {
            for (int j = 0j < (n - 1 - i); j = j + 1)
            {
                if (ar[j] > ar[j + 1])
                {
                    ar[j] = ar[j] + ar[j + 1];
                    ar[j + 1] = ar[j] - ar[j + 1];
                    ar[j] = ar[j] - ar[j + 1];
                }
            }
        }
        a = '1';
        d = '0';
    }
    else if (c == '2')
    {
        for (int i = 0i < (n - 1); i = i + 1)
        {
            for (int j = 0j < (n - 1 - i); j = j + 1)
            {
                if (ar[j] < ar[j + 1])
                {
                    ar[j] = ar[j] + ar[j + 1];
                    ar[j + 1] = ar[j] - ar[j + 1];
                    ar[j] = ar[j] - ar[j + 1];
                }
            }
        }
        d = '1';
        a = '0';
    }
    else
    {
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
    }
    Display();
    return;
}
void Selection()
{
start:
    printf("\nEnter 1 to Sort in Ascending Order\n");
    printf("Enter 2 to Sort in Descending Order\n");
    printf("Enter your Choice : ");
    char c;
    scanf(" %c", &c);
    if (c == '1')
    {
        for (int i = 0i < (n - 1); i = i + 1)
        {
            int smallplacetemp;
            small = ar[i];
            place = i;
            for (int j = i + 1j < nj = j + 1)
            {
                if (ar[j] < small)
                {
                    small = ar[j];
                    place = j;
                }
            }
            temp = ar[i];
            ar[i] = ar[place];
            ar[place] = temp;
        }
        a = '1';
        d = '0';
    }
    else if (c == '2')
    {
        for (int i = 0i < (n - 1); i = i + 1)
        {
            int temp;
            int great = ar[i];
            int place = i;
            for (int j = i + 1j < nj = j + 1)
            {
                if (ar[j] > great)
                {
                    great = ar[j];
                    place = j;
                }
            }
            temp = ar[i];
            ar[i] = ar[place];
            ar[place] = temp;
        }
        d = '1';
        a = '0';
    }
    else
    {
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
    }
    Display();
    return;
}
void Insertion()
{
start:
    printf("\nEnter 1 to Sort in Ascending Order\n");
    printf("Enter 2 to Sort in Descending Order\n");
    printf("Enter your Choice : ");
    char c;
    scanf(" %c", &c);
   int key , i , j;
    if (c == '1')
    {
for(i = 1i < ni = i + 1)
        {
            key = ar[i];
            j = i - 1;
            while(j >= 0 && ar[j] > key)
            {
                ar[j + 1] = ar[j];
                j = j - 1;
            }
            ar[j + 1] = key;
        }
        a = '1';
        d = '0';
    }
    else if (c == '2')
    {
for(i = 1i < ni = i + 1)
        {
            key = ar[i];
            j = i - 1;
            while(j >= 0 && ar[j] < key)
            {
                ar[j + 1] = ar[j];
                j = j - 1;
            }
            ar[j + 1] = key;
        }
 
        d = '1';
        a = '0';
    }
    else
    {
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
    }
    Display();
    return;
}
void Sort()
{
start:
    printf("\nEnter 1 to Sort using Bubble Sort\n");
    printf("Enter 2 to Sort using Selection Sort\n");
    printf("Enter 3 to Sort using Insertion Sort\n");
    printf("Enter your Choice : ");
    char c;
    scanf(" %c", &c);
    switch (c)
    {
    case '1':
        Bubble();
        break;
    case '2':
        Selection();
        break;
    case '3':
        Insertion();
        break;
    default:
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
        break;
    }
    return;
}
void Linear()
{
    int sposfound = 0;
    printf("\nEnter the element that you want
             to search in the array : ");
    scanf("%d", &s);
    for (int i = 0i < ni = i + 1)
        if (ar[i] == s)
        {
            pos = i;
            found = 1;
            break;
        }
    if (found == 1)
        printf("\nThe element %d is at position
                 %d\n\n"s, (pos + 1));
    else
        printf("\nValue Not Found.\n\n");
    return;
}
void Binary()
{
start:
    if (a == '0' && d == '0')
    {
        printf("\nFor Binary Search to work , ");
        printf("the array needs to be sorted.\n");
        printf("Enter 1 to sort the array : ");
        char ch;
        scanf(" %c", &ch);
        switch (ch)
        {
        case '1':
            Sort();
            break;
        default:
            printf("Sorry Wrong Choice...Enter Again.\n");
            goto start;
            break;
        }
    }
    if (a == '1' && d == '0')
    {
        int start = 0last = (n - 1)
int middlefound = 0spos;
        printf("\nEnter the element that
                 you want to search in the array : ");
        scanf("%d", &s);
        while (start <= last)
        {
            middle = (start + last) / 2;
            if (ar[middle] == s)
            {
                pos = middle;
                found = 1;
                break;
            }
            else if (s > ar[middle])
                start = middle + 1;
            else if (s < ar[middle])
                last = middle - 1;
        }
        if (found == 1)
            printf("\nThe element %d is at position
                     %d\n\n"s, (pos + 1));
        else
        {
            printf("\nValue Not Found.\n\n");
        }
    }
    else if (d == '1' && a == '0')
    {
        int start = 0last = (n - 1);
        int middlefound = 0spos;
        printf("\nEnter the element that you want
                 to search in the array : ");
        scanf("%d", &s);
        while (start <= last)
        {
            middle = (start + last) / 2;
            if (ar[middle] == s)
            {
                pos = middle;
                found = 1;
                break;
            }
            else if (s > ar[middle])
                last = middle - 1;
            else if (s < ar[middle])
                start = middle + 1;
        }
        if (found == 1)
            printf("\nThe element %d is at
                     position %d\n\n"s, (pos + 1));
        else
            printf("\nValue Not Found.\n\n");
    }
    return;
}
void Search()
{
start:
    printf("\nEnter 1 to Search using Linear Search\n");
    printf("Enter 2 to Search using Binary Search\n\n");
    printf("Enter your Choice : ");
    char c;
    scanf(" %c", &c);
    switch (c)
    {
    case '1':
        Linear();
        break;
    case '2':
        Binary();
        break;
    default:
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
        break;
    }
    return;
}
void Rotate()
{
start:
    printf("\nEnter the Number of Times you want
             to Rotate the Elements : ");
    int t;
    scanf("%d", &t);
    t = t % n;
    printf("\nEnter 1 to Rotate to Left (Up) Side\n");
    printf("Enter 2 to Rotate to Right (Down) Side\n");
    printf("Enter your Choice : ");
    char c;
    scanf(" %c", &c);
    if (c == '1')
    {
        for (int i = 1i <= ti = i + 1)
        {
            int temp = ar[0];
            for (int j = 0j < (n - 1); j = j + 1)
                ar[j] = ar[j + 1];
            ar[n - 1] = temp;
        }
    }
    else if (c == '2')
    {
        for (int i = 1i <= ti = i + 1)
        {
            int temp = ar[n - 1];
            for (int j = (n - 2); j >= 0j = j - 1)
                ar[j + 1] = ar[j];
            ar[0] = temp;
        }
    }
    else
    {
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
    }
    a = '0';
    d = '0';
    Display();
    return;
}
void Reverse()
{
    if (a == '1' || d == '1')
    {
        for (int i = 0i < (n / 2); i = i + 1)
        {
            ar[i] = ar[i] + ar[n - 1 - i];
            ar[n - 1 - i] = ar[i] - ar[n - 1 - i];
            ar[i] = ar[i] - ar[n - 1 - i];
        }
    }
    else
    {
        for (int i = 0i < (n / 2); i = i + 1)
        {
            ar[i] = ar[i] + ar[n - 1 - i];
            ar[n - 1 - i] = ar[i] - ar[n - 1 - i];
            ar[i] = ar[i] - ar[n - 1 - i];
        }
        a = '0';
        d = '0';
    }
    Display();
    return;
}
void Distinct()
{
    int d = 0;
    for (int i = 0i < ni = i + 1)
    {
        int j;
        for (j = 0j < ij = j + 1)
        {
            if (ar[j] == ar[i])
            {
                break;
            }
        }
        if (i == j)
            d = d + 1;
    }
    printf("\nThe number of distinct
             Numbers is %d\n\n"d);
    return;
}
void Most_Frequent()
{
start:
    printf("\nEnter 1 to find by a Simple method using two Loops\n");
    printf("Enter 2 to find by a Complex Method br Sorting\n");
    printf("Enter your Choice : ");
    char c;
    scanf(" %c", &c);
    int maximum = 0index = 0count = 0;
    if (c == '1')
    {
        for (int i = 0i < ni = i + 1)
        {
            count = 0;
            for (int j = 0j < nj = j + 1)
            {
                if (ar[j] == ar[i])
                    count = count + 1;
            }
            if (count > maximum)
            {
                maximum = count;
                index = i;
            }
        }
        printf("\nThe Number %d appears maximum
                 %d times\n\n"ar[index], maximum);
    }
    else if (c == '2')
    { 
        count = 1;
        maximum = 1;
        int t[n];
        for (int i = 0i < ni = i + 1)
            t[i] = ar[i];
        for (int i = 0i < (n - 1); i = i + 1)
        {
            for (int j = 0j < (n - 1 - i); j = j + 1)
            {
                if (t[j] > t[j + 1])
                {
                    t[j] = t[j] + t[j + 1];
                    t[j + 1] = t[j] - t[j + 1];
                    t[j] = t[j] - t[j + 1];
                }
            }
        }
        for (int i = 1i < ni = i + 1)
        {
            if (t[i] == t[i - 1])
                count = count + 1;
            else
            {
                if (count > maximum)
                {
                    maximum = count;
                    index = i - 1;
                }
                count = 1;
            }
        }
        if (count > maximum)
        {
            maximum = count;
            index = n - 1;
        }
        printf("\nThe Number %d appears maximum
                %d times\n\n"t[index], maximum);
    }
    else
    {
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
    }
    return;
}
void Frequency()
{
    int d = 1count = 0c = 0;
    printf("\nFrequency of each element :\n");
    for (int i = 1i < ni = i + 1)
    {
        int j;
        for (j = 0j < ij = j + 1)
        {
            if (ar[j] == ar[i])
            {
                break;
            }
        }
        if (i == j)
            d = d + 1;
    }
    int temp1[d];
    int temp2[d];
    for (int i = 0i < ni = i + 1)
    {
        int j;
        for (j = 0j < ij = j + 1)
        {
            if (ar[j] == ar[i])
            {
                break;
            }
        }
        if (i == j)
        {
            temp1[c] = ar[i];
            c = c + 1;
        }
    }
    count = 0;
    for (int i = 0i < di = i + 1)
    {
        count = 0;
        for (int j = 0j < nj = j + 1)
        {
            if (temp1[i] == ar[j])
            {
                count = count + 1;
            }
        }
        temp2[i] = count;
        printf("%d --> %d\n"temp1[i], temp2[i]);
    }
    return;
}
int main()
{
    printf("Enter the number of elements you want to enter : ");
    scanf("%d", &n);
    printf("\n");
    printf("Enter the Elements :\n");
    for (int i = 0i < ni = i + 1)
        scanf("%d", &ar[i]);
start:
    printf("\n");
    printf("Enter 1 to Delete an Element\n");
    printf("Enter 2 to Insert an Element\n");
    printf("Enter 3 to Sort the Elements\n");
    printf("Enter 4 to Search an Element\n");
    printf("Enter 5 to Display the Elements\n");
    printf("Enter 6 to Rotate the Elements\n");
    printf("Enter 7 to Reverse the Elements\n");
    printf("Enter 8 to print the number of Distinct Elements\n");
    printf("Enter 9 to print the most frequent Element\n");
    printf("Enter 10 to print frequency of each Element\n\n");
    printf("Enter 0 to Exit\n\n");
    printf("Enter your Choice : ");
    unsigned short c;
    scanf("%hu", &c);
    switch (c)
    {
    case 1:
        Delete();
        goto start;
        break;
    case 2:
        Insert();
        goto start;
        break;
    case 3:
        Sort();
        goto start;
        break;
    case 4:
        Search();
        goto start;
        break;
    case 5:
        Display();
        goto start;
        break;
    case 6:
        Rotate();
        goto start;
        break;
    case 7:
        Reverse();
        goto start;
        break;
    case 8:
        Distinct();
        goto start;
        break;
    case 9:
        Most_Frequent();
        goto start;
        break;
    case 10:
        Frequency();
        goto start;
        break;
    case 0:
        goto end;
        break;
    default:
        printf("Sorry Wrong Choice...Enter Again.\n");
        goto start;
        break;
    }
end:
    return 0;
}

Comments