Monday, 21 October 2013

reverse

echo "Enter the string"
read str
reverse=`echo $str | rev`

if test $str = $reverse
then
echo "palindrome"
else
echo "Non-palindrome"
fi

prime

set -vx
echo "enter no"
read no

flag=0

n=` expr $no - 1 `

while [ $n -ne 1 ]
do
  
 res=` expr $no % $n`
 
   if [ $res -eq 0 ]
then  
        flag=1
     break
   fi

  n=` expr $n -1 `

done

if [ $flag -ne 1 ]
then
  echo "prime"
else
echo "not prime"
fi
 

exp

#!/bin/sh


a=10 b=20

echo `expr $a + $b`

echo `expr $a - $b`

echo `expr $a \* $b`

echo `expr $a / $b`

echo `expr $a % $b`

fibo

#!/bin/sh
echo "Enter how many no.s you want?"
read n
echo "Fibonacci Series is::"
pr=1
ne=0
cu=0
echo $ne
echo $pr

i=2
while [ $i -lt $n ]
do
ne=`expr $pr + $cu`
echo $ne
cu=$pr
pr=$ne
i=`expr $i + 1`
done

fact

set  -vx
echo "enter no"
read  n
fact=1

if [ $n -eq 0 ]
then
 fact=0
fi

while [ $n -gt 0 ]
do
fact=`expr $fact \* $n `
n=`expr $n - 1 `
done

echo "facto:$fact"

banker_banker

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

typedef struct proc
{
    int rm[10];
    int ra[10];
    int rn[10];
    int pf;
    int pid;
}proc;

proc p[25];
int ar[10];
int np,nr;

int banker(proc p1[25],int ar1[10]);
void display();
void display1(proc p1[25]);
void request1();
void main()
{
    int i,j,val;
    proc p1[25];
    int ar1[10];
    printf("\n Enter No of proocesses : ");
    scanf("%d",&np);
    printf("\n Enter No of Resources : ");
    scanf("%d",&nr);
   
    printf("\n Enter Process Details.....");
    for(i=0;i<np;i++)
    {
        printf("\n Enter Process ID : ");
        scanf("%d",&p[i].pid);
        p[i].pf=0;
        printf("\n Enter Maximum Resources Needed : ");   
        for(j=0;j<nr;j++)
        {
            printf("\n R%d=",j);
            scanf("%d",&p[i].rm[j]);
        }
   
        printf("\n Enter Resources Allocated : ");   
        for(j=0;j<nr;j++)
        {
            printf("\n R%d=",j);
            scanf("%d",&p[i].ra[j]);
            p[i].rn[j]=p[i].rm[j]-p[i].ra[j];
        }
    }   
           
        printf("\n Enter Resources Available : ");   
        for(j=0;j<nr;j++)
        {
            printf("\n R%d=",j);
            scanf("%d",&ar[j]);
        }


        display();


    for(i=0;i<np;i++)
    {
        p1[i]=p[i];
    }   
       
    display1(p1);

    printf("\n Available Resources : ");
    for(i=0;i<nr;i++)
    {
        ar1[i]=ar[i];       
        printf("\t%d",ar1[i]);
    }
   

        val=banker(p1,ar1);
        request1();
       
}


void display()
{
    int i,j;
    system("clear");
    printf("\n PID     Max_Need_Resources      Allocated_Resources");
   
    for(i=0;i<np;i++)
    {
        printf("\n %d",p[i].pid);
        printf("     ");
        for(j=0;j<nr;j++)
        {
            printf("    %d",p[i].rm[j]);       
        }   
        printf("     ");
        for(j=0;j<nr;j++)
        {   
            printf("    %d",p[i].ra[j]);       
        }
    printf("\n");   
       
    }
   

}


int banker(proc p1[25],int ar1[10])
{
    int i,id,j,k=0,flg=0;
//    int ar1[10];
//    proc p1[25];
   
    printf("\n Sequence Of Execution : ");
    while(k<np)
    {
        for(i=0;i<np;i++)
        {
            if(p[i].pf==0)
            {
                for(j=0;j<nr;j++)
                {
                    if(p1[i].rn[j]<=ar1[j])
                    {
                        flg=0;
                    }
                    else
                    {
                        flg=1;
                        break;
                    }
                }   
           
                if(flg==0)
                {
                    for(j=0;j<nr;j++)
                    {
                        ar1[j]=ar1[j]+p1[i].ra[j];
                        p[i].pf=1;           
                    }
                    printf("=====>>%d",p[i].pid);
                    k++;
                    break;
                }
            }
        }//end for
       
        if(flg==1)
        {
            printf("\n System is in Unsafe state.....");
            return(0);       
        }
   
       
    }//end while   
    if(k==np)
    {
        printf("\n System is in Safe State......");
        return(1);       
    }
    printf("\n");
   
           
}

void request1()
{

    int i,id,r[10],j,val;
    proc p2[25];
    int ar2[10];
    //========requesting for resources========
       
   
    for(i=0;i<np;i++)
    {
        p2[i]=p[i];
    }   
       
    display1(p2);

    printf("\n Available Resources : ");
    for(i=0;i<nr;i++)
    {
        ar2[i]=ar[i];       
        printf("\t%d",ar2[i]);
    }
   
    printf("\n Enter Process Id : ");
    scanf("%d",&id);
   
    printf("\n Enter Requested resources : ");
    for(i=0;i<nr;i++)
    {
        scanf("%d",&r[i]);
        p2[id].ra[i]=p2[id].ra[i]+r[i];
        ar2[i]=ar2[i]-r[i];
        printf("Resource %d",i);
    }
    printf("\n caall to banker.....");
    val=banker(p2,ar2);
    if(val==1)
    {
        printf("\n Requested Resources Are Allocated.....");
    }
    else
    {
        printf("\n Requested Resources Are Not Allocated.....");
    }
}


void display1(proc p1[25])
{
    int i,j;
//    system("clear");
    printf("\n PID     Max_Need_Resources      Allocated      Cur_Need");
   
    for(i=0;i<np;i++)
    {
        printf("\n %d",p1[i].pid);
        printf("     ");
        for(j=0;j<nr;j++)
        {
            printf("    %d",p1[i].rm[j]);       
        }   
        printf("     ");
        for(j=0;j<nr;j++)
        {   
            printf("    %d",p1[i].ra[j]);       
        }
   
        printf("     ");
        for(j=0;j<nr;j++)
        {   
            printf("    %d",p1[i].rn[j]);       
        }
    printf("\n");   
       
    }
   

}




















awk

BEGIN{
        main();
        fact=1;
         rev=0;
    }
function main()
{
    do
    {
        printf("\n1.Factorial\n2.Prime Number\n3.Biggest of 3 no's\n4.Palindrome\
            \n5.Number Palindrome\n6.Exit");
        printf("\nEnter Choice  :  ");
        getline ch;
        if(ch==1)
        {
            printf("\n Enter The Number  :  ");
            getline num;
            fact=1;
            for(i=num;i>0;i--)
                {
                    fact=fact*i;
                }
            printf("\nFactorial of %d is %d\n\n",num,fact);
        }
        if(ch==2)
        {
            printf("\n Enter The Number:");
            getline num;
            if(num==1 || num==2)
               
            for(i=2;i>num;i++)
                   {
                    if(num%i==0)
                    {
                                                f=1;
                        break;
                                        }
                    else
                        f=0;
                }
            if(f==1)
                printf("%d is not a prime number",num);
            else
                printf("%d is a prime number",num);               
        }

        if(ch==3)
        {
            printf("\n Enter First Number");
            getline num1;
            printf("\n Enter Second Number");
            getline num2;
            printf("\n Enter Third Number");
            getline num3;
            if(num1>num2 && num1>num3)
                printf("%d is the greatest",num1);
            else if(num2>num3)
                printf("%d is the greatest",num2);
            else
                printf("%d is the greatest",num3);
        }
        if(ch==4)
        {
            printf("\nEnter The String:");
            getline str;
            len=length(str)
            for(i=len;i>0;i--)
            {
                rev=substr(str,i,1);
                str1=str1 rev
            }
            if(str1==str)
                printf("\nString is Palindrome");
            else
                printf("\nString is not a Palindrome");
        }
        if(ch==5)
        {
            printf("\n Enter The Number:");
            getline num;
            rev=0;
            num1=num
            while(num>=10)
              {
                rem=num%10;
                num=int(num/10);
                rev=rev+rem;
                rev=rev*10;
            }
            rev=rev+num;
            if(rev==num1)
                printf("\nNumber is Palindrome");
            else
                printf("\nNumber is not a Palindrome");
        }

    }while(ch!=6)
}

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

{

printf "Enter string :: ";
getline name;
l=length(name);
for(i=l;i>0;i--)
{
    rev=substr(name,i,1);
    str=str rev;

}
if(str==name)
{
printf "String is palindrome";

}
else
{

printf "String is not palindrome";
}
}
END{
}

----------------------------------------------------------
BEGIN{
FS=":"
main();
}
function create()
{
    printf("\nEnter Number of Records:");
    getline num;
    for(i=0;i<num;i++)
    {
        printf("\nEnter Roll No :");
        getline rollno;
        printf("\nEnter Name :");
        getline name;
        printf("\nEnter Marks of Subject M1 :");
        getline m1;
        printf("\nEnter Marks of Subject M2 :");
        getline m2;
        printf("\nEnter Marks of Subject M3 :");
        getline m3;
        printf("%d:%s:%d:%d:%d\n",rollno,name,m1,m2,m3)>>"stud.txt";
    }
}
function display()
{
    printf("ROLLNO\tNAME\tSUB1\tSUB2\tSUB3");
    while(getline <"stud.txt"==1)
    {
        printf("\n%d\t%s\t%d\t%d\t%d",$1,$2,$3,$4,$5);
    }
}
function modify()
{
    ff=0;
    printf("\nEnter Rollno of Student to modify:");
    getline rollno
    while(getline<"stud.txt"==1)
            {
        if(rollno==$1)
        {
                      name=$2
            ff=1;
            printf("Enter New Marks for M1,M2,M3:");
            getline m1;
             getline m2;
                              getline m3;
                printf("%d:%s:%d:%d:%d\n",rollno,name,m1,m2,m3)>>"new.txt";
        }
        else
        {
           print>"new.txt"
        }
    }
    system("rm stud.txt")
    system("mv new.txt stud.txt")
}
function delet()
{
    f=0;
    printf("\nEnter Roll No you want to delete:");
    getline rollno;
    while(getline<"stud.txt"==1)
    {
      if(rollno!=$1)
              {
        print>"new.txt"
      }
    }
    system("rm stud.txt")
    system("mv new.txt stud.txt")
}
function search()
{
    printf("\nEnter Roll No of Student to Search:");
    getline rollno;
    f=0;
    while(getline<"stud.txt"==1)
    {       
      if(rollno==$1)
      {
        printf("\nRecord is present and Name is:%s",$2);
        f=1;
        break;
      }
    }
    if(f==0)
        printf("\nRecord is not present");
}
function main()
{
    printf("\n1.Create DataBase\n2.Delete a Record\n3.Modify a Record\n4.Search a   Record\n5.DisplayDataBase");           
    printf("\nEnter your choice: ");
           getline ch
      if (ch==1)
         create();
      if (ch==2)
         delet();
        if (ch==3)
         modify();
      if (ch==4)
         search();
      if (ch==5)
          display();
      printf("\n\nDo you want to continue(1:yes,0:no):");
       getline ans
      if (ans==1)
        system("awk -f ASSN2.awk")
}