FE-May18-Sol.pdf-Page 1 of 4 Computer Sc...
FE-May18-Sol.pdf-Page 1 of 4 Computer Science
Showing 15-16 out of 16
FE-May18-Sol.pdf-Page 1 of 4 Comput...
FE-May18-Sol.pdf-Page 1 of 4 Computer Science
##### Page 15
Summer 2018
Algorithms and Analysis Tools Exam, Part B
Page
3
of
4
2)
(10 pts) DSN (Sorting)
Consider sorting students, where each student has a first name, last name and a unique ID number.
Assume all names contain uppercase letters only, so that strcmp does an alphabetic comparison for the
purposes of this problem. Complete the code below so that it sorts students by last name (A to Z),
breaking ties by first name (A to Z), and finally breaking ties between students with identical first and
last names by ID number (smallest to largest). For example, if we have the students (EMILIO
SANCHEZ 17), (ANDREA SANCHEZ 22), and (EMILIO SANCHEZ 10), the correct ordering would
be ANDREA SANCHEZ, followed by EMILIO SANCHEZ 10, with EMILIO SANCHEZ 17 last.
typedef struct student {
char first[20];
char last[20];
int ID;
} student;
void sort(student** list, int len) {
int i,j;
for (i=len-1; i>0; i--) {
for (j=0; j<i; j++) {
if (cmp(list[j], list[j+1]) > 0) {
student* tmp = list[j];
list[j] = list[j+1];
list[j+1] = tmp;
}
}
}
}
int cmp(student* a, student* b) {
if (strcmp(a->last, b->last) != 0)
return strcmp(a->last, b->last);
if (strcmp(a->first, b->first) != 0)
return strcmp(a->first, b->first);
return a->ID - b->ID;
}
Grading: 3 pts for breaking ties properly by last name, 3 pts for breaking ties properly by first
name, 4 pts for breaking ties by ID. If no string functions are used but the logic is correct via
regular relational operators, then take off 4 pts total for not properly calling the functions.

##### Page 16
Summer 2018
Algorithms and Analysis Tools Exam, Part B
Page
4
of
4
3) (
5 pts) ALG (Bitwise Operators)
What is the output of the following program?
#include <stdio.h>
int main() {
int n = 182, i = 0;
while (n > 0) {
if ((n & (1<<i)) > 0) {
printf("%d\n", (1<<i));
n ^=(1<<i);
}
i++;
}
return 0;
}
2
4
16
32
128
Grading: 1 pt per correct number listed. 1 pt off per extra number listed, cap at 0.

Browse thousands of Study Materials & Solutions from your Favorite Schools
University of Pune
University_of_Pune
School:
Computer_Science_I
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades