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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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" , &amp;reg_number, &amp;s1, &amp;s2, &amp;s3)) {
        if (s1 < s2 &amp;&amp; s1 < s3){
            score = (s2+s3)/2;
        } else if (s2 < s1 &amp;&amp; s2 < s3) {
            score = (s1+s3)/2;
        } else if (s3 < s1 &amp;&amp; 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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Repeats (Main)

A

Just add display_repeats(my_array, size) before free(my_array) line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

flexarray housekeeping

A
#include 
#include 
#include "flexarray.h"
struct flexarrayrec {
    int capacity;
    int itemcount;
    int *items;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

*erealloc

A
void *erealloc(void *p, size_t s){
    void *result = realloc(p, s);
    if (NULL == result &amp;&amp; s != 0){
        fprintf(stderr, "memory allocation failed!\n");
        exit(EXIT_SUCCESS);
    }
    return result;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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]);
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

flexarray_free

A

void flexarray_free(flexarray f){
free(f->items);
free(f);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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 &amp;&amp; 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;
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

sortnums

A
int main(void){
    int item;
    flexarray my_flexarray = flexarray_new();
    while (1==scanf("%d", &amp;item)){
        flexarray_append(my_flexarray, item);
    }
    flexarray_sort(my_flexarray);
    flexarray_print(my_flexarray);
    flexarray_free(my_flexarray);
return EXIT_SUCCESS; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

*emalloc

A
void *emalloc(size_t s){
    void *result = malloc(s);
    if (NULL == result){
        fprintf(stderr, "Memory allocation failed!\n");
        exit(EXIT_FAILURE);
    }
    return result;
}