Vous êtes sur la page 1sur 9

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #25, September 29, 2011

Please switch off your mobile phones.

Announcements
Last date for course drop is 20th October. p
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) If anyone from Friday Lab sections has a class/lab/tutorial on f F id L b ti h l /l b/t t i l Tuesday afternoon, please let us know by 10th morning.

Tuesday Lab Section please note


10th lab on Saturday, 22nd October, instead of 25th October.
Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 1

Best Programmers for Lab 6


Section E1 E3 E5 E7 E9 E11 E13 E15 Name Utkarsh Sanjay Patange Unnat Jain Pratik Pradyot Rath Mohit Sharma Dhrupal R Shah Swapnil Shwetank Jha Sarthak Sagar Aditya Aggarwal Section E2 E4 E6 E8 E10 E12 E14 Jayant Arihant Kumar Jain Saurav Narang none Gagan Agrawal Chaitanya Ahuja Abhishek Verma Name

Lec-25

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Structures
members functions returning structures passing structures to functions pointers to structures nested structures array of structures pointer in a structure

Lec-25

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Structure operations 1


#include <stdio.h> #include <math.h> struct point { double x; 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; return p; } 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 void modify_right ( t t point *p, d bl c, d bl d) id dif i ht (struct i t * double double { p->x = c; p->y = d; } // modifications are permanent int main ( ) { struct point p, q, s; // declaring structure variables struct point t = {9.0, -5.0}; // initialization during declaration double d; d bl 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

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}; modify_wrong (p, 12.0, 8.0); printf (%lf %lf\n, p.x, p.y); // error

// 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) }
Lec-25

// error

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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

Recap: Array of structures


An array of structures can be declared as: 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 to structure

Lec-25

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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
strcpy (s.name, Sudhir); Sudhir ); // wrong

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

Modular programming
Divide your problem into smaller manageable parts 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; int year; }; struct marks { int lab; int quiz; int midsem; int labexam; int final; int total; };
Lec-25

struct student { int roll; char *name; int hostel; int room; long mobile; struct date bday; struct marks esc101; struct marks phy; }

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

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.bday.day > s2.bday.day) return 0; ( y y y y) ; 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 { struct student temp; temp = *s1; *s1 = *s2; *s2 = temp; }

Lec-25

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

13

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 if stucompareless (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]; // n records to be searched // Linear search

int findstudent (struct student s1, int n) { int i; for (i = 0; i < n; i++) { if (s1.roll == slist[i].roll) return i } return (-1); }

Lec-25

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

15

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, s.bday.month, s.bday.year, s.mobile); s bday month s bday year s mobile); 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); printf (TOTAL Marks: \t%d\n, s esc101 total); ( TOTAL \t%d\n s.esc101.total); }

Lec-25

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Wishing all of you a very happy Dussehra Enjoy you vacat o s joy your vacations
Lec-25 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 17

Vous aimerez peut-être aussi