Sunday 10 August 2014

C Programming for Beginners - 1 | C Language Tutorial for Beginners

C Programming for Beginners - 1 | C Language Tutorial for Beginners



https://www.youtube.com/watch?v=qlp8_zlMnSk


Robert Sebesta Web technology text book




https://www.dropbox.com/s/n68k0y135s86e0o/Programming%20the%20World-Wide%20Web-Sebesta%20Robert.rar

Write an awk script to compute gross salary of an employee accordingly to rule given below. If basic salary is < 10000 then HRA=15% of basic & DA=45% of basic. If basic salary is >=10000 then HRA=20% of basic & DA=50% of basic.


BEGIN{
printf"enter the basic salary:Rs"
getline bp<"/dev/tty"
if(bp<10000)
{
hra=.15*bp
da=.45*bp
}
else
{
hra=.2*bp
da=.5*bp
}
gs=bp+hra+da
printf"gross salary=Rs.%.2f\n",gs

Write an awk script to find out total number of books sold in each discipline as well as total book sold using associate array down table as given below. Electrical 34 Mechanical-1 67 Electrical-1 80 Computer Science 43 Mechanical 65 Civil-1 198 Civil 198 Computer Science-1 64


BEGIN{
printf "enter the mechanical books:"
getline mc <"/dev/tty"
printf "enter the electrical books:"
getline ec <"/dev/tty"
printf "enter the computer books:"
getline cs <"/dev/tty"
printf "enter the civil books:"
getline ci <"/dev/tty"
}
{
if(NR==1)
tot1=$2-mc
if(NR==2)
tot2=$2-ec
if(NR==3)
tot3=$2-cs
if(NR==4)
tot4=$2-ci
}
{
if(NR==4)
{
printf "total mechanical books present:%d\n",tot1
printf "total electrical books present:%d\n",tot2
printf "total computer books present:%d\n",tot3
printf "total civil books present:%d\n",tot4
}
}

Write an awk script to delete duplicated line from a text file. The order of the original Lines must remain unchanged.


BEGIN {i=1}
{
flag=1;
for(j=1;j<i && flag ; j++)
{
if(x[j]==$0)
flag=0;
}
if (flag)
{
x[j]=$0;
printf("\n%s",x[i]);
i++;
}
}

Write an awk script that accepts date argument in the form of mm-dd-yy and displays it in the form if day, month, and year. The script should check the validity of the argument and in the case of error, display a suitable message.


BEGIN {
FS="-"
f=1;
printf("enter date with (mm-dd-yy) format:");
getline < "/dev/tty"
if(((($3%4!=0) && ($1==2) && ($2>28)) || (($3%4==0) && ($1==2) && ($2>29))) ||
((($1==1) || ($1==3) || ($1==5) || ($1==7) || ($1==8) || ($1==10) || ($1==12)) && ($2>31)) ||
((($1==4) || ($1==6) || ($1==9) || ($1==11)) && ($2>30)) || ($1<1) || ($2<1) || ($3<1) || ($1>12))
f=0;
if(f==0)
printf("you have entered an invalid date\n");
else
printf("\n \tThe date= %d\nThe month=%d\nThe year is=%d\n is valid date\n",$2,$1,$3);
}

Write a shell script that accept the file name, starting and ending line number as an argument and display all the lines between the given line number.


if [ $# -ne 3 ]
then
echo "Pass minimum three arguments"
exit
fi
c=`cat $1 | wc -l`
if [ $2 -le 0 -o $3 -le 0 -o $2 -gt $3 -o $3 -gt $c ]
then
echo "Invaid Input"
exit
fi
sed -n "$2,$3p" $1

Write a shell script that accepts two integers as its argument and compute the value of first number raised to the power of second number.


if [ $# -ne 2 ]
then
echo "Invalid number of arguments"
exit
fi
pwr=`echo $1^$2 |bc`
echo "$1 raised to $2 is : $pwr"

Write a shell script that reports the logging in of a specified user within one minute after he/she log in. The script automatically terminate if specified user does not log in during a specified period of time.


a=`date +%M`
b=`expr $a + 1`
while [ $b -gt `date +%M` ]
do
who | grep $1
if [ $? -eq 0 ]
then
echo "$1 has logged in 1 minute"
exit
fi
done
echo "$1 has not looged in within 1 minute"

Write a shell script that determine the period for which a specified user is working on system.


echo -e "enter the user name :\c"
read usr
tuser=`who | tr -s " " | head -1 | cut -d " " -f1`
if [ "$tuser" = "$usr" ]
then
tm=`who | tr -s " " | head -1 | cut -d " " -f4`
uhr=`echo $tm | cut -d ":" -f1`
umin=`echo $tm | cut -d ":" -f2`
shr=`date "+%H"`
smin=`date "+%M"`
if [ $smin -lt $umin ]
then
shr=`expr $shr - 1`
smin=`expr $smin + 60`
fi
h=`expr $shr - $uhr`
m=`expr $smin - $umin`
echo "user name : $usr"
echo "login period : $h : $m"
else
echo "Invalid User"
fi

Write a shell script that accept a list of filenames as its argument, count and report occurrence of each word that is present in the first argument file on other argument files.


if [ $# -ne 2 ]
then
echo "Error :invalid no of arguments "
exit
fi
str=`cat $1 | tr '\n' ' '`
for a in $str
do
echo "word=$a , count=`grep -c "$a" $2` "
done

Write a shell script that gets executed displays the message either “Good Morning” or “Good Afternoon” or “Good Evening” depending upon time at which the user logs in.


echo "Greetings of the day"
echo `date`
h=`date |cut -c 12-13`
if [ $h -ge 0 -a $h -lt 12 ]
then
echo "Good Morning"
elif [ $h -ge 12 -a $h -lt 18 ]
then
echo "Good Afternoon"
elif [ $h -ge 18 -a $h -lt 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi

Write a shell script that delete all lines containing a specific word in one or more file supplied as argument to it.


if [ $# -eq 0 ]
then
echo "Please enter one or more filenames as argument"
exit
fi
echo "Enter the word to be searched in files"
read word
for file in $*
do
sed "/$word/d" $file | tee tmp
mv tmp $file
done

Write a shell script that compute gross salary of an employee, accordingly to rule given below.


If basic salary is < 15000 then HRA=10% of basic 7 DA=90% of basic.
If basic salary is >=15000 then HRA=500 of basic & DA=98% of basic.
Echo ―enter Basic salary‖
read basic
if [ $basic -lt 15000 ]
then
hra=`expr 10 \* $basic / 100`
da=`expr 90 \* $basic / 100`
else
hra=`expr 50 \* $basic / 100`
da=`expr 98 \* $basic / 100`
fi
gs=`expr $basic + $hra + $da`
echo "Gross salary : Rs. $gs"

Write a shell script to compute the sum of number passed to it as argument on command line and display the result.


sum=0
for i in $*
do
sum=`expr $sum + $i`
done
echo "Sum of Number is : $sum"

Write a shell script using expr command to read in a string and display a suitable message if it does not have at least 10 characters.


echo "Enter a String"
read str
c=`expr "$str" : '.*'`
if [ $c -lt 10 ]
then
echo "The String has Less than 10 charecter"
else
echo "The String has $c charecters"
fi

Write a shell script to find smallest of three numbers that are read from keyboard. echo "Enter the Three Numbers"


read a b c
if [ $a -le $b -a $a -le $c ]
then
echo "$a is Smallest"
elif [ $b -le $c -a $b -le $a ]
then
echo "$b is Smallest"
else
echo "$c is Smallest"
fi 

Write a shell script to display the calendar for current month with current date replaced by * or ** depending on whether the date has one digit or two digits.


set `date`
y=$3
if [ $y -le 9 ]
then
cal |sed "s/$3/*/"
else
cal |sed "s/$3/**/"
fi

Write a shell script that accepts as filename as argument and display its creation time if file exist and if it does not send output error message

.
if [ $# -eq 0 ]
then
echo "No argument"
exit
fi
if [ -f $1 ]
then
time=`ls -l $1|cut -c 33-59`
echo "File $1 has created on $time"
else
echo "File $1 does not exist"
fi

Write a shell script that accept one or more filenames as argument and convert all of them to uppercase, provided they exist in current directory.


for f in $*
do
if [ -e $f ]
then
cat $f|tr "[a-z]" "[A-Z]">tmp
mv tmp $f
else
echo "File $f does not exist"
fi
done

Create a script file called file-properties that reads a file name entered and outputs it properties.


echo "Enter a file name"
read file
if [ -f $file ]
then
set - -`ls -l $file`
echo "file permission : $1"
echo "number of links : $2"
echo "User name : $3"
echo "Owner name : $4"
echo "Block size : $5"
echo "Date of modification : $6 : $7"
echo "Time of modification : $8"
echo "Name of file : $9"
else
echo "File does not exist"
fi 

Write shell script to implement terminal locking (similar to the lock command). It should prompt the user for a password. After accepting the password entered by the user, it must prompt again for the matching password as confirmation and if match occurs, it must lock the keyword until a matching password is entered again by the user, Note that the script must be written to disregard BREAK, control-D. No time limit need be implemented for the lock duration.


stty –echo
while true
do
clear
echo "Enter the paasword"
read pass1
echo "Re enter the password"
read pass2
if [ $pass1 = $pass2 ]
then
echo "Terminal locked"
echo "To unlock enter the password"
pass1=""
until [ "$pass1" = "$pass2" ]
do
read pass1
done
echo "Terminal unlocked"
stty echo
exit
else
echo "Password mismatch retype it"
fi
done

Write a shell script which accepts valid log-in names as arguments and prints their corresponding home directories, if no arguments are specified, print a suitable error message.


Clear
if [ $# -eq 0 ]
then
echo "No command line argument passed"
exit
fi
while [ $1 ]
do
cat /etc/passwd | cut -d ":" -f1 | grep "^$1" > temp
ck=`cat temp`
if [ "$ck" != "$1" ]
then
echo "ERROR:$1 is an invalid login name"
else
echo "Home Directory for $1 is"
echo `cat /etc/passwd | grep "^$1" | cut -d ":" -f6`
fi
shift
done

Write a shell script that takes a valid directory name as an argument and recursively descend all the subdirectories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard output.


if [ $# -ne 1 ]
then
echo "No argument"
exit
fi
if [ ! -e $1 ]
then
echo "The given directory does not exist"
exit
fi
echo "The file with maximum length $1 dir is"
ls -lR $1|tr -s " "|cut -d " " -f 5,10|sort -n|tail -n 1

Write a shell script that accepts two file names as arguments, checks if the permissions for these files are identical and if the permissions are identical, output common permissions and otherwise output each file name followed by its permissions.


if [ $# -ne 2 ]
then
echo "No arguments"
elif [ ! -e $1 -o ! -e $2 ]
then
echo "FIle does not exist"
else
p1=`ls -l $1|cut -c2-10`
p2=`ls -l $2|cut -c2-10`
if [ $p1 == $p2 ]
then
echo "File permissions are equal & is $p1"
else
echo "File permissions are not equal"
echo "1st file $p1"
echo "2nd file $p2"
fi
fi

Tuesday 29 July 2014

USEFUL LINKS

1. elearning.vtu.ac.in/

2. Coursera

3. Slide Share

4. IndiaBix.com


5. studyittoday.blogspot.com  :)

DBMS Lab5 [MySQL]


Students are advised to learn to create their own tables

 
Exercise: 5
Data requirements of movie industry are captured. Each movie is identified by title and
year of release. Each movie has length in minutes and classified under one genres (like action, horror etc.). Each movie has a plot outline. Production companies are identified by name and each has an address. A production company produces one or more movies. Actors are identified by id. Other details like name and date of birth ofactors are also stored. Each actor acts in one or more movies. Each actor has a role in movie. Directors are identified by id. Other details like name and date of birth of directors are also stored. Each director directs one or more movies. Each movie has one or more actors and one or more directors and is produced by a production company. 

ER-Diagram:



Relational schema:
Tables
mysql> create table movie(m_no int primary key,title varchar(20),yor int,lenght int,genres varchar(10));
Query OK, 0 rows affected (0.09 sec)
mysql> insert into movie values(
001,"akasmika",1992,180,"action");
Query OK, 1 row affected (0.03 sec)
mysql> insert into movie values( 002,"six-fivetwo",2013,150,"horror");
Query OK, 1 row affected (0.03 sec)
mysql> insert into movie values( 003,"kalla-malla-sulla",2012,150,"comedy")
Query OK, 1 row affected (0.04 sec)
mysql> insert into movie values( 004,"super",2010,150,"action");
Query OK, 1 row affected (0.03 sec)
mysql> insert into movie values( 005,"nagarahavu",1991,150,"mystery");
Query OK, 1 row affected (0.03 sec)
mysql> select * from movie;
mysql> create table production(p_no int primary key,name varchar(20),address varchar(20));
Query OK, 0 rows affected (0.10 sec)
mysql> insert into production values(121,"vajreshwari","bangalore");
Query OK, 1 row affected (0.02 sec)
mysql> insert into production values( 122,"old_films","madras");
Query OK, 1 row affected (0.03 sec)
mysql> insert into production values( 123,"raj_movies","madras");
Query OK, 1 row affected (0.02 sec)
mysql> insert into production values( 124,"sri_movies","bangalore");
Query OK, 1 row affected (0.03 sec)
mysql> insert into production values( 125,"golden_movies","bangalore");
Query OK, 1 row affected (0.04 sec)
mysql> select * from production;

mysql> create table produced(p_no int,m_no int,foreign key(p_no) references production(p_no),foreign key(m_no) references movie(m_no));
Query OK, 0 rows affected (0.12 sec)
mysql> insert into produced values(121,001);
Query OK, 1 row affected (0.03 sec)
mysql> insert into produced values(122,002);
Query OK, 1 row affected (0.02 sec)
mysql> insert into produced values(123,003);
Query OK, 1 row affected (0.03 sec)
mysql> insert into produced values(124,004);
Query OK, 1 row affected (0.02 sec)
mysql> insert into produced values(125,005);
Query OK, 1 row affected (0.03 sec)
mysql> select * from produced;
mysql> create table actor(a_id int primary key,name varchar(10),dob date);
Query OK, 0 rows affected (0.09 sec)
mysql> insert into actor values(101,"raj_kumar",19470424);
Query OK, 1 row affected (0.03 sec)
mysql> insert into actor values(102,"shankar_nag",19470602);
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> insert into actor values(103,"vishnuvardhan",19620505);
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> insert into actor values(104,"ambareesh",19600525);
Query OK, 1 row affected (0.04 sec)
mysql> insert into actor values(105,"upendra",19700515);
Query OK, 1 row affected (0.04 sec)
mysql> insert into actor values(106,"srk",19800516);
Query OK, 1 row affected (0.04 sec)
mysql> select * from actor;
mysql> create table acts(a_id int,m_no int,foreign key(a_id) references actor(a_id),foreign key(m_no) references movie(m_no));
Query OK, 0 rows affected (0.11 sec)
mysql> insert into acts values(101,1);
Query OK, 1 row affected (0.02 sec)
mysql> insert into acts values(102,2);
Query OK, 1 row affected (0.02 sec)
mysql> insert into acts values(103,3);
Query OK, 1 row affected (0.03 sec)
mysql> insert into acts values(104,4);
Query OK, 1 row affected (0.03 sec)
mysql> insert into acts values(105,5);
Query OK, 1 row affected (0.03 sec)
mysql>insert into acts values(106,5);
Query OK, 1 row affected (0.03 sec)
mysql> select * from acts;
mysql> create table director(d_id int primary key,name varchar(10),dob date);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into director values(333,"puttanna",19700424);
Query OK, 1 row affected (0.03 sec)
mysql> insert into director values(334,"siddayya",19700424);
Query OK, 1 row affected (0.03 sec)
mysql> insert into director values(335,"bhargava",19700425);
Query OK, 1 row affected (0.03 sec)
mysql> insert into director values(336,"kumar",19700525);
Query OK, 1 row affected (0.02 sec)
mysql> insert into director values(337,"bangaru",19700516);
Query OK, 1 row affected (0.03 sec)
mysql> insert into director values(338,"upendra",19890516);
Query OK, 1 row affected (0.02 sec)
mysql> select * from director;

mysql> create table directs(d_id int,m_no int,foreign key(d_id) references director(d_id),foreign key(m_no) references movie(m_no));
Query OK, 0 rows affected (0.12 sec)
mysql> insert into directs values(333,1);
Query OK, 1 row affected (0.04 sec)
mysql> insert into directs values(334,2);
Query OK, 1 row affected (0.03 sec)
mysql> insert into directs values(335,3);
Query OK, 1 row affected (0.03 sec)
mysql> insert into directs values(336,4);
Query OK, 1 row affected (0.03 sec)
mysql> insert into directs values(337,5);
Query OK, 1 row affected (0.03 sec)
mysql> insert into directs values(338,4);
Query OK, 1 row affected (0.02 sec)
mysql> select * from directs;


Queries:
a)List the details of horror movies released in 2012 and directed by more than 2 directors.
mysql> select distinct m.* from movie m, directs d where m.genres="horror" and yor=2012 and m.m_no=d.m_no  having count(d.m_no)>=2;

b) List the details of actors who acted in movies having same titles but released before 2000 and after 2010.
mysql> select a.* from actor a,acts ac1,acts ac2,movie m1,movie m2
where m1.m_no=m2.m_no and m1.yor<=2000 and m2.yor>=2010 and
 m1.m_no=ac1.m_no and m2.m_no=ac2.m_no;

c) List the details of production companies producing maximum movies.
mysql> select * from production where p_no=(select p_no from produced where m_no=(select max(m_no) from produced));


d) List the details of movies where director and actor have same date of birth.
mysql> select m.* from movie m,actor a,acts ac,director d where a.a_id=ac.a_id and m.m_no=ac.m_no and a.dob=d.dob;

e)  Retrieve  the  names  of  directors  directed  all  the movies  produced  by  any  one  production company.
mysql> select name from director where d_id=(select d.d_id from production p,produced p1,directs d where p.name="vajreshwari" and p.p_no=p1.p_no and p1.m_no=d.m_no);

 
- |