Program
function[]=simp3(x,f)
[x0,xn]=size(x)
[y0,yn]=size(f)
if((x0<>1)|(y0<>1))then
error('x of f,or both ,not coloumn vector (s)')
abort;
end;
if((xn<>yn))
then
error ('x of f,or both,not of the same length')
abort;
end;
if(modulo(xn-1,3)<>0)then
disp(xn-1,"list size=");
error("list size must be of the from 3*i+1, where i=integer");
abort;
end;
n=xn;
h=x(2)-x(1);
i=f(1,1)+f(1,n);
for j=2:n-1
if(modulo(j-1,3)==0) then
i=i+2*f(1,j)
else
i=i+3*f(1,j)
end;
end;
i=(3/8)*h*i;
printf('integration by simpson 3/8 rd rule = %f',i);
endfunction
Output:
x=(4:0.2:5.2);
simp(x,log10(x));
integration by simpson 3/8 rd rule = 1.827847
function[]=simp3(x,f)
[x0,xn]=size(x)
[y0,yn]=size(f)
if((x0<>1)|(y0<>1))then
error('x of f,or both ,not coloumn vector (s)')
abort;
end;
if((xn<>yn))
then
error ('x of f,or both,not of the same length')
abort;
end;
if(modulo(xn-1,3)<>0)then
disp(xn-1,"list size=");
error("list size must be of the from 3*i+1, where i=integer");
abort;
end;
n=xn;
h=x(2)-x(1);
i=f(1,1)+f(1,n);
for j=2:n-1
if(modulo(j-1,3)==0) then
i=i+2*f(1,j)
else
i=i+3*f(1,j)
end;
end;
i=(3/8)*h*i;
printf('integration by simpson 3/8 rd rule = %f',i);
endfunction
Output:
x=(4:0.2:5.2);
simp(x,log10(x));
integration by simpson 3/8 rd rule = 1.827847
6 Comments
bankers 1
ReplyDelete#include
int n=5,m=4,j,i,cnt;
int work[]={1,5,2,0};
int max[7][7];
int allo[7][7];
int need[7][7];
int ss[5];
int finish[5];
//int max[10][10];
//int allo[10][10];
void print(int x[7][7])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d\t",x[i][j]);
printf("\n");
}
}
void acceptdata(int x[7][7])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&x[i][j]);
}
}
main()
{
int i;
printf("allocation matrix\n");
acceptdata(allo);
print(allo);
printf("\n\n");
printf("max matrix\n");
acceptdata(max);
print(max);
printf("need matrix\n");
computeneed();
print(need);
printf("work is\n");
for(i=0;i<m;i++)
printf("%d\t",work[i]);
checkstate();
}
void computeneed()
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-allo[i][j];
}
int checkneed(int i)
{
int j;
cnt=0;
for(j=0;j<m;j++)
if(need[i][j]<=work[j])
cnt++;
if(cnt==m)
return 1;
else
return 0;
}
void checkstate()
{
int flag=0;
int k=0,cnt=0;
for(i=0;i<n;i++)
finish[i]=0;
while(1)
{ flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
if(checkneed(i)==1)
{
for(j=0;j<m;j++)
work[j]=work[j]+allo[i][j];
finish[i]=1;
flag=1;
ss[k++]=i;
}
}
if(n==k)
{
cnt=1;
break;
}
if(flag==0)
{
printf("not in safe state");
break;
}
}
if(cnt==1)
{
printf("safe sequence is\n");
for(i=0;i<n;i++)
printf("P%d\t",ss[i]);
}
}
bankers 2
ReplyDelete#include
int n = 5, m = 3, j, i, cnt;
int work[5];
int max[][3] = {{7,5,3}, {3,2,2}, {9,0,2}, {2,2,2}, {4,3,3}};
int allo[][3] = {{0,1,0}, {2,0,0}, {3,0,2}, {2,1,1}, {0,0,2}};
int need[5][3];
int ss[5];
int finish[5];
int avl[3];
// Function prototypes
void print(int x[][3]);
void computeneed();
int checkneed(int i);
void checkstate();
void print(int x[][3]) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
printf("%d\t", x[i][j]);
printf("\n");
}
}
void computeneed() {
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - allo[i][j];
}
int checkneed(int i) {
int j, cnt = 0;
for (j = 0; j < m; j++)
if (need[i][j] <= work[j])
cnt++;
return (cnt == m) ? 1 : 0;
}
void checkstate() {
int flag, k = 0, cnt = 0;
for (i = 0; i < n; i++)
finish[i] = 0;
while (1) {
flag = 0;
for (i = 0; i < n; i++) {
if (finish[i] == 0 && checkneed(i) == 1) {
for (j = 0; j < m; j++)
work[j] += allo[i][j];
finish[i] = 1;
flag = 1;
ss[k++] = i;
}
}
if (n == k) {
cnt = 1;
break;
}
if (flag == 0) {
printf("Not in a safe state\n");
return;
}
}
if (cnt == 1) {
printf("Safe sequence is: ");
for (i = 0; i < n; i++)
printf("P%d\t", ss[i]);
printf("\n");
}
for (i = 0; i < m; i++)
work[i] = avl[i];
}
int main() {
int i;
printf("Allocation Matrix:\n");
print(allo);
printf("\nMax Matrix:\n");
print(max);
printf("Need Matrix:\n");
computeneed();
print(need);
printf("Enter available resources: ");
for (i = 0; i < m; i++) {
scanf("%d", &work[i]);
avl[i] = work[i];
}
checkstate();
return 0;
}
1.FCFS
ReplyDelete#include
int main()
{
int hpos, req[20],i,n,mov=0;
printf("Enter the Head Position");
scanf("%d",&hpos);
printf("Enter the total no request");
scanf("%d",&n);
printf("Enter the Request");
for(i=0;i",hpos);
mov =mov + (abs(hpos-req[i]));
hpos =req[i];
}
printf("%d\n",hpos);
printf("Total Seek Position :%d",mov);
}
2.SSTFs
ReplyDelete#include
#include
int main()
{
int readyq[100],i,n,totalhead=0,initial,count=0;
printf("Enter no of requests");
scanf("%d",&n);
printf("Enter no of request sequence");
for(i=0;idiff)
{
min=diff;
index=i;
}
}
totalhead=totalhead+min;
initial=readyq[index];
readyq[index]=1000;
count++;
}
printf("total head movements is %d",totalhead);
}
#include
ReplyDelete#include
int main()
{
int readyq[100],i,n,totalhead=0,initial,count=0;
printf("Enter no of requests");
scanf("%d",&n);
printf("Enter no of request sequence");
for(i=0;idiff)
{
min=diff;
index=i;
}
}
totalhead=totalhead+min;
initial=readyq[index];
readyq[index]=1000;
count++;
}
printf("total head movements is %d",totalhead);
}
SCAN Disk Scheduling algorithm
ReplyDelete#include
#include
void scan(int req[],int n,int head,int size,int dir)
{
int seek=0,i,j,temp,bound=size,start;
for(i=0;ireq[j])
{
temp=req[i];
req[i]=req[j];
req[j]=temp;
}
}
}
for(start=0;start=head)
{
break;
}
printf("seek sequence:%d",head);
if(dir==1)
{
for(i=start;i%d",head);
}
seek+= abs(bound - head);
head = bound;
printf(" ->%d",head);
for(i=start-1;i>=0;i--)
{
seek+=abs(req[i]-head);
head=req[i];
printf("->%d",head);
}
}
else
{
for(i=start-1;i>=0;i--)
{
seek+=abs(req[i]-head);
head=req[i];
printf("->%d",head);
}
seek+= abs( head -0);
head = 0;
printf(" ->%d",head);
for(i=start;i%d",head);
}
}
printf("\n Total seek time:%d\n",seek);
}
int main()
{
int n,head,size,dir,i;
printf("Enter number of requets:");
scanf("%d",&n);
int req[n];
printf("Enter request queue:");
for(i=0;i<n;i++)
scanf("%d",&req[i]);
printf("Enter disk size:");
scanf("%d",&size);
printf("Enter initial head position:");
scanf("%d",&head);
printf("Enter direction (1 for right,0 for left):");
scanf("%d",&dir);
scan(req,n,head,size,dir);
return 0;
}