1 / 23

Today’s Agenda

Today’s Agenda. Data Abstraction Data Driven Programming. Data Driven Programming. Consider the scenario: Drama club Professor-in-charge wants an electronic list of members. Data Driven Programming. Requirements What are the requirements? Adding a new member to the list

Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda Data Abstraction Data Driven Programming

  2. Data Driven Programming Consider the scenario: Drama club Professor-in-charge wants an electronic list of members.

  3. Data Driven Programming Requirements What are the requirements? Adding a new member to the list Removing a member from the list Finding out whether someone is a member Is that all? Program should be “fast”!

  4. List Data - Operations Consider Search Choices: Ordered List, Unordered List Internal (Computer) Representation – Arrays Ordered List: O(log2N) Unordered List: O(N)

  5. List Data - Operations Now, consider addition: If ordered list is our choice – Order needs to be maintained on addition Position to add must be found (I.e. searched) Cost: At least O(logN) E.g. List (Ids of Member): 1 4 7 12 15 27 New Member: 13 New List: 1 4 7 12 13 15 27 (Last two elements moved!)

  6. List Data - Operations Addition in Ordered List O(logN) to find the position Shifting elements to the right: Worst case: All elements to be shifted: O(N) Average case: (1 + 2 + … + N) / N is O(N) Total complexity: (Worst and average case): N + logN is O(N)

  7. List Data - Operations Addition – Unordered List Idea: Add it at the end! Time Complexity: O(1) Ordered List or Unordered List? Question to Prof-in-Charge: How often do you add members? How often do you search for members?

  8. List Data - Operations Does deletion affect the choice? Deletion is (partly) similar to addition: Locate (I.e. search), Delete, Move (Left) the rest But these have to be done irrespective of whether the list is ordered or unordered.

  9. List Data - Operations Design for change: What if the mix of operations change tomorrow? Choice of list may change. Ensure separation between Interface and Implementation This ensures Prof-in-charge need not know all your efficiency computations!

  10. Linear Collection /* file: dramaClubList.h */ #define MAX 1000 typedef unsigned int Member; typedef unsinged int ListSize; typedef enum { TRUE = 1, FALSE=0 } Boolean; // List is an array of at most MAX elements of type Member typedef Member List[MAX];

  11. Tuple types-Drama club .. /* file: dramaClubMember.h */ typedef unsigned int ID; typedef enum { A1=1, A2, B1, B2, C1, C2, D1, D2 } Group; #define NAME_LEN 50 typedef struct { ID i; char n[NAME_LEN]; Group g; } Member; typedef Member List[MAX];

  12. /* file: dramaClubOps.h */ #include “dramaClubList.h” OR “dramaclubMember.h” extern ListSize add(Member m, List ms, ListSize n); extern ListSize delete(Member m, List ms, ListSize n); extern Boolean isMember(Member m, List ms, ListSize n); // No change needed in the interface so far!

  13. dramaClubOps.c - Implementation /* file: dramaClubOps.c */ ListSize add(Member m, List ms, ListSize n) { } int pos, newsize = n; if (n+1 > MAX) return 0; // This should lead to a test case. for (pos=0; pos < n; pos++) {// pos iterations if (ms[pos] > m) break; if (ms[pos] == m) return newsize; // m already present } for ( ; n > pos; n--) ms[n] = ms[n-1]; // n – pos –1 iterations ms[n] = m; newsize++; return newsize;

  14. #include”search.h” extern Boolean isMember(Member m, List ms, ListSize n) { if (search(m,ms,n) == TRUE) return TRUE; else return FALSE; } //search.h Boolean search(Member m, List ms, ListSize n);

  15. Programming Exercise on data driven programming

  16. Consider a police department containing N police officers. Each police officer contains the following information in his record. 1. An unsigned int id representing the unique identification number of the police. 2. A character array of maximum 30 characters for the name. 3. An unsigned int sc for the number of cases solved 4. An unsigned int ac for the number of cases attended 5. An unsigned int age for storing the age of the police 6. An unsigned int jyr to store the joining year. • A police department is a list containing maximum of MAX_POL number of police officers where MAX_POL is 1000.

  17. readPoliceDept() • rank() • countPoliceOnRank() • countPoliceService() • sortPoliceDept() • comparePolice() • printPoliceDept()

  18. Operations (1) rank() • The rank of police officer is computed as follows. • rank = (100 * solvedCases / attendedCases) • if attendedCases = 0, rank = 0;

  19. comparePolice() • compares police p1 and p2 based on rank, if rank is same then compare based on id .

  20. PoliceDef.h #define NAME_LEN 50 // Maximum length of Name #define MAX_POL 1024 // Max number of Police in one Dept typedef unsigned int ID; // Unique Police identification number typedef char NAME[NAME_LEN]; // Name of Police typedef unsigned int solvedCases; // Number of cases solved typedef unsigned int attendedCases; // Total no. of cases attended typedef unsigned int AGE; // Police officer's Age typedef unsigned int joiningYear; // Police officer's joining year typedef unsigned int noOfPolice; // number of Police officers in Dept

  21. PoliceDef.h typedef enum { GREATER = 1, EQUAL = 0, LESSER = -1 } ORDER; typedef struct { ID id; NAME name; solvedCases sc; attendedCases ac; AGE age; joiningYear jyr; } police; // The police structure typedef police policeDept[MAX_POL]; // the policeDept

  22. PoliceOps.h • extern void readPoliceDept(policeDeptpdt, noOfPolice N); • extern float rank(police p); • extern int countPoliceOnRank (policeDept pdt, noOfPolice N); • Count the number of police who has rank more 75. • extern int countPoliceService(policeDept pdt, noOfPolice N); • Count the number of police who has completed 25 years of service. • extern order comparePolice(policep1,police p2); • extern void sortPoliceDept(policeDept pdt, noOfPolice N); • Sort police based on their ranks. Use insertion sort. • extern Boolean searchPolice(policeDept pdt,noOfPolice N,police p); • extern void printPoliceDept(policeDept pdt, noOfPolice N);

  23. comparePolice() ORDER comparePolice (police p1, police p2) { if(rank(p1)<rank(p2)) return LESSER; else if(rank(p1) == rank(p2)) { if(p1.id < p2.id) return LESSER; else if(p1.id == p2.id) return EQUAL; else return GREATER; } else return GREATER; }

More Related