Monday, 15 August 2016

C Program for Matrix Rotation






SOURCE CODE:

#include<stdio.h>

void main()
{
int a[10][10],b[10][10],T[4];   // a for original matrix  and b to store intermediate result 
int i,j,k,t,n,m,beg,end;
printf("Enter the size of the matrix:");
scanf("%d",&n);

for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
b[i][j]=a[i][j];
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf(" %2d  ",b[i][j]);
printf("\n");
}
beg=0;end=n-1;
t=0;                                // t for number of square rotations
m=n;
while(t<(m/2))
{

T[0]=a[beg][beg];                  // T is to store the values at corners
T[1]=a[beg][end];
T[2]=a[end][end];
T[3]=a[end][beg];

i=beg;j=beg+1;
  for(k=0;k<n-2;k++)               // 4 fors for 4 rotations
     { 
     b[i][j+1]=a[i][j];
       j++;
      }

j=end;
i=beg+1;
  for(k=0;k<n-2;k++)
     {
      b[i+1][j]=a[i][j];
       i++;
      }

i=end;
j=end-1;
  for(k=0;k<n-2;k++)
    {
     b[i][j-1]=a[i][j];
     j--;
    }

 i=end-1;
 j=beg;
   for(k=0;k<n-2;k++)
    {
     b[i-1][j]=a[i][j];
      i--;
    }
b[beg][beg+1]=T[0];          //adding corner elements at rotated positions
b[beg+1][end]=T[1];
b[end][end-1]=T[2];
b[end-1][beg]=T[3];
beg++;                      //moving to inner squares
end--;
n=n-2;
for(i=beg;i<n;i++)
for(j=beg;j<n;j++)
a[i][j]=b[i][j];
t++;
}
printf("\nAfter rotation:\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
printf(" %2d ",b[i][j]);
printf("\n");
}


}
  

OUTPUT:

Enter the size of the matrix:5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15
 16  17  18  19  20
 21  22  23  24  25

After rotation:

  6   1   2   3   4
 11  12   7   8   5
 16  17  13   9  10
 21  18  19  14  15
 22  23  24  25  20

--------------------------------







Thursday, 26 November 2015

C program for deadlock detection - Operating Systems

ALGORITHM:

1. Mark each process that has a row in the Allocation matrix of all zeros.
2. Initialize a temporary vectorW to equal the Available vector.
3. Find an indexi such that processi is currently unmarked and thei th row ofQ
is less than or equal to W . That is,Q ik Wk, for 1 … k m . If no such row is
found, terminate the algorithm.
4. If such a row is found, mark processi and add the corresponding row of the
allocation matrix to W . That is, setWk = Wk + Aik, for 1 … k m . Return
to step 3.




SOURCE CODE:
#include<stdio.h>
static int mark[20];
int i,j,np,nr;

int main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];

printf("\nEnter the no of process: ");
scanf("%d",&np);
printf("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}




printf("\nEnter the request matrix:");

for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);

printf("\nEnter the allocation matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
/*Available Resource calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];

}
}

//marking processes with zero allocation

for(i=0;i<np;i++)
{
int count=0;
 for(j=0;j<nr;j++)
   {
      if(alloc[i][j]==0)
        count++;
      else
        break;
    }
 if(count==nr)
 mark[i]=1;
}
// initialize W with avail

for(j=0;j<nr;j++)
    w[j]=avail[j];

//mark processes with request less than or equal to W
for(i=0;i<np;i++)
{
int canbeprocessed=0;
 if(mark[i]!=1)
{
   for(j=0;j<nr;j++)
    {
      if(request[i][j]<=w[j])
        canbeprocessed=1;
      else
         {
         canbeprocessed=0;
         break;
          }
     }
if(canbeprocessed)
{
mark[i]=1;

for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
}
}
}

//checking for unmarked processes
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;


if(deadlock)
printf("\n Deadlock detected");
else
printf("\n No Deadlock possible");
}


OUTPUT:

Enter the no of process: 4
Enter the no of resources: 5

Total Amount of the Resource R1: 2
Total Amount of the Resource R2: 1
Total Amount of the Resource R3: 1
Total Amount of the Resource R4: 2
Total Amount of the Resource R5: 1

Enter the request matrix:0 1 0 0 1
0 0 1 0 1
0 0 0 0 1
1 0 1 0 1

Enter the allocation matrix:1 0 1 1 0
1 1 0 0 0
0 0 0 1 0
0 0 0 0 0

 Deadlock detected
--------------------------------


Thursday, 5 November 2015

PLSQL Program to check whether a number is armstrong or not

declare
n number(5);
temp number(5);
r number(3);
s number(5);
begin
n:=&n;
temp:=n;
s:=0;
while(n>0)
loop
r:=n mod 10;
s:= s + (r*r*r);
n:=floor(n/10);
end loop;
if(temp = s) then
dbms_output.put_line(temp ||' is a Armstrong no');
else
dbms_output.put_line(temp ||' is NOT a Armstrong no');
end if;
end;

PL SQL Program to check whether a number is perfect or not

declare
n number;
i number;
tot number;
begin
n:=&n;
tot:=0;
for i in 1..n/2
loop
if(n mod i=0) then
tot:= tot+i;
end if;
end loop;
if(n=tot)then
dbms_output.put_line('Perfect no');
else
dbms_output.put_line('Not a Perfect no');
end if;
end;

Tuesday, 3 November 2015

CPP Program for implementing stack as linked list - Data Structures

#include<iostream>
using namespace std;
struct node
{
int data;
struct node *link;
};
struct node *top=NULL;

void push(int val)
{
node *newnode =new node;
newnode->data=val;
newnode->link=top;
top=newnode;
}

void pop()
{
if(top==NULL)
cout<<"\nStack is empty!";
else
top=top->link;
}


void display()
{
node *temp=top;
if(temp==NULL)
cout<<"\nStack is empty!";
else
{
while(temp!=NULL)
{
  cout<<temp->data<<"\n";//to print horizontally we need to point from bottom, hence to avoid this we print vertically..
  temp=temp->link;
}
}
}

int main()
{
int choice,val;
while(1)
{
cout<<"\nSTACK LINKED LIST \n1.Push 2.Pop 3.Display 4.Exit \nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1:
   cout<<"\nEnter value to Push:";cin>>val;
   push(val);
   break;
case 2:
   pop();
   break;
case 3:
   display();
   break;
case 4:
   return 0;
   break;
default:
cout<<"\nCheck ur input\n";
}
}
return 0;
}



OUTPUT:

STACK LINKED LIST
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:1

Enter value to Push:1

STACK LINKED LIST
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:1

Enter value to Push:5

STACK LINKED LIST
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:1

Enter value to Push:3

STACK LINKED LIST
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:3
3
5
1

STACK LINKED LIST
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:2

STACK LINKED LIST
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:3
5
1

STACK LINKED LIST
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:

Child parent process for sorting using fork() call - Operating Systems

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t pid;
int i,j;
int a[5];
printf("\nEnter 5 numbers:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
pid=fork();
if(pid==0)
{
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
{
if (a[i]>a[j])
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

for(i=0;i<5;i++)
printf("%d",a[i]);
}
}

CPP Program for Single linked list - Data Structures

#include<iostream>
using namespace std;
typedef struct node
{
int data;
struct node *link;
};
node *slist=NULL;
void insBeg(int val)
{
node *newnode = new node;

newnode->data=val;
newnode->link=slist;
slist=newnode;
}
void insEnd(int val)
{

if(slist==NULL)
{
node *newnode = new node;
newnode->data=val;
newnode->link=NULL;
slist=newnode;
}
else
{
node *temp=slist;
while(temp->link!=NULL)
temp=temp->link;
node *newnode = new node;
newnode->data=val;
newnode->link=NULL;
temp->link=newnode;
}
}
int insLoc(int val,int loc)
{
node *temp=slist;

for(int i=1;i<loc;i++)
{
temp=temp->link;
if(temp==NULL)
{
cout<<"\nInvalid Location:";
return 0;
}
}
node *newnode=new node;
newnode->data=val;
newnode->link=temp->link;
temp->link=newnode;
}
int delnode(int val)
{
if(slist->link==NULL && slist->data == val)
slist=NULL;
node *temp=slist;
while(temp!=NULL)
{
node *p;
if(temp->data==val)
{
   if(temp==slist)
   slist=temp->link;
   else
   {
     p->link=temp->link;
    }
    delete temp;
     return 0;
}
else
{
p=temp;
temp=temp->link;
}
}
cout<<"\nData not found";
}

int retrive(int loc)
{
node *temp=slist;
int count=1;
while(temp!=NULL)
{
if(count==loc)
return temp->data;
temp=temp->link;
count++;
}
cout<<"Invalid location";
}
int countnode()
{
node *temp=slist;
int count=1;
while(temp->link!=NULL)
{
temp=temp->link;
count++;
}
return count;
}

void display()
{
node *temp=slist;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->link;
}
}

int main()
{
int choice,val,loc;
do
{
cout<<"\nSINGLE LINKED LIST\n1.Insert at the Beginning \n2.Insert at end\n3.Insert at location\n4.delete\n5.retrive\n6.Count\n7.Display\n8.Exit";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter the data:";cin>>val;
insBeg(val);
break;
case 2:
cout<<"\nEnter the data:";cin>>val;
insEnd(val);
break;
case 3:
cout<<"\nEnter the data,location:";cin>>val;cin>>loc;
insLoc(val,loc);
break;
case 4:
cout<<"\nEnter the data:";cin>>val;
delnode(val);
break;
case 5:
cout<<"\nEnter the location:";cin>>val;
cout<<"\nretrieved data:"<<retrive(val);
break;
case 6:
cout<<"\nNumber of nodes:";cout<<countnode();
break;
case 7:
display();
break;
case 8:
return 0;
break;
}


}while(1);
return 0;
}