C : round robin algorithm implementation


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char p[10][5];
int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m;
float avgwt;

for(i=0;i<n;i++)
{
printf("Enter the process name : ");
scanf("%s",&p[i]);
printf("Enter the processing time : ");
scanf("%d",&pt[i]);
}
m=n;
wt[0]=0;
i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=pt[i];
}
i++;
wt[i]=wt[i-1]+et[i-1];
}while(i<n);

count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{

wt[i]=wt[found]-(count*timer);
count=0;
found=0;
}
}
for(i=0;i<m;i++)
{
totwt+=wt[i];
}
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
{
printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);
}
printf("\nTotal waiting time %d\n",totwt);
printf("\nTotal avg.turnaround time: %f",avgwt);
getch();
}

C : secant method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.00001
#define f(x) x*x-4*x-10
int main()
{
float x1,x2,x3,f1,f2;
printf("Enter two value (x1,x2) : ");
scanf("%f",&x1);
scanf("%f",&x2);
f1=f(x1);
f2=f(x2);
Label:
x3=(f2*x1-f1*x2)/(f2-f1);
if(fabs((x3-x2)/x3)>E)
{
x1=x2;
f1=f2;
x2=x3;
f2=f(x3);
goto Label;
}
else
printf("The root is %f",x3);
getch();
return(0);
}

C : newton rapson method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) x*x-3*x+2
#define df(x) 2*x-3
#define E 0.00001

int main()
{
float x0,x1,y0,dy0;
int i,n;
printf("Enter the value of x : ");
scanf("%f",&x0);
printf("\nEnter the number of iteration : ");
scanf("%d",&n);
printf("\nIter.\tx0\tf(x0)\tdf(x0)\tx1\n");

for(i=0;i<n;i++)
{
y0=f(x0);
dy0=df(x0);
x1=(x0-(y0/dy0));
x0=x1;
printf("%d\t%.4f\t%.4f\t%.4f\t%.4f",i,x0,y0,dy0,x1);
}
if((fabs(x1-x0)/x1)<E)
   {
printf("\nNumber of iteration is %d",i);
printf("\nRoot = %f\n",x1);

}

getch();
return(0);

}

C++ : lagrangue's interpolation


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int i,j,n;
float mult,sum=0,x[10],f[10],a;
cout<<"Enter the number of sample points : ";cin>>n;
for(i=0;i<n;i++)
{
        cout<<"( x["<<i<<"],f["<<i<<"] ) : ";
cin>>x[i]>>f[i];
}

cout<<"\nEnter the value of x : ";
cin>>a;

for(i=0;i<=n;i++)
{
mult=1;
for(j=0;j<=n;j++)
{
if(j!=1)
mult=mult*((a-x[j])/(x[i]-x[j]));
}
sum=sum+mult*f[i];
}
cout<<"\nThe value of f(x) = "<<sum;
getch();
return(0);
}

C : fixed point iteration method


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

#define E 0.00001
#define g(x) 2-x*x

int main()
{
float x1,x2;

printf("Enter the initial guess : ");
scanf("%f",&x1);

Lbl:
x2=g(x1);
if(((x2-x1)/x2)<E)
{
printf("The root is %f",x2);
goto End;
}
else
   {
x1=x2;
goto Lbl;
}
End:
getch();
return(0);
}

C : bisection method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define E 0.00001
#define f(x) x*x-4*x-10
int main()
{
float x0,x1,y0,y1,x2,y2;
int i=0;
printf("enter the initial guess\n\n");
scanf("%f%f", &x0,&x1);
y0=f(x0);
y1=f(x1);
if((y0*y1)>0)
{
printf("initial guess are not suitable \n\n");
getch();
exit(0);
}
printf("iter\tx0\tf(x0)\tx1\tf(x1)\tx2\tf(x2)\n");
while(fabs((x1-x0)/x1)>E)
{
x2=(x0+x1)/2;
y2=f(x2);
i=i+1;
printf("%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t\n",i,x0,y0,x1,y1,x2,y2);
if(y0*y2>0)
x0=x2;
else
x1=x2;
}
printf("number of iteration is %d\n",i);
printf("the root is %.2f",x2);
getch();
return(0);
}

C++ : matrix display using class


#include<iostream>
#include<conio.h>

using namespace std;

class matrix
{
int a;
int b;
int A[3][3];
public:
void readmatrix();
void displaymatrix();
};

void matrix::readmatrix()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout<<"A["<<i<<"]["<<j<<"] : ";
cin>>A[i][j];
}
}
}
void matrix::displaymatrix()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout<<" "<<A[i][j];
}
cout<<endl;
}

}



int main()
{

matrix m;
m.readmatrix();
m.displaymatrix();
getch();
return(0);
}