data and address

Upload: subramaniam-ganesan

Post on 05-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Data and Address

    1/78

    /* Data and address */

    #include

    main()

    {

    int x;

    x=10;

    printf("Value of x is %d",x);

    printf("\nAddress of x is %u",&x);

    printf("\nValue of x is %d\n",*(&x));

    }

  • 7/31/2019 Data and Address

    2/78

    "pointer1.c" 10L, 169C written

    [1011@localhost ]$ gcc pointer1.c

    [1011@localhost ]$ ./a.out

    Value of x is 10

    Address of x is 3215134992

    Value of x is 10

    [1011@localhost ]$

    /* pointer variables */

    #include

    main()

    {

    int x, y, *iptr;

    float *fptr;

    x =125;

    iptr = &x ; /* iptr points to x */

    y = 23;

    /* fptr = &y; is erroneous */

    printf("X value is %d and stored at %u\n", x, &x);

    printf("Y value is %d and stored at %u\n", y, &y);

    printf("\nInt pointer holds the address %u\n", iptr);

    printf("Accessing value thru pointer : %d\n", *iptr);

    iptr = &y; /* iptr points to y */

    printf("\nInt pointer now holds the address %u\n", iptr);

  • 7/31/2019 Data and Address

    3/78

    printf("Accessing value thru pointer : %d\n", *iptr);

    printf("\nSize of int pointer: %d bytes", sizeof(iptr));

    printf("\nSize of float pointer: %d bytes", sizeof(fptr));

    printf("\n\nAddress of main function is %u\n", main);

    }

  • 7/31/2019 Data and Address

    4/78

    "pointer2.c" [New] 21L, 690C written

    [1011@localhost ]$ gcc pointer2.c

    [1011@localhost ]$ ./a.out

    X value is 125 and stored at 3217977096

    Y value is 23 and stored at 3217977092

    Int pointer holds the address 3217977096

    Accessing value thru pointer : 125

    Int pointer now holds the address 3217977092

    Accessing value thru pointer : 23

    Size of int pointer: 4 bytes

    Size of float pointer: 4 bytes

    Address of main function is 134513540

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    5/78

  • 7/31/2019 Data and Address

    6/78

    /* pointer variables */

    #include

    main()

    {

    int x=12, *p1, **p2;

    float z=8.5;

    void *ptr; /* Generic pointer */

    ptr = &x; /* ptr points to x (int) */

    ptr = &z; /* ptr points to y (float) */

    p1 = &x;

    p2 = &p1; /* Pointer to pointer */

    printf("X value is %d and stored at %u\n", x, &x);

    printf("\nPointer holds the address %u\n", p1);

    printf("Accessing value thru pointer : %d\n", *p1);

    printf("Pointer is stored at location : %u/n", &p1);

    printf("\nPointer-to-pointer holds the address %u\n", &p2);

    printf("Accessing value thru ptr-to-ptr : %d\n", **p2);

    printf("\nSize of ptr-to-ptr: %d bytes\n", sizeof(ptr));

    }

  • 7/31/2019 Data and Address

    7/78

  • 7/31/2019 Data and Address

    8/78

    "pointer3.c" [New] 19L, 619C written

    [1011@localhost ]$ gcc pointer3.c

    [1011@localhost ]$ ./a.out

    X value is 12 and stored at 3219886300

    Pointer holds the address 3219886300

    Accessing value thru pointer : 12

    Pointer is stored at location : 3219886296/n

    Pointer-to-pointer holds the address 3219886292

    Accessing value thru ptr-to-ptr : 12

    Size of ptr-to-ptr: 4 bytes

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    9/78

  • 7/31/2019 Data and Address

    10/78

    # Menu program

    ch='y'

    while [ $ch == 'y' ]

    do

    echo -e "\tMENU

    1. List of files

    2. Working Directory

    3. Date and Time

    4. Users of the system

    5. Calendar

    Enter the option : \c"

  • 7/31/2019 Data and Address

    11/78

    read choice

    case "$choice" in

    1) ls -1 ;;

    2) pwd ;;

    3) date ;;

    4) who ;;

    5) cal

    esac

    echo -n "Do you wish to continue (y/n) : "

    read ch

    done

  • 7/31/2019 Data and Address

    12/78

    "command2.sh" [New] 23L, 317C written

    [1011@localhost ]$ sh command3.sh

    sh: command3.sh: No such file or directory

    [1011@localhost ]$ sh command2.sh

    MENU

    1. List of files

    2. Working Directory

    3. Date and Time

    4. Users of the system

    5. Calendar

    Enter the option : 4

    1019 pts/1 2012-03-30 19:17 (192.168.10.20)

  • 7/31/2019 Data and Address

    13/78

    root :0 2012-03-21 13:48

    1007 pts/2 2012-03-30 19:17 (192.168.10.9)

    1011 pts/3 2012-03-30 19:18 (192.168.10.34)

    1023 pts/4 2012-03-30 19:51 (192.168.10.19)

    1022 pts/5 2012-03-30 19:20 (192.168.10.21)

    1014 pts/6 2012-03-30 19:29 (192.168.10.27)

    1016 pts/7 2012-03-30 19:22 (192.168.10.26)

    1012 pts/8 2012-03-30 19:22 (192.168.10.33)

    1008 pts/10 2012-03-30 20:03 (192.168.10.35)

    1004 pts/11 2012-03-30 19:39 (192.168.10.7)

    1003 pts/12 2012-03-30 20:45 (192.168.10.6)

    1009 pts/13 2012-03-30 20:42 (192.168.10.185)

    1017 pts/14 2012-03-30 20:19 (192.168.10.29)

    1024 pts/16 2012-03-30 20:43 (192.168.10.15)

    Do you wish to continue (y/n) : n

    [1011@localhost ]$ sh command2.sh

  • 7/31/2019 Data and Address

    14/78

    #! /bin/bash

    x=`date +%H`

    mrd=`date +%p`

    if [ $mrd=='am' ]

    then

    if [ $x -le 11 ]

    then

    echo "Good Morning"

    fi

    else

    if [ $x -le 2 ]

    then

    echo "Good Afternoon"

    elif [ $x -le 6 ]

    then

    echo "Good Evening"

    else

    echo "Good Night"

    fi

    fi

  • 7/31/2019 Data and Address

    15/78

    # Number of days in a month

    mth=`date +%m`

    mn=`date +%B`

    case $mth in

    02)echo "February usually has 28 days"

    echo "If leap year, then it ha 29 days";;

    04|06|09|11)echo "The current month $mn has 30 days";;

    *) echo "The current month $mn has 31 days"

    Esac

    for x in

    do

    ls -R $x

    done

  • 7/31/2019 Data and Address

    16/78

  • 7/31/2019 Data and Address

    17/78

    "command4.sh" [New] 5L, 27C written

    [1011@localhost ]$ sh command4.sh

    [1011@localhost ]$

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    18/78

    echo -n "Enter filename :"

    read fname

    if [ -e $fname ]

    then

    if [ -f $fname ]

    then

    echo "Regular file"

    elif [ -d $fname ]

  • 7/31/2019 Data and Address

    19/78

    then

    echo "Directory"

    else

    echo "Special file"

    fi

    else

    echo "File does not exist"

    fi

  • 7/31/2019 Data and Address

    20/78

    "command5.sh" [New] 16L, 206C written

    [1011@localhost ]$ sh command5.sh

    Enter filename :samp.c

    File does not exist

    [1011@localhost ]$

    #!/bin/bash

    #Detect the user at logon

  • 7/31/2019 Data and Address

    21/78

    while true

    do

    if who|grep $1>/dev/null

    then

    echo $1 is logged in

    exit

    fi done

    # Duplicate file removal

    if cmp $1 $2

    then

    echo "Files $1, $2 are identical"

    rm $2

  • 7/31/2019 Data and Address

    22/78

    echo "$2 file deleted"

    else

    echo"Files $1, $2 are distinct"

    fi

  • 7/31/2019 Data and Address

    23/78

    "command7.sh" 9L, 146C written

    [1011@localhost ]$ sh command7.sh 1,1 All

    cmp: missing operand after `cmp'

    cmp: Try `cmp --help' for more information.

    command7.sh: line 8: echoFiles , are distinct: command not found

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    24/78

    while true

    do

    gcc -o $1.out $1.c

    case "$?" in

    0)echo "Executing..."

    ./$1.out

    exit;;

    *)

    sleep 5

    vi $1.c

    esac

    done

  • 7/31/2019 Data and Address

    25/78

    # Odd or even using if-else

    echo -n "Enter a non-zero number :"

    read num

    rem=`expr $num % 2`

    if [ $rem -eq 0 ]

  • 7/31/2019 Data and Address

    26/78

    then

    echo "$num is Even"

    else

    echo "$num is Odd"

    fi

  • 7/31/2019 Data and Address

    27/78

    "condition1.sh" 10L, 163C written

    [1011@localhost ]$ sh condition1.sh

    Enter a non-zero number :12

    12 is Even

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    28/78

    # Biggest using logical expression

    echo -n "Give value for A B and C:"

    read a b c

    if [ $a -gt $b -a $a -gt $c ]

    then

    echo "A is the Biggest number"

    elif [ $b -gt $c ]

    then

    echo "B is the Biggest number"

    else

    echo "C is the Biggest number"

    fi

  • 7/31/2019 Data and Address

    29/78

  • 7/31/2019 Data and Address

    30/78

    "condition2.sh" [New] 12L, 242C written

    [1011@localhost ]$ sh condition2.sh

    Give value for A B and C:4 3 4

    C is the Biggest number

    [1011@localhost ]$

    # Leap year

    echo -n "Enter a year :"

    read year

    rem1=`expr $year % 4`

    rem2=`expr $year % 100`

    rem3=`expr $year % 400`

    if [ $rem3 -eq 0 ]

    then

    echo "$year is a Leap Year"

    elif [ $rem2 -ne 0 -a $rem1 -eq 0 ]

    then

    echo "$year is a Leap Year"

    else

    echo "$year is Not a leap year"

    fi

  • 7/31/2019 Data and Address

    31/78

  • 7/31/2019 Data and Address

    32/78

    "condition3.sh" [New] 15L, 278C written

    [1011@localhost ]$ sh condition3.sh

    Enter a year :1900

    1900 is Not a leap year

    [1011@localhost ]$

    echo -n "Enter the mark:"

    read mark

    if [ $mark -gt 90 ]

    then

    echo "S Grade"

    elif [ $mark -gt 80 ]

    then

    echo "A Grade"

    elif [ $mark -gt 70 ]

    then

    echo "B Grade"

  • 7/31/2019 Data and Address

    33/78

    elif [ $mark -gt 60 ]

    then

    echo "C Grade"

    elif [ $mark -gt 55 ]

    then

    echo "D Grade"

    elif [ $mark -ge 50 ]

    then

    echo "E Grade"

    else

    echo "U Grade"

    fi

  • 7/31/2019 Data and Address

    34/78

    "condition4.sh" [New] 23L, 309C written

    [1011@localhost ]$ sh condition4.sh

    Enter the mark:65

    C Grade

    [1011@localhost ]$

    echo -n "Enter the first string :"

    read s1

    echo -n "Enter the second string :"

    read s2

  • 7/31/2019 Data and Address

    35/78

    if [ $s1 == $s2 ]

    then

    echo "Strings are the same"

    else

    echo "Strings are distinct"

    fi

  • 7/31/2019 Data and Address

    36/78

    "condition5.sh" [New] 10L, 174C written

    [1011@localhost ]$ sh condition5.sh

    Enter the first string :ece-a

    Enter the second string :ECE-A

    Strings are distinct

  • 7/31/2019 Data and Address

    37/78

    [1011@localhost ]$

    echo -n "Enter employee basic pay : "

    read basic

    if [$basic -gt 30000 ]

    then

    hra=`expr 5 \* $basic / 100 `

    da=`expr 5 \* $basic / 100`

    tax=`expr 10 \* $basic /100`

    elif [ $basic -gt 20000 ]

    then

    hra=`expr 4 \* $basic / 100`

    da=`expr 3 \* $basic / 100`

    tax=`expr 8 \* $basic /100`

    else

    hra=`expr 3 \* $basic / 100`

    da=`expr 2 \* $basic / 100`

    tax=`expr 5 \* $basic / 100`

    fi

    gross=`expr $basic + $da + $hra`

    netpay=`expr $gross - $tax`

    echo "Gross pay : $gross"

    echo "Net pay : $netpay"

  • 7/31/2019 Data and Address

    38/78

  • 7/31/2019 Data and Address

    39/78

    "condition6.sh" [New] 21L, 487C written

    [1011@localhost ]$ sh condition6.sh

    Enter employee basic pay : 12000

    condition6.sh: line 3: [12000: command not found

    Gross pay : 12600

    Net pay : 12000

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    40/78

    # Multiplication table using for loop

    clear

    echo -n "Which multiplication table? : "

    read n

    for x in 1 2 3 4 5 6 7 8 9 10

    do

    p=`expr $x \* $n`

    echo -n "$n X $x = $p"

    sleep 1

    done

    # Multiplication table using for loop

    clear

  • 7/31/2019 Data and Address

    41/78

    Which multiplication table? : 6

    6 X 1 = 66 X 2 = 126 X 3 = 186 X 4 = 246 X 5 = 306 X 6 = 366 X 7 = 426 X 8 = 486

    X 9 = 546 X 10 = 60[1011@localhost ]$

    # Armstrong number using while loop

    echo -n "Enter a number : "

    read n

    a=$n

    s=0

    while [ $n -gt 0 ]

    do

    r=`expr $n % 10`

    s=`expr $s + \( $r \* $r \* $r \)`

    n=`expr $n / 10`

    done

    if [ $a -eq $s ]

    then

  • 7/31/2019 Data and Address

    42/78

    echo "Armstrong Number"

    else

    echo -n "Not an Armsrong number"

    fi

  • 7/31/2019 Data and Address

    43/78

    "loop2.sh" 17L, 263C written

    [1011@localhost ]$ sh loop2.sh 6,19 All

    Enter a number : 370

    Armstrong Number

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    44/78

    # To reverse a number using while loop

    echo -n "Enter a number : "

    read n

    rd=0

    while [ $n -gt 0 ]

    do

    rem=`expr $n % 10`

    rd=`expr $rd \* 10 + $rem`

    n=`expr $n / 10`

    done

    echo "Reversed number is $rd"

  • 7/31/2019 Data and Address

    45/78

  • 7/31/2019 Data and Address

    46/78

  • 7/31/2019 Data and Address

    47/78

  • 7/31/2019 Data and Address

    48/78

    # Fibonacci series using while loop

    echo -n "Enter number of terms : "

    read n

    echo "Fibonacci series"

    f1=0

    f2=1

    echo -n "$f1 "

    echo -n " $f2 "

  • 7/31/2019 Data and Address

    49/78

    i=2

    while [ $i -lt $n ]

    do

    f3=`expr $f1 + $f2`

    echo -n " $f3 "

    f1=$f2

    f2=$f3

    i=`expr $i + 1`

    done

    echo

  • 7/31/2019 Data and Address

    50/78

    "loop4.sh" 18L, 247C written

    [1011@localhost ]$ sh loop4.sh

    Enter number of terms : 8

    Fibonacci series

    0 1 1 2 3 5 8 13

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    51/78

    echo -n "Enter the number : "

    read n

    i=2

    m=`expr $n / 2`

    until [ $i -gt $m ]

    do

    q=`expr $n % $i`

    if [ $q -eq 0 ]

    then

    echo "Not a prime number"

    exit

    fi

    i=`expr $i + 1`

    done

    echo "Prime number"

  • 7/31/2019 Data and Address

    52/78

    "loop5.sh" [New] 15L, 193C written

  • 7/31/2019 Data and Address

    53/78

    [1011@localhost ]$ sh loop5.sh

    Enter the number : 17

    Prime number

    [1011@localhost ]$

    echo -n "Enter a positive number : "

    read n

    f=1

    until [ $n -lt 1 ]

    do

    f=`expr $f \* $n`

    n=`expr $n - 1`

    done

    echo "Factorial value : $f"

  • 7/31/2019 Data and Address

    54/78

  • 7/31/2019 Data and Address

    55/78

    "loop6.sh" [New] 10L, 138C written

    [1011@localhost ]$ sh loop6.sh

    Enter a positive number : 10

    Factorial value : 3628800

    [1011@localhost ]$

    echo -n "Enter N value : "

    read n

    sum=0

  • 7/31/2019 Data and Address

    56/78

    i=1

    until [ $i -gt $n ]

    do

    sum=`expr $sum + $i`

    i=`expr $i + 1`

    done

    echo "The sum of n numbers is $sum"

  • 7/31/2019 Data and Address

    57/78

    "loop7.sh" [New] 10L, 145C written

    [1011@localhost ]$ sh loop7.sh

    Enter N value : 26

    The sum of n numbers is 351

  • 7/31/2019 Data and Address

    58/78

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    59/78

    clear

    pc=0

    s=0

    i=0

    until false

    do

    echo -n "Enter a number (9999 to quit) : "

    read n

    if [ $n -eq 9999 ]

    then

    break

  • 7/31/2019 Data and Address

    60/78

    fi

    i=`expr $i + 1`

    if [ $n -le 0 ]

    then

    continue

    fi

    pc=`expr $pc + 1`

    s=`expr $s + $n`

    done

    avg=`expr "scale=2; $s / $pc" | bc`

    echo "Total No. of entries : $i"

    echo "No. of positive datas : $pc"

    echo "Positive aggregate : $avg"

    clear

    pc=0

    Enter a number (9999 to quit) : 32

    Enter a number (9999 to quit) : 78

    Enter a number (9999 to quit) : 0

    Enter a number (9999 to quit) : 11

    Enter a number (9999 to quit) : 47

    Enter a number (9999 to quit) : -9

    Enter a number (9999 to quit) : 12

    Enter a number (9999 to quit) : 7

  • 7/31/2019 Data and Address

    61/78

    Enter a number (9999 to quit) : 9999

    Total No. of entries : 8

    No. of positive datas : 6

    Positive aggregate : 31.16

    [1011@localhost ]$

    # vowel with multiple values in a pattern

    echo -n "Key in a lower case charsacter :"

    read choice

    case $choice in

    a|e|i|o|u) echo "It's a vowel";;

    *) echo "It's a consonant"

    esac

  • 7/31/2019 Data and Address

    62/78

  • 7/31/2019 Data and Address

    63/78

    "multiway1.sh" [New] 7L, 178C written

    [1011@localhost ]$ sh multiway1.sh

    Key in a lower case charsacter :e

    It's a vowel

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    64/78

  • 7/31/2019 Data and Address

    65/78

    #Arithmetic operations--multiple statements in a block

    echo -n "Enter the two numbers :"

    read a b

    echo " 1. Addition"

    echo " 2. Subtraction"

    echo " 3. Multiplication"

    echo " 4. Division"

    echo " 5. Modulo Division"

    echo -n "Enter the option : "

    read option

    case $option in

  • 7/31/2019 Data and Address

    66/78

    1) c=`expr $a + $b`

    echo "$a + $b = $c";;

    2) c=`expr $a - $b`

    echo "$a - $b = $c";;

    3) c=`expr $a \* $b`

    echo "$a * $b = $c";;

    4) c=`expr $a / $b`

    echo "$a / $b = $c";;

    5) c=`expr $a % $b`

    echo "$a % $b = $c";;

    *) echo "Invalid option"

    esac

  • 7/31/2019 Data and Address

    67/78

    "multiway2.sh" 23L, 513C written

    [1011@localhost ]$ sh multiway2.sh

    Enter the two numbers :2 4

  • 7/31/2019 Data and Address

    68/78

    1. Addition

    2. Subtraction

    3. Multiplication

    4. Division

    5. Modulo Division

    Enter the option : 1

    2 + 4 = 6

    [1011@localhost ]$

  • 7/31/2019 Data and Address

    69/78

  • 7/31/2019 Data and Address

    70/78

  • 7/31/2019 Data and Address

    71/78

    # String matching

    echo "Two/Four wheeler rental"

    echo -n "Enter the required vehicle : "

    read vehicle

    case $vehicle in

    "car") echo "For $vehicle Rs.20 per km";;

    "van") echo "For $vehicle Rs.10 per km";;

    "jeep") echo "For $vehicle Rs.5 per km";;

    "bicycle") echo "For $vehicle 20 paisa per km";;

    *) echo "Sorry, I cannot get a $vehicle for you";;

    esac

  • 7/31/2019 Data and Address

    72/78

  • 7/31/2019 Data and Address

    73/78

    "multiway3.sh" 11L, 351C written

    [1011@localhost ]$ sh multiway3.sh

    Two/Four wheeler rental

    Enter the required vehicle : bicycle

    For bicycle 20 paisa per km

    [1011@localhost ]$

    #Vote--range matching

    echo -n "Enter your age : "

    read age

    case $age in

    [0-9] | 1[0-7] )echo "You are not eligible to vote";;

    *)echo "Eligible to vote"

    esac

  • 7/31/2019 Data and Address

    74/78

  • 7/31/2019 Data and Address

    75/78

    "multiway4.sh" 7L, 157C written

    [1011@localhost ]$ sh multiway4.sh

    Enter your age : 12

    You are not eligible to vote

    [1011@localhost ]$

    program (degconv.sh)

    # degree conversion

    echo -n "Enter Fahrenheit : "

    read f

    c= `expr \ ( $f - 32 \ ) \* 5 / 9 `

    echo "Centigrade is : $c"

  • 7/31/2019 Data and Address

    76/78

    # circle metrics using readonly variable

    pi=`expr "scale=2; 22 / 7" | bc`

    readonly pi # pi value cannot be altered

    echo -n "Enter value for radius :"

    read radius

    area=`expr "scale=2; $pi * $radius * $radius" | bc`

    circum=`expr "scale=2; 2 * $pi * $radius" | bc`

    echo "Area : $area"

    echo "Circumference : $circum"

  • 7/31/2019 Data and Address

    77/78

  • 7/31/2019 Data and Address

    78/78

    "shell3.sh" [New] 9L, 320C written

    [1011@localhost ]$ sh shell3.sh

    Enter value for radius :12

    Area : 452.16

    Circumference : 75.36

    [1011@localhost ]$

    # Swapping values

    echo -n "Enter value for A : "

    read a

    echo -n "Enter value for B : "

    read b

    t=$a

    a=$b

    b=$t

    echo "Values after swapping"

    echo "A value is $a"

    echo "B value is $b"