Created
May 29, 2018 05:58
-
-
Save alexcu/dfe6fb5dfc00826d8e57e24894818fae to your computer and use it in GitHub Desktop.
COMP2005 Sample Exam Answers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* Q1 **********/ | |
int amicable_pair(int n1, int n2) { | |
int sum_factors_n1 = 0, sum_factors_n2 = 0; | |
int i; | |
for (i = 1; i < n1; i++) { | |
if (n1 % i == 0) { | |
sum_factors_n1 += i; | |
} | |
} | |
for (i = 1; i < n2; i++) { | |
if (n2 % i == 0) { | |
sum_factors_n2 += i; | |
} | |
} | |
printf("%d == %d\n", sum_factors_n1, sum_factors_n2); | |
return sum_factors_n1 == n2 && sum_factors_n2 == n1; | |
} | |
/********* Q2 **********/ | |
int powerto(int base, int exp) { | |
if (exp == 0) { | |
return 1; | |
} | |
return base * powerto(base, exp - 1); | |
} | |
int compute_S(int n) { | |
int result; | |
for (int i = 1; i <= n; i++) { | |
result += powerto(((i - 1) / i), i); | |
} | |
return result; | |
} | |
/********* Q3 **********/ | |
void insertionsort(int A[], int n) { | |
if (n <= 1) { | |
return; | |
} | |
insertionsort(A, n - 1); | |
for (int i = n - 1; i > 0; i--) { // -1 because -1 below! | |
if (A[i] < A[i - 1]) { | |
int temp = A[i]; | |
A[i] = A[i-1]; | |
A[i-1] = temp; | |
} | |
} | |
} | |
/********* Q4a **********/ | |
#define ROWS 100 | |
#define COLS 200 | |
void make_zero(double data[ROWS][COLS]) { | |
for (int i = 0; i < ROWS; i++) { | |
for (int j = 0; j < COLS; j++) { | |
data[i][j] = 0; | |
} | |
} | |
} | |
/********* Q4b **********/ | |
double absolute(double a) { | |
if (a < 0) { | |
return a * -1; | |
} | |
return a; | |
} | |
int approx_equal(double data1[ROWS][COLS], double data2[ROWS][COLS]) { | |
for (int i = 0; i < ROWS; i++) { | |
for (int j = 0; j < COLS; j++) { | |
if (absolute(data1[i][j] - data2[i][j]) >= 10E-6) { | |
return 0; | |
} | |
} | |
} | |
return 1; | |
} | |
/********* Q4c **********/ | |
void swap_cols(double data[ROWS][COLS], int index) { | |
for (int i = 0; i < COLS; i++) { | |
double temp = data[index - 1][i]; | |
data[index - 1][i] = data[index][i]; | |
data[index][i] = temp; | |
} | |
} | |
int compare_rows(double data[ROWS][COLS], int row1, int row2) { | |
for (int i = 0; i < COLS; i++) { | |
if (data[row1][i] > data[row2][i]) { | |
return 1; | |
} else if (data[row1][i] < data[row2][i]) { | |
return 0; | |
} | |
} | |
// equal rows, doesnt matter | |
return 0; | |
} | |
void sort_by_rows(double data[ROWS][COLS]) { | |
for (int i = 1; i < ROWS; i++) { | |
if (compare_rows(data, i - 1, i)) { | |
// Swap the columns... | |
swap_cols(data, i); | |
// Re-sort from start recursively... | |
sort_by_rows(data); | |
} | |
} | |
} | |
int q4_test() | |
{ | |
double data[ROWS][COLS]; | |
make_zero(data); | |
data[0][0] = 3; | |
data[0][1] = 4; | |
data[0][2] = 5; | |
data[1][0] = 5; | |
data[1][1] = 6; | |
data[1][2] = 7; | |
data[2][0] = 4; | |
data[2][1] = 5; | |
data[2][2] = 6; | |
data[3][0] = 4; | |
data[3][1] = 2; | |
data[3][2] = 4; | |
data[4][0] = 4; | |
data[4][1] = 2; | |
data[4][2] = 3; | |
for (int i = 0; i < ROWS; i++) { | |
int printed = 0; | |
for (int j = 0; j < COLS; j++) { | |
if (data[i][j] != 0) { | |
printed = 1; | |
printf("%0.0f ", data[i][j]); | |
} | |
} | |
if (printed) { | |
printf("\n"); | |
} | |
} | |
printf("===\n"); | |
sort_by_rows(data); | |
for (int i = 0; i < ROWS; i++) { | |
int printed = 0; | |
for (int j = 0; j < COLS; j++) { | |
if (data[i][j] != 0) { | |
printed = 1; | |
printf("%0.0f ", data[i][j]); | |
} | |
} | |
if (printed) { | |
printf("\n"); | |
} | |
} | |
return 0; | |
} | |
/********* Q5 **********/ | |
#define MAX_BULDING_NAME 40 | |
#define MAX_FLOOR_NAME 10 | |
#define MAX_FLOORS_PER_BUILDING 20 | |
#define MAX_BUILDINGS_PER_UNIVERSITY 250 | |
typedef struct floor_t { | |
char name[MAX_FLOOR_NAME]; | |
float area; | |
int has_male_toilet; | |
int has_female_toilet; | |
int is_air_conditioned; | |
} floor_t; | |
typedef struct building_t { | |
char name[MAX_BULDING_NAME]; | |
int location_number; | |
char location[2]; | |
int num_floors; | |
int num_lifts; | |
int year_constructed; | |
floor_t floors[MAX_FLOORS_PER_BUILDING]; | |
} building_t; | |
float fraction_of_ac(building_t university[MAX_BUILDINGS_PER_UNIVERSITY]) { | |
float ac_area = 0, total_area = 0; | |
for (int i = 0; i < MAX_BUILDINGS_PER_UNIVERSITY; i++) { | |
building_t building = university[i]; | |
for (int j = 0; building.num_floors; j++) { | |
total_area += building.floors[j].area; | |
if (building.floors[j].is_air_conditioned) { | |
ac_area += building.floors[j].area; | |
} | |
} | |
} | |
return ac_area / total_area; | |
} | |
void q5_main() { | |
building_t university[MAX_BUILDINGS_PER_UNIVERSITY]; | |
} | |
/********* Q6 **********/ | |
// a) array = multiple values of a single type stored under one | |
// identifer | |
// struct = a custom data type that stores members, but instances | |
// of it are in itself one variable | |
// b) int = whole number; float = decimal number | |
// c) 100000 = 2^5 = 32 (60 - 32 = 28) | |
// 010000 = 2^4 = 16 (32 + 16 = 48) | |
// 001000 = 2^3 = 8 (48 + 8 = 56) | |
// 000100 = 2^2 = 4 (56 + 4 = 60) | |
// 111100 | |
// But in 16 bit it would be: | |
// 0000 0000 0011 1100 | |
// d) What is 4? | |
// 0000 0000 0000 0100 | |
// Invert the bits | |
// 1111 1111 1111 1011 | |
// Add one (in binary) | |
// 1111 1111 1111 1100 | |
// e) The three components are: sign bit, mantissa, exponent | |
// 1 bit sign bit | |
// 8 bit mantissa | |
// 23 bit exponent | |
// f-j) Np idea. | |
/********* Q7 **********/ | |
void print_perms(int n) { | |
int sequence[n]; | |
// Create the sequence from 0...n | |
for (int i = 0; i < n; i++) { | |
sequence[i] = i + 1; | |
} | |
// Now iterate through n times going LTR.. will repeat | |
// with re-swap | |
for (int i = 0; i < n; i++) { | |
// Go through n times again to actually SWAP | |
for (int j = 1; j < n; j++) { | |
// Print the sequence out | |
for (int k = 0; k < n; k++) { | |
printf("%d ", sequence[k]); | |
} | |
printf("\n"); | |
// Then actually swap them around | |
int temp = sequence[j - 1]; | |
sequence[j - 1] = sequence[j]; | |
sequence[j] = temp; | |
} | |
} | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
print_perms(3); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swap mantissa and exponent in Q6e