TA ZC142-L7

  • Upload
    rajpd28

  • View
    227

  • Download
    0

Embed Size (px)

Citation preview

  • 7/28/2019 TA ZC142-L7

    1/45

    TA ZC142 Computer ProgrammingLecture 7Date: 30/01/2013

  • 7/28/2019 TA ZC142-L7

    2/45

    Last Lecture

    Character Arrays (Strings)

  • 7/28/2019 TA ZC142-L7

    3/45

    Array of strings

    Programming Exercise

    Efficiency and Complexity

    Resources and Measurements

    Complexity

    Time Complexity

    Space Complexity

    Todays Agenda

  • 7/28/2019 TA ZC142-L7

    4/45

    Arrays of Strings

    Declaration:

    char name[5][30];

    Five strings each contains maximum thirty characters.

    Initialization: char[5][10]={One,Two,Three,Four,Five};

    Other valid declarations: char[][]={One,Two,Three,Four,Five};

    char[5][]={One,Two,Three,Four,Five};

  • 7/28/2019 TA ZC142-L7

    5/45

    String Arrays: Reading and Displaying

    void main()

    { char name[5][30];

    printf(\n Enter five strings);

    /* Reading strings */

    for(i=0;i

  • 7/28/2019 TA ZC142-L7

    6/45

    Programming on Array of

    String

  • 7/28/2019 TA ZC142-L7

    7/45

    Write a C program to read the full name ofthe customers and their telephone numbers.

    Full name includes Surname followed by firstname and then the middle name.

    Sort the customer names in alphabetical

    order with surname first followed by comma(,) and initials of the first and middle names.Display this sorted customer list along with

    their telephone no. E.g Ghokle Suraj Ram should be written as

    Ghokle,S.R

  • 7/28/2019 TA ZC142-L7

    8/45

    OUTPUT

    asma@atlas:~/cp1$ ./a.out

    Enter names and telephone numberssharma anil sudhir 1234

    verma mona abhay 9876

    garg sona abhi 7654

    modak tinu pintu 9870shree shits sumit 8765

    Customer list in alphabetical order

    garg,s.a 7654modak,t.p 9870

    sharma,a.s 1234

    shree,s.s 8765

    verma,m.a 9876

  • 7/28/2019 TA ZC142-L7

    9/45

    Declarations

    charfname[20][10],mname[20][10],sname[20][10];

    charname[20][15],telephone[20][10],temp[20];

  • 7/28/2019 TA ZC142-L7

    10/45

    for(i=0;i

  • 7/28/2019 TA ZC142-L7

    11/45

    Code for Sorting list in alphabetical order along

    with telephone numbers

    for(i=1;i

  • 7/28/2019 TA ZC142-L7

    12/45

    Print the list

    for(i=0;i

  • 7/28/2019 TA ZC142-L7

    13/45

    Exercise : Array of Strings

    Problem Statement: Write a C program thatwill read and store the details of a list ofstudents in the format

    ID NAME MARKS

    And produce the following output

    1. Alphabetical list of Names, IDs and Marks.

    2. List sorted on IDs3. List sorted on Marks

  • 7/28/2019 TA ZC142-L7

    14/45

    Exercise

    1. Write a C program which will read aline of text and rewrite it in the

    alphabetical order.

    2. Write a C program to replace a

    particular word by another word in agiven string. Both the words areprovided by the user at run time.

  • 7/28/2019 TA ZC142-L7

    15/45

    Efficiency of algorithm needs algorithm

    analysis.

    Analyzing algorithms means to predict theresources that the algorithm requires.

    Efficiency

  • 7/28/2019 TA ZC142-L7

    16/45

    Resources

    CPU Time

    Storage

    Complexity

    Time Complexity

    Space Complexity

    Complexity of Algorithm

  • 7/28/2019 TA ZC142-L7

    17/45

    Running Time of Algorithm is affected by

    Hardware

    CPU, Clock Rate, memory, disk etc

    Software

    Operating system, programming language,

    compilers etc

    Running time of an algorithm is always

    proportional to the input size.

    Time Complexity

  • 7/28/2019 TA ZC142-L7

    18/45

    Efficiency of algorithms must be

    compared on their running time in the

    same h/w and s/w environment. So implementation of the algorithm is

    needed to study time complexity.

    But it is always not possible.

    Time Complexity

  • 7/28/2019 TA ZC142-L7

    19/45

    Based on algorithm (high level description

    of problem)

    It takes into account all possible inputs

    It also allows us to evaluate relative

    efficiency of two algorithms

    Analytical Framework

  • 7/28/2019 TA ZC142-L7

    20/45

    Primitive Operations

    Assigning a value to a variable

    Calling a function

    Performing an arithmetic operation

    Comparing two numbers

    Indexing into an array

    Returning value from function

    Computational Model

  • 7/28/2019 TA ZC142-L7

    21/45

    Each Primitive operation is an instruction

    One instruction takes one unit execution time.

    This approach assumes that all primitive

    operations take fairly similar execution time.

    Primitive operations does not depend on input

    size.

    Run time of an algorithm is estimated based on

    number of primitive operations.

    Computational Model

  • 7/28/2019 TA ZC142-L7

    22/45

    Example 1 (Y and Z are input)

    X = 3 * Y + Z;

    X = 2 + X;// 2 units of time and 1 unit of storage

    Complexity Example [1]

  • 7/28/2019 TA ZC142-L7

    23/45

    Example 2 (a and N are input)

    j = 0;

    while (j < N) do

    a[j] = a[j] * a[j];

    b[j] = a[j] + j;

    j = j + 1;endwhile;

    // 4N + 1 units of time and 2N+1 units of

    storage

    Complexity Example [2]

  • 7/28/2019 TA ZC142-L7

    24/45

    Example 1 (Y and Z are input)

    X = 3 * Y + Z;

    X = 2 + X;

    // Constant Unit of time and Constant Unit of

    storage

    Order of complexity [1]

  • 7/28/2019 TA ZC142-L7

    25/45

    Example 2 (a and N are input)

    j = 0;

    while (j < N) do

    a[j] = a[j] * a[j];

    b[j] = a[j] + j;

    j = j + 1;endwhile;

    // time units prop. to N and storage prop. to N

    Order of complexity [2]

  • 7/28/2019 TA ZC142-L7

    26/45

    Example 2 (a and N are input)j = 0;

    while (j < N) do

    k = 0;while (k < N) do

    a[k] = a[j] + a[k];

    k = k + 1;

    endwhile;

    b[j] = a[j] + j;j = j + 1;

    endwhile;

    // time prop. to N2 and storage prop. to N

    Order of complexity [3]

  • 7/28/2019 TA ZC142-L7

    27/45

    What if the algorithm is more

    complicated?

    How this analytical approach will work forlarge input size?

    Each statement may have small number of

    primitive operations.

    So there are limitations of analytical

    approach.

  • 7/28/2019 TA ZC142-L7

    28/45

    Allows to characterize main factors

    affecting an algos running time without

    going in much details of the algorithm. Run time is proportional to the input size

    of the algorithm.

    Asymptotic Notation

  • 7/28/2019 TA ZC142-L7

    29/45

    Order Notation

    log2N N N2 N3 2N

    1 2 4 8 4

    3.3 10 102 103 >103

    6.6 100 104 106 >1025

    9.9 1000 106 109 >10250

    13.2 10000 108 1012 >102500

  • 7/28/2019 TA ZC142-L7

    30/45

    Purpose

    Capture proportionality

    Machine independent measurement

    Asymptotic growth (I.e. large values of input

    size N)

    Order Notation

  • 7/28/2019 TA ZC142-L7

    31/45

    f(n)function which characterizes the

    running time of algorithm.

    Notation is f(n) 0, n >= n0 where n0 >= 1

    f(n) is order of g(n)

    f(n) is O(g(n))

    Asymptotic Notation

  • 7/28/2019 TA ZC142-L7

    32/45

    Motivation for Order

    Notationlog2N/10 N/10

    N2/10 N3/102N/10

    0.1 0.2 0.4 0.8 0.4

    0.33 1 10 102 >102

    0.66 10 103 105 >1024

    0.99 100 105

    108

    >10249

    1.32 1000 107 1011 >102499

    Speed factor of 10

  • 7/28/2019 TA ZC142-L7

    33/45

    Motivation for Order Notation

    log2N/104 N/104

    N2/104 N3/104 2N/104

    0.0001 0.0002 0.0004 0.0008 0.0004

    0.00033 0.0001 0.01 0.1 >0.1

    0.00066 0.001 1 102 >1021

    0.00099 0.01 102 105 >10246

    0.00132 0.1 104 108 >102496

    Speed factor of 104

  • 7/28/2019 TA ZC142-L7

    34/45

    Examples

    (17*N + 5) is O(N)

    (5*N3

    + 10*N2

    + 3) is O(N3

    ) (C1*Nk+ C2*Nk-1 + + Ck*N + C)

    is O(Nk)

    (2N + 4*N3 + 16) is O(2N)

    (5*N*log(N) + 3*N) is O(N*log(N))

    1789 is O(1)

    Order Notation

  • 7/28/2019 TA ZC142-L7

    35/45

    function search(X, A, N)

    j = 0;

    while (j < N)

    if (A[j] == X) return j;

    j++;

    endwhile;

    return Not_found;

    Linear Search - Complexity

  • 7/28/2019 TA ZC142-L7

    36/45

    low = 1; high = N;

    while (low

  • 7/28/2019 TA ZC142-L7

    37/45

    Let key=7; BinarySearch(A, 0,16, 7)

    Find the index of the middle element:

    A[mid]=A[(0+16)/2]=A[8]=45 A[mid]==7?

    Binary Search

  • 7/28/2019 TA ZC142-L7

    38/45

    7 < 45, search left sublist:A[0..7]

    Begin BinarySearch(A, 0, 7, 7)

    Find the index of the middle element:A[mid]=A[(0+7)/2]=A[3]=8

    A[mid]==7?

  • 7/28/2019 TA ZC142-L7

    39/45

    7 < A[mid]=8, perform

    BinarySearch(A, 0, 2, 7)

    mid=1, so we compare 7 with A[1]= 6.

  • 7/28/2019 TA ZC142-L7

    40/45

    7 >A[mid]=6, look at sublist A[2..2]

    The value of element 2 matches the search

    key, so our search is successful and we

    return the index 2.

  • 7/28/2019 TA ZC142-L7

    41/45

    Sorting Algorithm

    int main()

  • 7/28/2019 TA ZC142-L7

    42/45

    int main(){

    int a[10],t,i,j,n=5;for(i=0;i

  • 7/28/2019 TA ZC142-L7

    43/45

    int main(){

    int a[10],n=5,i,j,x;

    for(i=0;i=i+1;j--)a[j] = a[j-1];

    a[j] = x; n++;for(i=0;i

  • 7/28/2019 TA ZC142-L7

    44/45

    int main(){

    int a[10],n=5,i,j,x;

    for(i=0;i

  • 7/28/2019 TA ZC142-L7

    45/45

    Any Doubts ?