esc101-lec25

Upload: mukesh-kumar-dewra

Post on 05-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 esc101-lec25

    1/9

    1

    ESc101: Fundamentals of Com utin

    2011-12-Monsoon Semester

    Lecture #25, September 29, 2011

    Please switch off your mobile phones.

    Announcements

    Last date for course dro is 20th October.

    I will sign drop requests till 17th October.

    Friday Lab Section please note Lab on 14th suspended due to Gymkhana Holiday for Antaragni

    It will be conducted on 18th (Tuesday)

    Lec-25

    anyone rom r ay a sec ons as a c ass a u or a on

    Tuesday afternoon, please let us know by 10th morning.

    Tuesday Lab Section please note

    10th lab on Saturday, 22nd October, instead of 25th October.

    1Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

  • 7/31/2019 esc101-lec25

    2/9

    2

    Best Programmers for Lab 6

    Section Name Section Name

    E1 Utkarsh Sanjay Patange E2 Jayant

    E3 Unnat Jain E4 Arihant Kumar Jain

    E5 Pratik Pradyot Rath E6 Saurav Narang

    E7 Mohit Sharma E8 none

    E9 Dhrupal R Shah E10 Gagan Agrawal

    E11 Swapnil Shwetank Jha E12 Chaitanya Ahuja

    E13 Sarthak Sagar E14 Abhishek Verma

    E15 Aditya Aggarwal

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    2

    Recap

    Structures

    members

    functions returning structures

    passing structures to functions pointers to structures

    nested structures

    Lec-25

    pointer in a structure

    3Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

  • 7/31/2019 esc101-lec25

    3/9

    3

    Recap: Structure operations 1

    #include

    #include

    struct point {

    double y;

    }; // defining a structure

    struct point new_point (double c, double d) // structure as return value

    { struct point p;

    p.x = c;

    p.y = d;

    }

    double distance (struct point a, struct point b) // structure as parameter

    { return (sqrt (pow (a.x b.x, 2) + pow (a.y b.y), 2));

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    4

    Recap: Structure operations 2

    void modify_wrong (struct point p, double c, double d)

    { p.x = c;

    p.y = d;

    } // modifications are temporary

    vo mo y_r ght (struct po nt p, ouble c, ouble

    { p->x = c;

    p->y = d;

    } // modifications are permanentint main ( )

    { struct point p, q, s; // declaring structure variables

    struct point t = {9.0, -5.0}; // initialization during declaration

    double d;

    struct point *ptr;

    printf(%lf %lf\n, t.x, t.y); // Accessing individual members

    q.x = 4.0; // modifying members in structure notation

    q.y = -3.0;

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    5

  • 7/31/2019 esc101-lec25

    4/9

    4

    Recap: Structure operations 3

    p = new_point (7.0, -1.0); // Setting structure through functions return value

    d = distance (p, q); // passing structures as parameters

    printf(Distance = %lf\n, d);

    // s = {6.0, 2.0}; // error

    modify_wrong (p, 12.0, 8.0);

    printf(%lf %lf\n, p.x, p.y); // No modification would have happened

    ptr = &p;

    modify_right (ptr, 12.0, 8.0); // Now p would have been modified

    printf(%lf %lf %lf %lf\n, p.x, p.y, ptr->x, ptr->y);

    // if (q == p) // error

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    6

    Recap: Nested structures

    A structure can have another structure as its member

    struct line {

    struct point p;

    struct point q;

    };

    struct line l;

    Value x of point p of variable l of type struct line can be

    accessed as:

    l.p.x

    The . operator has left-to-right associativity

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    7

  • 7/31/2019 esc101-lec25

    5/9

    5

    Recap: Array of structures

    struct point t [3];

    An individual structure is accessed as t[0], etc.

    A member of a structure is accessed as t[1].x, etc.

    All operations allowed on normal arrays are allowed on

    arrays of structures

    Just like any other array, it is equivalent to a pointer tostructure

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    8

    Recap: Pointer in a structure

    A structure can have a pointer as its member

    struct student {

    int roll;

    char *name;

    };

    struct student s;

    Declaring a variable of type struct student just declares the pointer

    name it does not allocate space for it

    strc (s.name Sudhir // wron

    Memory for name has to be allocated explicitly using malloc

    s.name = (char *) malloc (30 * sizeof(char));

    strcpy (s.name, Sudhir); // right

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    9

  • 7/31/2019 esc101-lec25

    6/9

    6

    Modular programming

    Write functions to solve each small part

    If your code is more than what you can see in one screen,

    then perhaps it is better to divide this into functions

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    10

    Example: Nested Structure

    struct date {

    int day;

    int month;

    struct student {

    int roll;

    char *name;

    int year;

    };

    struct marks {int lab;

    int quiz;

    int midsem;

    int hostel;

    int room;

    long mobile;struct date bday;

    struct marks esc101;

    struct marks phy;

    int labexam;

    int final;

    int total;

    };

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    11

  • 7/31/2019 esc101-lec25

    7/9

    7

    Comparing two structures

    int stucompareless (struct student s1, struct student s2)

    // sort on total marks, then age, and then roll number.

    {

    if(s1.esc101.total < s2.esc101.total) return 1;

    if(s1.esc101.total > s2.esc101.total) return 0;

    if(s1.bday.year < s2.bday.year) return 1;

    if(s1.bday.year > s2.bday.year) return 0;

    if(s1.bday.month < s2.bday.month) return 1;

    if(s1.bday.month > s2.bday.month) return 0;

    if(s1.bday.day < s2.bday.day) return 1;

    if(s1.bda .da > s2.bda .da return 0;

    if(s1.roll < s2.roll) return 1;

    return 0;

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    12

    Swapping two student records

    void swapstudents (struct student *s1, struct student *s2)

    // Notice the arguments are pointers for permanent effect

    {

    temp = *s1;

    *s1 = *s2;

    *s2 = temp;

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    13

  • 7/31/2019 esc101-lec25

    8/9

    8

    Sorting student records

    struct student slist[100];

    void sortstudents ( int n) // n records to be sorted

    { // Using Bubblesort will do sorting later

    int i, k, swap;

    for (k = n; k > 1; k--)

    { swap = 0;

    for (i = 0; i < k; i++)

    // In each iteration, shift the bigger value record to the right

    ifstucompareless (slist[i], slist[i+1])

    { swapstudents (&slist[i], &slist[i+1]);

    swap = 1;}

    if(!swap) break;

    }

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    14

    Searching student records

    struct student slist[100];

    int findstudent (struct student s1, int n) // n records to be searched

    { int i;

    for (i = 0; i < n; i++) // Linear search

    {

    if(s1.roll == slist[i].roll) return i

    }

    return (-1);

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    15

  • 7/31/2019 esc101-lec25

    9/9

    9

    Print student record

    void printstudent (struct student s)

    {

    printf(%d\t%s\t%2d %2d %4d\t%ld\n, s.roll, s.name, s.bday.day,

    . . , . . , .

    printf(Marks in ESc101\n);

    printf(Lab: \t\t%d\n, s.esc101.lab);

    printf(Quizzes: \t%d\n, s.esc101.quiz);

    printf(Mid-sem exam: \t%d\n, s.esc101.midsem);

    printf(Lab-exam: \t%d\n, s.esc101.labexam);

    printf(End-sem exam: \t%d\n, s.esc101.final);

    , . .

    }

    Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    16

    Wishing all of you a very

    happy Dussehra

    En o our vacations

    Lec-25 17Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon