Post

Week 12: Dynamic Allocation — Solution

Week 12: Dynamic Allocation — Solution

Exercise Solutions

Exercise 1: Dynamic array — largest and smallest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n;
    printf("Enter the size of the array: ");
    scanf("%d", &n);

    int *arr = (int *)malloc(n * sizeof(int));
    if (arr == NULL) {
        printf("failed to allocate\n");
        return 1;
    }

    printf("Enter %d elements of the array: ", n);
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);

    int largest = arr[0], smallest = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > largest)  largest  = arr[i];
        if (arr[i] < smallest) smallest = arr[i];
    }

    printf("Largest element present in the given array is : %d\n", largest);
    printf("Smallest element present in the given array is : %d\n", smallest);

    free(arr);
    return 0;
}

Example Output:

1
2
3
4
Enter the size of the array: 3
Enter 3 elements of the array: 10 9 22
Largest element present in the given array is : 22
Smallest element present in the given array is : 9

Exercise 2: Utilizing realloc — insert a number at any position

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n;
    printf("Enter size of the array : ");
    scanf("%d", &n);

    int *arr = (int *)malloc(n * sizeof(int));
    if (arr == NULL) {
        printf("failed to allocate\n");
        return 1;
    }

    printf("Enter elements in array : ");
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);

    int value, pos;
    printf("Enter element to insert : ");
    scanf("%d", &value);
    printf("Enter the element position : ");
    scanf("%d", &pos);

    int *tmp = (int *)realloc(arr, (n + 1) * sizeof(int));
    if (tmp == NULL) {
        printf("failed to reallocate\n");
        free(arr);
        return 1;
    }
    arr = tmp;

    // shift elements from position `pos` one slot to the right
    for (int i = n; i > pos; i--) {
        arr[i] = arr[i - 1];
    }
    arr[pos] = value;
    n += 1;

    printf("Array elements after insertion : ");
    for (int i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");

    free(arr);
    return 0;
}

Example Output:

1
2
3
4
5
Enter size of the array : 5
Enter elements in array : 0 1 2 3 4
Enter element to insert : 9
Enter the element position : 2
Array elements after insertion : 0 1 9 2 3 4

Exercise 3: Scoreboard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char subject[30];
    int  mark;
} Course;

int main(void) {
    int n;
    printf("Enter the number of records: ");
    scanf("%d", &n);

    Course *courses = (Course *)malloc(n * sizeof(Course));
    if (courses == NULL) {
        printf("failed to allocate\n");
        return 1;
    }

    for (int i = 0; i < n; i++) {
        printf("Enter subject and marks:\n");
        scanf("%29s %d", courses[i].subject, &courses[i].mark);
    }

    int total = 0;
    printf("Displaying Information:\n");
    for (int i = 0; i < n; i++) {
        printf("%-8s %d\n", courses[i].subject, courses[i].mark);
        total += courses[i].mark;
    }

    printf("Your average  = %d\n", total / n);

    free(courses);
    return 0;
}

Example Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enter the number of records: 4
Enter subject and marks:
Math 100
Enter subject and marks:
English 80
Enter subject and marks:
Korean 94
Enter subject and marks:
Science 77
Displaying Information:
Math     100
English  80
Korean   94
Science  77
Your average  = 87
This post is licensed under CC BY 4.0 by the author.