Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Tuesday, 26 July 2016

Checking minimum and maximum of an array using functions : C program

/*
 Write a function which return min and max value from an array
*/
#include<stdio.h>
void minmax();
void main()
{
int i,n,minValue,maxValue;
printf("Enter the size of array:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minValue = array[0];maxValue=array[0];
minmax(array,n,&minValue,&maxValue);
printf("Minimum value is %d and maximum value is %d ",minValue,maxValue);
}

void minmax(int* array,int n,int* minValue,int* maxValue)
{
int i;
for(i=0;i<n;i++)
{
    if(*(array+i) < *minValue )
        *minValue = *(array+i);
    if(*(array+i) > *maxValue )
        *maxValue = *(array+i);
}
}

Friday, 27 May 2016

Printing a Pascal's Triangle : C program

Probably this is one of the toughest pattern to print using C program for beginners , but after going through this post this won't be tough any more.
Lets focus on definition , what pascal triangle comprises of...
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients.
like , we can see in image :

Pascal's Triangle


Now , see .
Isn't it like this ?

pascal's triangle represented in terms of combinations !



Now , probably you are getting the point i am talking about.
We can simply print the pattern by calling a function of *combination.
The two for loops are required , for row and column and using the following logic we can print the pascal triangle up to required number of rows .


C program : Printing a Pascal's Triangle





#include < stdio.h > 
int comb();/*function for finding combination*/
int main()
   }
}


int fact();/*functions for finding factorial*/

{
   int i,j,n;
   int ans;
   printf("Enter number of rows of pascal's triangle :\n");
   scanf("%d",&n);
   for(i=0;i < =n;i++)/*main for loop*/
   {
       for(j=n-i;j!=0;j--)/*nested for loop for managing space*/
       {
           printf(" ");
       }
       for(j=0;j < =i;j++)/*nested for loop for iCj=nCr */
       {
           ans=comb(i,j);/*calling a function for combination*/
           printf("%d ",ans);/*printing the required number*/
       }
       printf("\n");/*printing new line after dealing with one row*/


int comb(int x,int y)
{
    int a,b,c,d;
    a=fact(x);/*calling a function for factorial*/
    b=fact(y);
    c=fact(x-y);
    d=a/(b*c);
    return(d);
}
int fact(int x)
{
    int i,p=1;
    for(i=1;i < =x;i++)
    {
        p=p*i;
    }
    return(p);
}



*For explanation of functions of combination and factorial refer to the link below :

Permutation and Combination using C functions !

Thank you guys for visiting !
Suggestions and feedback are always accepted .

Thursday, 26 May 2016

Permutations and Combinations #1 : C program.

Hello friends !
Today we are going to deal with extremely powerful tool of 'C' language , and that is user-defined "Functions".
Some prerequisite knowledge is required to understand this program, and that is -

  1. Switch case:
  2. Functions 
  3. Mathematical meaning of Permutations and Combination.
Basically , Permutation is the number of possible arrangements and Combination is the number of possible selections in a given case and under certain constraints .
Here we are going to calculate simply , nPr and nCr. 
We have certain mathematical formulas for calculating this -
  • nPr = n!/r! .
  • nCr = n!/(r!*(n-r)!) .
So , what we need to do is , make three functions for calculating 1.Factorial 2.Permutations & 3.Combinations .

* For finding factorial we can initiate fact=1, and run a loop using counter variable i n times performing the calculation fact=i*fact and increment i by 1 in each loop.
* And for finding nPr and nCr all we need to do is call function of factorial and put the required in factorial in the given formula .
And that's it !
Try to code yourself or copy the program given below , and check whether it is running !


C Program : Permutations and Combinations !



#include < stdio.h > 
/*program to calculate permutations and combinations !*/
int comb();/*function for calculating combination*/
int per();/*function for calculating permutation*/
int fact();/*function for calculating factorial*/

int main()
{
    int e,n,r,i,c,p;
    printf("Enter \n1. Permutations\n2. Combinations !\n");
    scanf("%d",&e);
    printf("Enter n and r\n");
    scanf("%d%d",&n,&r);
    switch(e)
    {
    case 1:
        p=per(n,r);/*calling a function for calculating permutation */
        /* take note that values within the parenthesis are assigned to parameters of the function*/
        printf("nPr=%d\n",p);/*printing the answer*/
        break;/*break is essential while using Switch-case after every step*/
        /* If break is not used then the all following cases will be calculated*/
    case 2:
        c=comb(n,r);
        printf("nCr=%d\n",c);
        break;
    default :
        printf("bye!");
    }

}
int per(int x,int y)
{
    int a,b,c;
    a=fact(x);
    b=fact(y);
    c=a/b;/* according to formula of nPr */
    return(c);/*returning the calculated value of nPr*/
}
int comb(int x,int y)
{
    int a,b,c,d;
    a=fact(x);
    b=fact(y);
    c=fact(x-y);
    d=a/(b*c);/*according to formula of nCr*/
    return(d);/*returning the calculated value of nCr*/
}
int fact(int x)
{
    int i,p=1;
    for(i=1;i < = x ; i++)
    {
        p=p*i;/* multiplying 1*2*3*...n times to find factorial*/
    }
    return(p);/* Returning value of calculated factorial*/
}


"Thanks for referring this , any modification and suggestions are accepted. "