#include #include #include #define NBTECH 6 #define NDUAL 3 #define NMS 2 #define NPHD 3 #define NBLOG 1 #define SKIP_PROB 0.4 typedef struct { char R[10]; /* Roll number */ int A; /* Arts marks */ int S; /* Science marks */ } studrec; /* Add nb BTech, nd Dual, nm MS, np PhD and nl backlog students. Random addition used in place of reading from the user. */ studrec *readData ( int nb, int nd, int nm, int np, int nl ) { studrec *L; int i, r, rold; L = (studrec *)malloc((nl + nb + nd + nm + np) * sizeof(studrec)); if (L) { rold = 0; for (i=0; i L[j].A) return 1; if (L[i].A < L[j].A) return -1; return 0; } /* Compare two science marks */ int cmpS ( studrec *L, int i, int j ) { if (L[i].S > L[j].S) return 1; if (L[i].S < L[j].S) return -1; return 0; } /* Compare an arts mark with an immediate operand (x) */ int cmpImmA ( studrec *L, int i, int x ) { if (L[i].A > x) return 1; if (L[i].A < x) return -1; return 0; } /* Compare a science mark with an immediate operand (x) */ int cmpImmS ( studrec *L, int i, int x ) { if (L[i].S > x) return 1; if (L[i].S < x) return -1; return 0; } /* Bubble sort parameterized by the comparison routine cmp(). C reference: See function pointers. */ int *sort ( studrec *L, int n, int (*cmp)(studrec *, int, int) ) { int *J, i, j, k; J = (int *)malloc(n * sizeof(int)); if (J) { for (i=0; i=0; --j) { for (i=0; i<=j; ++i) { if (cmp(L,J[i],J[i+1]) < 0) { k = J[i]; J[i] = J[i+1]; J[i+1] = k; } } } } return J; } /* Binary search, indexed by J[], parameterized by cmp() */ int binsearch ( studrec *L, int *J, int n, int x, int (*cmp)(studrec *,int,int) ) { int l, u, m; l = 0; u = n-1; while (l != u) { m = (l + u) / 2; if (cmp(L,J[m],x) > 0) l = m+1; else u = m; } return l; } /* Part 1: Search for a and b (the boundaries), determine the start and end indices, and print all records between them (both inclusive). Indexed by J[], parameterized by cmp(). */ void intervalSearch ( studrec *L, int *J, int n, int a, int b, int (*cmp)(studrec *,int,int) ) { int start, end, i; printf("--- List of students:\n"); if (cmp(L,J[0],a) < 0) return; if (cmp(L,J[n-1],b) > 0) return; start = binsearch(L,J,n,b,cmp); while ((start > 0) && (cmp(L,J[start],b) == 0)) --start; end = binsearch(L,J,n,a,cmp); while ((end < n-1) && (cmp(L,J[end],a) == 0)) ++end; if (cmp(L,J[start],b) > 0) ++start; if (cmp(L,J[end],a) < 0) --end; for (i=start; i<=end; ++i) { printf(" %s,%3d,%3d\n", L[J[i]].R, L[J[i]].A, L[J[i]].S); } } /* Solves Part 2. First generate the rank arrays RA[] and RS[] for the two courses. Then make a run over all the students. */ void rankCompare ( studrec *L, int *JA, int *JS, int n ) { int i, rank, *RA, *RS, nBA, nBS, nSC; RA = (int *)malloc(n * sizeof(int)); RS = (int *)malloc(n * sizeof(int)); if (RA && RS) { rank = 0; for (i=0; i RS[i]) { printf("Decision: BS\n"); ++nBS; } else { printf("Decision: SC\n"); ++nSC; } } } printf("\n"); printf(" Number of BA = %d\n", nBA); printf(" Number of BS = %d\n", nBS); printf(" Number of SC = %d\n", nSC); free(RA); free(RS); } int main ( int argc, char *argv[] ) { int nb, nd, nm, np, nl, n; studrec *L; int *JA, *JS; int a, b; srand((unsigned int)time(NULL)); if (argc >= 6) { nb = atoi(argv[1]); nd = atoi(argv[2]); nm = atoi(argv[3]); np = atoi(argv[4]); nl = atoi(argv[5]); } else { nb = NBTECH; nd = NDUAL; nm = NMS; np = NPHD; nl = NBLOG; } n = nb + nd + nm + np + nl; L = readData(nb,nd,nm,np,nl); if (L == NULL) exit(1); printf("\n+++ Initial list (sorted by roll numbers)\n"); printList1(L,n); JA = sort(L,n,cmpA); if (JA == NULL) exit(2); printf("\n+++ List sorted by arts marks\n"); printList2(L,JA,n); JS = sort(L,n,cmpS); if (JS == NULL) exit(2); printf("\n+++ List sorted by science marks\n"); printList2(L,JS,n); printf("\n+++ Interval search\n"); a = 10 + rand() % 60; b = a + 10 + rand() % 10; printf("\n+++ Arts: a = %d, b = %d\n", a, b); intervalSearch(L,JA,n,a,b,cmpImmA); a = 10 + rand() % 60; b = a + 1; printf("\n+++ Arts: a = %d, b = %d\n", a, b); intervalSearch(L,JA,n,a,b,cmpImmA); a = 20 + rand() % 60; b = a + 5; printf("\n+++ Science: a = %d, b = %d\n", a, b); intervalSearch(L,JS,n,a,b,cmpImmS); a = 20 + rand() % 60; b = a + 10 + rand() % 10; printf("\n+++ Science: a = %d, b = %d\n", a, b); intervalSearch(L,JS,n,a,b,cmpImmS); printf("\n+++ Rank comparisons\n"); rankCompare(L,JA,JS,n); free(L); free(JA); free(JS); exit(0); }