Practical 1 Flashcards

1
Q

Admin Stuff

A
#include (stdio.h)
#include (stdlib.h)
#include (string.h) --> all triangle braces!
#define STRING_LEN 80
#define ARRAY_LEN 100
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

print_array

A
void print_array(char *a, int n){
    if (n > 0){
        printf("%c", a[0]);
        print_array(a+1, n-1);
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Main

A
int main(){
    char word[STRING_LEN];
    char *wordlist[ARRAY_LEN];
    int num_words = 0;
    int i;
    double average = 0.0;

while (num_words < ARRAY_LEN && 1 == scanf(ā€œ%79sā€, word)){
wordlist[num_words] = emalloc((strlen(word) + 1) * sizeof wordlist[0][0]);
strcpy(wordlist[num_words], word);
num_words++;
}

for (i=0; i < num_words; i++){
    average += strlen(wordlist[i]);
}
average = average / num_words;

for(i =0; i < num_words;i++){
    if (strlen(wordlist[i]) > average){
        print_array(wordlist[i], strlen(wordlist[i]));
        printf("\n");
    }

}

if (num_words > 0){
    fprintf(stderr, "%.2f\n", average);
}

for (i=0; i < num_words; i++){
    free(wordlist[i]);
}
return EXIT_SUCCESS;

}

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