The Programming Project

Monday, September 16, 2013

Intersection of Sets



To find intersection of two sets through C

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void inter_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
inter_set(s1,s2,n,m);
return 0;
}
void inter_set(int *s1,int*s2,int n,int m)
{
int *s,i=0,j,k;
int flag=0;
s=(int *)malloc((m<n ? m: n)*sizeof(int));
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s1[k])
{
flag=1;
break;
}
}

if(flag==1)
{
s[i]=s2[j];
         i++;
flag=0;
}
 if(flag==0)
continue;
 }
 if(i==0)
printf("\n Intersection is Null:");
else
{
  printf("\n Intersection of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);
}


return;
}

Union of Sets


This C program finds the Union of two Sets (atleast one of is non-empty)
#Task- Apply recursion to the program below to find Union of finite number of sets

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void union_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
union_set(s1,s2,n,m);
return 0;
}
void union_set(int *s1,int*s2,int n,int m)
{
int *s,i,j,k;
int flag=0;
s=(int *)malloc((m+n)*sizeof(int));
for(i=0;i<n;i++)
s[i]=s1[i];
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s[k])
{
flag=1;
break;
}
}

if(flag==1)
{
flag=0;
continue;
         }
else
      if(flag==0)
{
s[i]=s2[j];
i++;
flag=0;
}
}
  printf("\n Union of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);



}

Saturday, September 14, 2013

Calculating Factorial in Python

A simple program in Python to calculate the factorial of an natural number:

#File: fact.py
#A simple program
def main():
    print "Factorial Function"
    x=input("Enter a natural number:")
    l=1
    for i in range(x):
        l=l*x
        x=x-1
    print l
main()

Run the python interpreter from the folder where the file (fact.py) is residing and import it as shown below:


administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fact
Factorial Function
Enter a natural number:350
123587405826548875014395199766546457224532073946919515879429330230093035357491314216934583295011178445941552109432761532449767761892237043444942213964090091669490545661255111334533069825455607852789836451585122902099649977304226794874840601811017764137584868137504975397325925882541777117706619490238363409254589994079334626893194608016888986949684994333459029365214555784862353939102567266745712846824819004146064184543888123533464975621179287075018586481357643313075153359002713294611632614208134036650116689052585573350955360246170451786972351365370405722036294385680478287278827977511411909071460914807681131728232182991517416470483157998067487290163200000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>>


Modified Euler's Method for Numerical Solution of first order Differential Equation

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double dfxy(double x,double y);
FILE *fp;
int main(int argc, char* argv[])
{
double x0,y0,h,x,*xi,*yi,temp=0.0;
char c;
int n,i;
if(argc==1)
    {
    printf("\n Enter the value of h (interval-width):");
    scanf("%lf",&h);
    printf("\n Enter the initial value of x:");
    scanf("%lf",&x0);
    printf("\n Enter the initial value of y:");
    scanf("%lf",&y0);
    printf("\n Enter the value of x at which y is calculated:");
    scanf("%lf",&x);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    /*fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));*/
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
n=(x-x0)/h;
xi[0]=x0;
yi[0]=y0;
printf("\n\tX\t\t\tY\n");
printf("\tf(%+lf)\t\t=%+lf",xi[0],yi[0]);
i=1;
while(x0!=x)
    {
    yi[i]=yi[i-1]+dfxy(xi[i-1],yi[i-1])*h;
    x0=x0+h;
    xi[i]=x0;
    //printf("\n >>>>>>>>%lf",yi[i]);
    do
    {
    temp=yi[i];
    yi[i]=yi[i-1]+(h/2)*(dfxy(xi[i-1],yi[i-1])+dfxy(xi[i],yi[i]));
    //printf("\n >>>>>>>>%lf",yi[i]);
    }while(fabs(temp-yi[i])>0.00001);
    i++;
    xi[i]=x0;
    yi[i]=temp;
    printf("\n\tf(%+lf)\t\t=%+lf",xi[i],yi[i]);
    }
printf("\n");
return 0;
}
double dfxy(double x,double y)
    {
    return (x*x+y);
    }

Euler's Method for Numerical Solution of first order Differential Equation





#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double dfxy(double x,double y);
FILE *fp;
int main(int argc, char* argv[])
{
double x0,y0,h,x,*xi,*yi,temp=0.0;
int n,i;
if(argc==1)
    {
    printf("\n Enter the value of h (interval-width):");
    scanf("%lf",&h);
    printf("\n Enter the initial value of x:");
    scanf("%lf",&x0);
    printf("\n Enter the initial value of y:");
    scanf("%lf",&y0);
    printf("\n Enter the value of x at which y is calculated:");
    scanf("%lf",&x);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    /*fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));*/
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
n=(x-x0)/h;
xi[0]=x0;
yi[0]=y0;
printf("\n\tX\t\t\tY\n");
printf("\t%+lf\t\t%+lf",xi[0],yi[0]);
i=1;
while(x0!=x)
    {
    yi[i]=yi[i-1]+dfxy(xi[i-1],yi[i-1])*h;
    x0=x0+h;
    xi[i]=x0;
    printf("\n\t%+lf\t\t%+lf",xi[i],yi[i]);
    }
printf("\n");
//printf("\n Value of the integral using Trapezoidal Rule is %+.8lf\n",invert==0 ? (h/2.0)*temp:-(h/2.0)*temp);
return 0;
}
double dfxy(double x,double y)
    {
    return (x+y);
    }

Simpson's Rule




#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double integrand(double x);
FILE *fp;
int main(int argc, char* argv[])
{
double lower_limit,upper_limit,h,*xi,*yi,temp=0.0;
int n,i,invert=0;
if(argc==1)
    {
    printf("\n Enter the number of intervals:");
    scanf("%d",&n);
    printf("\n Enter the lower limit:");
    scanf("%lf",&lower_limit);
    printf("\n Enter the upper limit:");
    scanf("%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
if(lower_limit>upper_limit)
    {
    temp=lower_limit;
    lower_limit=upper_limit;
    upper_limit=temp;
    invert=1;
    }
h=(upper_limit-lower_limit)/n;
temp=lower_limit;
for(i=0;i<=n;i++)
    {
    xi[i]=temp;
    temp=temp+h;
    }
printf("  xi\t\t\tyi\n");
temp=0.0;
for(i=0;i<=n;i++)
    {
    yi[i]=integrand(xi[i]);
    printf("%+lf\t\t%+.8lf\n",xi[i],yi[i]);
    if(i==0 || i==n)
        temp+= yi[i];
    else   
        {
        if(i%2==0)
        temp += 2*yi[i];
        else
        temp += 4*yi[i];
        }
    }   
printf("\n Value of the integral using Simpson's Rule is %+.8lf\n",invert==0 ? (h/3.0)*temp:-(h/3.0)*temp);
return 0;
}
double integrand(double x)
    {
    return (log(x));
    }

Trapezoidal Rule

C Program to implement Trapezoidal Rule



#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
double integrand(double x);
FILE *fp;
int main(int argc, char* argv[])
{
double lower_limit,upper_limit,h,*xi,*yi,temp=0.0;
int n,i,invert=0;
if(argc==1)
    {
    printf("\n Enter the number of intervals:");
    scanf("%d",&n);
    printf("\n Enter the lower limit:");
    scanf("%lf",&lower_limit);
    printf("\n Enter the upper limit:");
    scanf("%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else if(argc==2)
    {
    fp=fopen(argv[1],"r");
    fscanf(fp,"%d",&n);
    fscanf(fp,"%lf",&lower_limit);
    fscanf(fp,"%lf",&upper_limit);
    xi=(double *)malloc((n+1)*sizeof(double));
    yi=(double *)malloc((n+1)*sizeof(double));
    }
else
    {
    printf("\n Invalid arguments, program will terminate:");
    exit(1);
    }
if(lower_limit>upper_limit)
    {
    temp=lower_limit;
    lower_limit=upper_limit;
    upper_limit=temp;
    invert=1;
    }
h=(upper_limit-lower_limit)/n;
temp=lower_limit;
for(i=0;i<=n;i++)
    {
    xi[i]=temp;
    temp=temp+h;
    }
printf("  xi\t\t\tyi\n");
temp=0.0;
for(i=0;i<=n;i++)
    {
    yi[i]=integrand(xi[i]);
    printf("%+lf\t\t%+.8lf\n",xi[i],yi[i]);
    if(i==0 || i==n)
        temp+= yi[i];
    else
        temp += 2*yi[i];
    }   
printf("\n Value of the integral using Trapezoidal Rule is %+.8lf\n",invert==0 ? (h/2.0)*temp:-(h/2.0)*temp);
return 0;
}
double integrand(double x)
    {
    return (log(x));
    }