Practical 2 Flashcards
1
Q
Prime Numbers (is_prime)
REMEMBER: #include
(BRIEF HAS PSUEDOCODE)
A
int is_prime(int candidate) { int i = 2;
for (i = 2; i < candidate; i++) { if (candidate%i == 0) { return 0; } } return 1; }
2
Q
Prime Numbers (main)
BRIEF HAS PSEUDOCODE
A
int main(void){ int candidate = 2; int num_printed = 0;
while (num_printed < 200){ if (is_prime(candidate)){ if (num_printed % 10 == 0){ printf("\n"); } printf("%5d\t" , candidate); num_printed++; } candidate++; } return EXIT_SUCCESS; }
3
Q
Largest Skating Score
(BRIEF HAS PSEUDO BUT NEED TO SCAN IN 4 things)
so while (4 = scanf…)
at end:
IF (score > highest){
…
}
A
int main(void) {
int reg_number; double s1 = 0.0; double s2 = 0.0; double s3 = 0.0; double score = 0.0; double highest = 0.0; int winner = 0;
while (4 == scanf("%d%lg%lg%lg\n" , &reg_number, &s1, &s2, &s3)) { if (s1 < s2 && s1 < s3){ score = (s2+s3)/2; } else if (s2 < s1 && s2 < s3) { score = (s1+s3)/2; } else if (s3 < s1 && s3 < s2) { score = (s1+s2)/2; } if (score > highest) { highest = score; winner = reg_number; } } printf("Winner: %d\nScore: %f\n", winner, highest); return EXIT_SUCCESS; }
4
Q
Repeats (display_repeats)
A
int display_repeats(int *arr, int size){ int *freq; int i;
freq = malloc(size * sizeof freq[0]);
/* three separate loops! */
/* First initialise freq[I] to 0 */ for (i=0; i < size; i++){ freq[i] = 0; }
/* incr whole of freq[arr[I]]] */ for (i=0; i < size; i++){ freq[arr[i]]++; }
/* if freq[I] positive, print */ for (i=0; i < size; i++){ if (freq[i] > 0){ printf("%d appears %d times\n", i, freq[i]); } }
/* free the freq */ free(freq); return EXIT_SUCCESS; }
5
Q
Repeats (Main)
A
Just add display_repeats(my_array, size) before free(my_array) line
6
Q
flexarray housekeeping
A
#include #include #include "flexarray.h"
struct flexarrayrec { int capacity; int itemcount; int *items; };
7
Q
*erealloc
A
void *erealloc(void *p, size_t s){ void *result = realloc(p, s); if (NULL == result && s != 0){ fprintf(stderr, "memory allocation failed!\n"); exit(EXIT_SUCCESS); } return result; }
8
Q
flexarray_new
A
flexarray flexarray_new(){ flexarray result = emalloc(sizeof *result); result->capacity = 2; result->itemcount = 0; result->items = emalloc(result->capacity * sizeof result->items[0]); return result; }
9
Q
flexarray_append
A
void flexarray_append(flexarray f, int num){ if (f->itemcount == f->capacity){ f->capacity += f->capacity; f->items = erealloc(f->items, f->capacity * sizeof f->items[0]); } f->items[f->itemcount++] = num; }
10
Q
flexarray_print
A
void flexarray_print(flexarray f){ int i; for (i=0;i< f->itemcount; i++){ printf("%d\n", f->items[i]); } }
11
Q
flexarray_sort
A
void flexarray_sort(flexarray f){ (insertion or selection) } See insertion or selection (only give this 5/5 when you're sure you know each one well and know the DIFFERENCE.
12
Q
flexarray_free
A
void flexarray_free(flexarray f){
free(f->items);
free(f);
}
13
Q
Insertion Sort
A
void flexarray_sort(flexarray f){ int i, key, j;
/* Everything contained in a for loop FROM 1 */ for (i = 1; i < f->itemcount; i++){ /* set key to item at index */ key = f->items[i]; /* set j to i minus 1 */ j = i-1;
/* while j not negative and item at j bigger than key */ while (j>= 0 && f->items[j] > key){ /* item at j + 1 is now item at j */ f->items[j+1] = f->items[j]; /* decrement j */ j--; } /* item at j+1 is now key */ f->items[j+1] = key; } }
14
Q
Selection Sort
A
void flexarray_sort(flexarray f){ int i, j, min, temp;
for (i=0; i < f->itemcount -1; i++){ min = i; for (j= i+1; j < f->itemcount; j++){ if (f->items[j] < f->items[min]){ min = j; } } temp = f->items[i]; f->items[i] = f->items[min]; f->items[min] = temp; } }
15
Q
sortnums
A
int main(void){ int item; flexarray my_flexarray = flexarray_new();
while (1==scanf("%d", &item)){ flexarray_append(my_flexarray, item); } flexarray_sort(my_flexarray); flexarray_print(my_flexarray); flexarray_free(my_flexarray);
return EXIT_SUCCESS; }