Archive for the ‘Computer programming assignment’ Category

The program posted by mayur can be simplified further..

His program calculates n!,(n-r)! and r!.then it does further multiplication and division

If you observe the term n!/(n-r)! it can be written as n(n-1)..(n-r+1)(n-r)!/(n-r)! which reduces to n(n-1)..(n-r+1)

thus if we calculate the above term , calculate r! and divide above term by r! we will have the answer in little less calculations!!

Here is the C  program for it…..

 

#include<stdio.h>
float nCr(int n,int r)
{
 int num=1,den=1,i;

 for(i=1;i<=r;i++) //calculates r!
  den*=i;

 for(i=n;i>=n-r+1;i--)  //calculates n(n-1)..(n-r+1)
  num*=i;

 return (float)num/(float)den;//returns n(n-1)..(n-r+1)/r!=nCr

}
void main()
{
 int n,r;
 clrscr();
 printf("Enter value of n and r for nCr computation");
 scanf("%d%d",&n,&r);
 printf("\nThe value of %dC%d is %f",n,r,nCr(n,r));
 getch();
}

Correction!

Posted: September 8, 2011 by Mayur More in CP assignment unit III, Uncategorized

The program for removal of duplicate elements is modified pz take note of it

an extra if() statement is added after n=n-1

thnx!

Finding (n r) i.e combination

Posted: September 7, 2011 by Mayur More in CP assignment unit III
Description:This program uses functions, as we all know the formula for (n r)=n!/(r!*(n-r)!)
so we have to find the factorial of n , r, n-r. so instead of finding factorial for three times
we can define one function which will do the job of finding factorial, so in main()
we send the value of n,r,n-r to find_fact() function and it calculates the fact of each of them and sends 
it to main(),which can be used for further calculation of (n r)
 
CODE:
#include<stdio.h>
#include<conio.h>
int find_fact(int);
void main()
{
     int n,c,r,nmr,n_fact,r_fact,nmr_fact;
     clrscr();
     printf("enter the value of n and r so as to find (n r) \nwhere n elements are taken r at a time:\n");
     printf("n:");
     scanf("\n%d",&n);
     printf("r:");
     scanf("%d",&r);
     n_fact=find_fact(n);
     r_fact=find_fact(r);
     nmr=n-r;
     nmr_fact=find_fact(nmr);
     c=n_fact/(r_fact*nmr_fact);
     printf("%d combinations are possible.",c);
     getch();
}
find_fact(int x)
{
	int i,fact=1;
	for(i=1;i<=x;i++)
	fact=fact*i;
	return(fact);
}

Bubble Sort

Posted: September 7, 2011 by Mayur More in CP assignment unit III
Description: I hope you know bubble sort technique.
to read more about the working of bubble sort and to see how bubble sort algo works, 
you can go to topics>>Arrays>>sorting techniques>>bubble sort 
or click here:

CODE:
#include<stdio.h>
main()
{
    int size,i,j,temp,a;
    int array[100];
    printf("enter the numbers of elements in the array");
    scanf("%d",&size);
    for(i=0;i<size;i++)
    {
	 scanf("%d",&array[i]);
    }
    for(i=0;i<size-1;i++)
    {
	  for(j=0;j<size-1-i;j++)
	  {
	       if(array[j]>array[j+1])
	       {
		      temp=array[j];
		      array[j]=array[j+1];
		      array[j+1]=temp;
	       }
	  }
    }
    printf("The sorted array is:\n");
    for(i=0;i<size;i++)
    {
    printf("%d",array[i]);
    }
    getch();
}

Selection Sort

Posted: September 7, 2011 by Mayur More in CP assignment unit III
Description: as we know the algo for selection sort I hope it will be easy for you to understand it.
To know more abt Selection Sort the working of algo etc
goto Topics>>Arrays>>Sorting techniques>>selection Sort
or click here! 

CODE:
#include<stdio.h>
main()
{
    int i,j,n,temp,a[100],min,loc;
    printf("enter the no. of elements in an array:");
    scanf("%d",&n);
    printf("\nenter the elements in an array:");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {       min=a[i];
        loc=i;
        for(j=i+1;j<n;j++)
        {
            if(a[j]<min)
            {
                min=a[j];
                loc=j;
            }
        }
        if(loc!=0)
        {
            temp=a[i];
            a[i]=a[loc];
            a[loc]=temp;
        }
    }
    for(i=0;i<n;i++)
    {
        printf("\n%d",a[i]);
    }
}    

HERE IS AN ALTERNATIVE CODE BUT IT IS NOT COMPLETELY CONSIDERED AS SELECTION 
SORT IT CAN NEITHER BE CONSIDERED AS SELECTION SORT NOR BUBBLE SORT BUT 
THE ABOVE CODE IS PERFECTLY A SELECTION SORT

CODE:
#include<stdio.h>
void main()
{
  int size,i,j,temp;
    printf("\nEnter size of the array :");
  scanf("%d",&size);
  int array[size];
  printf("\nEnter %d elements in to the array:",size);
  for(i=0;i<size;i++)
  scanf("%d",&array[i]);
  for(i=0;i<size;i++)
 {
      for(j=i+1;j<size;j++)
     {
	   if(array[i]>array[j])
	  {
	       temp=array[i];
	      array[i]=array[j];
	      array[j]=temp;
	   }
      }
  }
  printf("\nThe array after sorting is: ");
  for(i=0;i<size;i++)
  printf(" %d",array[i]);
  getch();
}

Removal of Duplicate elments from an array

Posted: September 7, 2011 by Mayur More in CP assignment unit III
Description: In this program we use three for(), first we check the first element of a[] 
i.e a[i] where i=1 with the next element a[j] where j is defined as j=i+1,
whether they are equal if the are equal then we move the a[j+1] element to a[j] 
so that a[j] is automatically deleted and so on.. check for next element and 
after each deletion reduce n to n-1 so as we are deleting one element.

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

main()

{
   int n, loc,i,dup,k, j, temp;
   printf("Enter the number of elements:");
   scanf("%d",&n);
   int a[n];
   printf("\nenter the array:");
   for(i=0;i<n;i++)
   {
	scanf("%d",&a[i]);
   }
   for(i=0;i<n;i++)
   {
	for(j=i+1;j<n;j++)
	{
		if(a[i]==a[j])
		{

			for(k=j;k<n;k++)
			{
				a[k]=a[k+1];
			}
		        n=n-1;
                        if(a[i]==a[j])
                        {
                        j--;
                        }
                 }
	}
   }
    for(i=0;i<n;i++)
   {
	printf("\n%d",a[i]);
   }

   getch();
}

Array Order Reversal

Posted: September 7, 2011 by Mayur More in CP assignment unit III
description: in this program we first calculate the mid of the array, 
then we exchange the first and the last, 
then second and second last and so on till the mid element.
you can do the same program by taking another array say b[] and 
store the values of a[] in b[] in reverse order and then again store values
of b[] in a[] in sequence.

CODE:

#include<stdio.h>
#include<conio.h>
main()
{
   int n, c,temp;
   clrscr();
   printf("Enter the number of elements in array\n");
   scanf("%d",&n);
   int a[n];
   printf("Enter the array elements\n");

   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&a[c]);

   for (c=0;c<n/2;j++)
   {
      temp=a[c];
      a[c]=a[n-c-1];
      a[n-c-1]=temp;
   }

   printf("Reverse array is\n");

   for( c = 0 ; c < n ; c++ )
      printf("%d\n", a[c]);

   getch();

}

Program to find the maximum of a given set

Posted: September 7, 2011 by Mayur More in CP assignment unit III
#include<stdio.h>
#include<conio.h>
void main()
{
	int i,size,max;
	int array[50];
	clrscr();
	printf("Enter the no of elements in the array:");
	scanf("%d",&size);
	printf("\nEnter the elements in the array:");
	for(i=0;i<size;i++)
	{
		scanf("%d",&array[i]);
	}
	max=array[0];
	for(i=0;i<size;i++)
	{
	     if(array[i]>max)
	     {
		max=array[i];
	     }
	}
	printf("\nthe max of the given array is:%d",max);
	getch();
}