To find the frequency distribution of the elements in the given integer array.

Tags

, , , , ,

//C- Programm to find the frequency of the elements in an integer array (Using 3-Way Quicksort to sort Input array)

#include <stdio.h>

void quickSort(int a[],int low, int high);
void partition(int a[],int low, int high, int *i, int *j);
void swap(int *a,int *b);
void printArry(int a[],int size);

int main(){
    int a[] = {4, 9, 4, 4, 1, 9, 4, 4, 9, 4, 4, 1, 4};

    int size = sizeof(a) / sizeof(int);
    printf("Input array: ");
    printArry(a,size);                       // To Print the Given array
    quickSort(a, 0, size - 1);               //To sort teh given array of elements
    printf("Sorted array: ");
    printArry(a,size);                       // To Print the Sorted array

    printf("----:Frequency Distribution of array elements:----\n");
    int count = 0;
    int num = a[0];
    int i;
    for (i = 0; i <= size; i++) {
        if (num != a[i]) {
            printf("\tElement %d occurs %d times.\n",num,count);
            num = a[i];
            count = 0;
            i--;
        }else
        {
            count++;
        }
    }
    return 0;
}

//Utility function to swap two numbers

void swap(int *a,int *b){

    int temp = *a;
    *a = *b;
    *b = temp;

}

//To partition the given array

void partition(int a[],int low, int high, int *i, int *j){

    if ((high-low) <= 1) {
        if (a[high] < a[low]) {
            swap(&a[high], &a[low]);
            *i = low;
            *j = high;
            return;
        }
    }

    int mid = low;
    int pivot = a[high];

    while (mid <= high) {
        if (a[mid] < pivot) {
            swap(&a[low++], &a[mid++]);
        }
        else if (a[mid] == pivot){
            mid++;
        }
        else if (a[mid] > pivot){
            swap(&a[mid], &a[high--]);
        }
    }

    *i = low - 1;
    *j = mid;

}

//Implementation of quick sort recursively

void quickSort(int a[],int low, int high){

    if (low >= high) {
        return;
    }

    int i,j;

    partition(a, low, high, &i, &j);

    quickSort(a, low, i);
    quickSort(a, j, high);

}

//To print the array contents

void printArry(int a[],int size)
{
    int i;
    for (i = 0; i < size; i++) {
        printf("\t%d",a[i]);
    }
    printf("\n");
}


SAMPLE INPUT/OUTPUT:

Input array:     4    9    4    4    1    9    4    4    9    4    4    1    4

Sorted array:     1    1    4    4    4    4    4    4    4    4    9    9    9

----:Frequency Distribution of array elements:----

    1 occurs 2 times

    4 occurs 8 times

    9 occurs 3 times

To Print n odd numbers

//C-Programm to print n-even numbers

#include <stdio.h>

int main() {

    

    int n=8;

    int count = 0;

    int i = 0;

    

    while (count < n){

        if (i%2 == 0){

            printf("%d ",i);

            ++i;

            ++count;

        }

        i++;

    }

    

    

    return 0;

}

Output:

0 2 4 6 8 10 12 14

C-Program to print the pattern -4

Tags

, ,

C Program to print the below(Diamond) pattern in the consoleScreen Shot 2016-06-01 at 12.05.52 AM

#include<stdio.h>

#include<stdlib.h>
void main()
{
    int n = 5;
    int k = n+(n-1);
    for (int row = 0; row < k; ++row) {
        if (row <= (n-1)) {
            for (int col = 0 ; col < k; ++col) {
                int tmpCol = ((n-row)-1);
                int tmpCol1 = ((n+row)-1);
                if ((col == tmpCol) || (col == tmpCol1)) {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
        }else
        {
            for (int col = 0 ; col < k; ++col) {
                int tmpCol = row%n+1;
                int tmpCol1 = (k-2)-row%n;
                if ((col == tmpCol) || (col == tmpCol1)) {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
        }
        printf("\n");
    }
}

C – Program’s For Printing Patterns – 3.

Tags

, ,

C Program to print the below pattern in the console
—————–

* * * * * * * * *

* * * * * * * *

* * * * * * *

* * * * * *

* * * * *

* * * *

* * *

* *

——————

#include<stdio.h>

#include<stdlib.h>

void main()

{

int n = 10;

for (int j = 0; j < n; ++j) {

for (int i = j; i < n; ++i) {

printf("* ");

}

printf("\n");

}

}

To print Fibonacci series.

/*programm to print Fibonacci series upto n*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int n,a,b,i,fib;
    
    
    a=0;
    b=1;
    printf("Enter required number of fib numbers:");
    scanf("%d",&n);
    
    
    if(n==0)
        printf("%d",a);
    else if(n==1)
    {
        printf("\nFibonacci Series:");
        printf("%d\t%d",a,b);
    }
    else
    {
        printf("\nFibonacci Series:");
        printf("%d\t%d\t",a,b);
        for(i=2;i<=n;i++)
        {
            fib=a+b;
            a=b;
            b=fib;
            printf("%d\t",fib);
        }
    }
}
​​

To reverse a number.

//programm to reverse the number upto 4-digits only
#include<stdio.h>
#include<conio.h>
void main()
{
    int reverse=0,n;
    printf("Enter the Number To be reversed:");
    scanf("%d",&n);
    
    while(n!=0)
    {
        reverse=reverse*10;
        reverse=reverse+n%10;
        n=n/10;
    }
    printf("The reversed number is: %d",reverse);
    getch();
}

To check if the number is palindrome or not.

Tags

//To chechk weather the number is palindrome or not
#include<stdio.h>
#include<conio.h>
void main()
{
    int n,rev=0;
    printf("Enter the number to be checked:");
    scanf("%d",&n);
    int num=n;
    while(n!=0)
    {
        rev=rev*10;
        rev=rev+n%10;
        n=n/10;
    }
    if(num == rev)
    {
        printf("The number is Palindrome :)!");
    }
    else
    {
        printf("The number is not palindrome \:(");
    }
}
​​

Check for palindrome of string without using library string.h

Tags

/*Programm to check weather the entered string is palindrome or not
 without using library function*/
#include<stdio.h>
#include<conio.h>

void main()
{
    char str[20];
    char revStr[20];
    int i=0,j=0,strLen=0,c=0;
    printf("\nEnter the string to be Checked for palindrome:");
    scanf("%s",&str);
    
    for(i=0;str[i]!='';i++)
    {
        strLen=strLen+1;
    }
    i=0;
    for(j=strLen-1;j>=0;j--)
    {
        revStr[i]= str[j];
        i++;
    }
    revStr[i]='';
    printf("\nreversed str=%s",revStr);
    while(str[c] == revStr[c])
    {
        if(str[c] == '' || revStr[c]=='')
            break;
        c++;
    }
    if(str[c] == '' || revStr[c]=='')
        printf("\nString is Palindrome\n");
    else
        printf("\nString is not Palindrome");
    
    printf("\nEnd Of Programm");
    getch();
}

To Reverse the string without using string.h library

Tags

/*To reverse the string without using library functions*/

#include<stdio.h>
#include<conio.h>

void main()
{
    char a[10],b[10];
    int count=0;
    int i=0,j;
    printf("Enter String to reverse:");
    scanf("%s",a);
    
    /*get the length of the string Without using strlen(); */
    for(i=0;a[i]!= '';i++)
    {
        count=count+1 ;
    }
    
    printf("str len = %d\n",count);
    printf("\nReversed String is :: ");
    
    for(j=count-1;j>=0;j--)
    {
        printf("%c",a[j]);
    }

}