90 likes | 224 Views
Today’s Agenda. Programming Exercise on data driven programming. 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.
E N D
Today’s Agenda Programming Exercise on data driven programming
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.
readPoliceDept() • rank() • countPoliceOnRank() • countPoliceOnAge() • comparePolice() • sortPoliceDept() • printPoliceDept()
Operations (1) rank() • The rank of police officer is computed as follows. • rank = (100 * solvedCases / attendedCases) • if attendedCases = 0, rank = 0;
comparePolice() • compares police p1 and p2 based on rank, if rank is same then compare based on id .
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
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
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 countPolice2(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. • Extern void printPoliceDept(policeDept pdt, noOfPolice N);
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; }