Loading

Archive for 9 Jul 2012

Student Database in Shell

Shell Program to Implement Student Database

*****************************************************

printf "Enter a Filename::"
read fname
ch='y'
while [ $ch = 'y' ]
do
printf "\n\t\t***MENU***\n"
echo "1.Add Record"
echo "2.Display Record"
echo "3.Modify Record"
echo "4.Delete Record"
echo "5.Search Record"
echo "6.Exit"
echo "Enter a Choice::"
read ch
case "$ch" in
1)
printf "\n\t\t***Enter Student Information***"
printf "\nRoll no::"
read rno
printf "\nName::"
read name
printf "\nYear::"
read year
printf "\nBranch::"
read branch
printf "\nPercentage::"
read per
printf "%-5d|%-10s|%-5s|%-5s|%-5s\n" $rno $name $year $branch $per >>

$fname
;;
2)
if [ -e $fname ]
then
cat $fname
else
echo "File Does not Exist......"
fi
;;
3)
printf "Enter the Students Rno to Modify::"
read rno
grep "$rno" "$fname"
ans=$?
if [ $ans -eq 0 ]
then
echo "Record Found...."
grep -v "$rno" "$fname" > temp.dat
printf "\nName::"
read name
printf "\nRno"
read rno
printf "\nYear::"
read year
printf "\nBranch::"
read branch
printf "\nPercentage::"
read per
echo "$rno""|""$name""|""$year""|""$branch""|""$per" >> temp.dat
sort temp.dat > temp1.dat
rm temp.dat
rm "$fname"
mv temp1.dat "$fname"
echo "Record Modified"
else
echo "Record not Found"
fi
;;
4)
printf " \nEnter Roll no to Delete::"
read rno
grep "$rno" "$fname"
ans=$?
if [ $ans -eq 0 ]
then
echo "Record Found...."
grep -v "$rno" "$fname" > temp.dat
rm "$fname"
mv temp.dat "$fname"
printf "\nRecord Deleted!!!!!!!!!"
else
printf "\nRecord not Found :-("
fi
;;
5)
if [ -e $fname ]
then
echo "Enter Rno of Record::"
read rno
grep "^$rno" "$fname"
ans=$?
if [ $ans -eq 0 ]
then
echo "Record Found......."
else
echo "Record Not Found"
fi
else
echo "File does Not Exists :-)"
fi
;;
6)
exit
;;
*)
echo "Invalid Choice"
esac
printf "Do you want to Continue?"
read ch
done

Leave a comment

Menu Driven Shell Program 2

Menu Driven Shell Program to Perform Following Functions

 1. Greatest of 3 nos

2. Reverse a no

3. Sum of 4 digit no

4. Accept a word & no 'n'.Print word 'n' times.

*********************************************************************************

echo "1.Greatest of 3 nos"
echo "2.Reverse a no"
echo "3.Sum of 4 digit no"
echo "4.Accept a word & no 'n'.Print word 'n' times."
echo "Enter ur Choice::"
read choice
case "$choice" in
1)
echo "Enter Three  Nos::"
read n1 n2 n3
echo -e "\nGreatest no is::"
if [ $n1 -gt $n2 -a $n1 -gt $n3 ]
then
    echo $n1
elif [ $n2 -gt $n1 -a $n2 -gt $n1 ]
then
    echo $n2
else
    echo $n3
fi
;;
2)
echo "Enter a no::"
read n
on=$n
rev=""
while [ $n -gt 0 ]
do
    s=`expr $n % 10`
    n=`expr $n / 10`
    rev=$( echo $rev$s)
done
echo "$on is reversed to $rev"
;;
3)
echo -e "Enter a no::"
read n
sum=0

while [ $n -gt 0 ]
do
    s=`expr $n % 10`
    n=`expr $n / 10`
    sum=`expr $sum + $s`
done
echo "Sum of digits of no is $sum"
;;

4)
echo -e "Enter a Word::"
read word
echo -e "Enter a No::"
read n
i=1
while [ $n -gt 0 ]
do
n=`expr $n - 1`
echo $i ")" $word
i=`expr $i + 1`
done
;;
esac

Leave a comment

Menu Driven Shell Program 1

Menu Driven Shell Program to Perform Following Functions

1.Factorial of number  

2.Greatest number of 3  

3.Prime number or not  

4.number is palindrome or not  

5.string is palindrome or not

*******************************************************************************

ans="y"
while [ $ans = "y" ]    #loops till answer is yes
do
  echo "-----MENU-----"    #prints menu
  echo "1.Factorial of number"
  echo "2.Greatest number of 3"
  echo "3.Prime number or not"
  echo "4.number is palindrome or not"
  echo "5.string is palindrome or not"
  echo "6.Exit"
  echo –n "enter your choice :"   
  read ch    #reads users choice
  case $ch in
  1)echo –n  "Enter a number: "
     read num
     i=2
     res=1
     if [ $num -ge 2 ]
     then
        while [ $i -le $num ] #calculates factorial of given number
        do
            res=`expr $res \* $i`
            i=`expr $i + 1`
       done
   fi
   echo "Factorial of $num = $res"
   ;;
  2)echo –n "Enter the 1st number :"
     read num1
     echo –n "Enter the 2nd number :"
     read num2
     echo –n "Enter the 3rd number :"
     read num3
     if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]    #compare 3

numbers
     then
         echo "$num1 is biggest number!!!"
    elif  [ $num2 -gt $num3 ]
    then
        echo "$num2 is biggest number!!!"
    else
        echo "$num3 is biggest number!!!"
    fi
    ;;
  3)i=2
     flag=0
     echo –n "Enter the number :"
     read num
     while [ $i -lt `expr $num - 1` ]    #check whether number is prime
     do
        if [ `expr $num % $i` = 0 ]
        then
          flag=1    #if number is prime sets the flag
        fi
       i=`expr $i + 1`
    done
     if [ $flag = 1 ]
     then
        echo "$num is not prime."
    else
       echo "$num is prime."
    fi
    ;;
  4)l=0
    cnt=1
    tag=0
    echo –n "Enter a number :"
    read num
    l=`echo $num|wc -c`
    l=`expr $l - 1`
    lh=`expr $l / 2`
    while [ $cnt -le $lh ]    #loops till half length of number
    do
        c1=`echo $num|cut -c $cnt`
        c2=`echo $num|cut -c $l`
        if [ $c1 != $c2 ]    #compare first and last number
        then
            cnt=$lh
            tag=1
        fi
        cnt=`expr $cnt + 1`
        l=`expr $l - 1`
    done
    if [ $tag -eq 0 ]
    then
        echo "Number is Palindrome"
    else
       echo “Number is not Palindrome”
    fi
    ;;
  5)l=0
    cnt=1
    tag=0
    echo –n "Enter a String :"
    read str
    l=`echo $str|wc -c`
    l=`expr $l - 1`
    lh=`expr $l / 2`
    while [ $cnt -le $lh ]    #ckecks whether string is palindrome
    do
       c1=`echo $str|cut -c $cnt`
       c2=`echo $str|cut -c $l`
       if [ $c1 != $c2 ]
       then
             cnt=$lh
            tag=1
       fi
      cnt=`expr $cnt + 1`
      l=`expr $l - 1`
   done
    if [ $tag -eq 0 ]
    then
       echo "String is Palindrome"
    else
       echo "String is not Palindrome"
    fi
    ;;
  6)exit;;    #exit from shell script
  *)echo "Please enter correct choice !!!";;
  esac
  echo –n "Do you want to continue?"   
  read ans
done
echo "Thank you!!!"

Leave a comment

Optimal Binary Search Tree

C - Program to Implement Optimal Binary Search Tree Using Dynamic Programming


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

void con_obst(void);
void print(int,int);
float a[20],b[20],wt[20][20],c[20][20];
int r[20][20],n;

int main()
{
      int i;
      clrscr();
      cout<<"\n****** PROGRAM FOR OBST ******\n";
      cout<<"\nEnter the no. of nodes : ";
      cin>>n;
      cout<<"\nEnter the probability for successful search :: ";
      cout<<"\n-------------------------------------------------\n";
      for(i=1;i<=n;i++)
      {
            cout<<"p["<<i<<"]";
            cin>>a[i];
      }
      cout<<"\nEnter the probability for unsuccessful search :: ";
      cout<<"\n--------------------------------------------------\n";
      for(i=0;i<=n;i++)
      {
            cout<<"q["<<i<<"]";
            cin>>b[i];
      }
      con_obst();
      print(0,n);
      cout<<endl;
      getch();
}

void con_obst(void)
{
      int i,j,k,l,min;
      for(i=0;i<n;i++)
      { //Initialisation       
            c[i][i]=0.0;
            r[i][i]=0;
            wt[i][i]=b[i];
         // for j-i=1 can be j=i+1
            wt[i][i+1]=b[i]+b[i+1]+a[i+1];
            c[i][i+1]=b[i]+b[i+1]+a[i+1];
            r[i][i+1]=i+1;
      }
      c[n][n]=0.0;
      r[n][n]=0;
      wt[n][n]=b[n];
      //for j-i=2,3,4....,n
      for(i=2;i<=n;i++)
      {
            for(j=0;j<=n-i;j++)
            {
                  wt[j][j+i]=b[j+i]+a[j+i]+wt[j][j+i-1];
                  c[j][j+i]=9999;
                  for(l=j+1;l<=j+i;l++)
                  {
                        if(c[j][j+i]>(c[j][l-1]+c[l][j+i]))
                        {
                              c[j][j+i]=c[j][l-1]+c[l][j+i];
                              r[j][j+i]=l;
                        }
                  }
                  c[j][j+i]+=wt[j][j+i];
            }
            cout<<endl;
      }
      cout<<"\n\nOptimal BST is :: ";
      cout<<"\nw[0]["<<n<<"] :: "<<wt[0][n];
      cout<<"\nc[0]["<<n<<"] :: "<<c[0][n];
      cout<<"\nr[0]["<<n<<"] :: "<<r[0][n];
}

void print(int l1,int r1)
{
      if(l1>=r1)
      return;
      if(r[l1][r[l1][r1]-1]!=0)
      cout<<"\n Left child of "<<r[l1][r1]<<" :: "<<r[l1][r[l1][r1]-1];

      if(r[r[l1][r1]][r1]!=0)
      cout<<"\n Right child of "<<r[l1][r1]<<" :: "<<r[r[l1][r1]][r1];
      print(l1,r[l1][r1]-1);
      print(r[l1][r1],r1);
      return;
}


 Click Here to Download Source Code with Executable Program 

Leave a comment