CodeBlanks Logo Icon CodeBlanks

Completing tasks

Learn to update task status with CompleteTask.

The complete_task function

To mark a task as completed we write the complete_task function. It sets the done field of the task to 1.

void complete_task(struct Task tasks[], int id) {
    tasks[id - 1].done = 1;
}

We use id - 1 because ids start at 1 but array indices start at 0. So we convert the 1-based id into a 0-based index.

1-based id, 0-based index

Task ids start at 1 (the first task has id 1). But C arrays start at 0. So we write tasks[id - 1] to convert the 1-based id into a 0-based index.

complete_task(tasks, 1);  /* tasks[0].done = 1 */
complete_task(tasks, 2);  /* tasks[1].done = 1 */

This split reconciles user-friendly ids with array access. The user types 1, the program uses 0-based.

Practice Add the complete_task function

Add the void complete_task(struct Task tasks[], int id) function to the code below. Inside it write tasks[id - 1].done = 1;. In main add a complete_task(tasks, 1); call before the list_tasks call (to complete the first task). Output should be 1 Shopping 1 and 2 Sport 0.

Starter code

main.c
#include <stdio.h>
#include <string.h>

struct Task {
    int id;
    char title[50];
    int done;
};

int add_task(struct Task tasks[], int count, const char *title) {
    tasks[count].id = count + 1;
    strcpy(tasks[count].title, title);
    tasks[count].done = 0;
    return count + 1;
}

void list_tasks(struct Task tasks[], int count) {
    for (int i = 0; i < count; i++) {
        printf("%d %s %d\n", tasks[i].id, tasks[i].title, tasks[i].done);
    }
}

int main(void) {
    struct Task tasks[10];
    int count = 0;
    count = add_task(tasks, count, "Shopping");
    count = add_task(tasks, count, "Sport");
    list_tasks(tasks, count);
    return 0;
}

Sample solution

main.c
#include <stdio.h>
#include <string.h>

struct Task {
    int id;
    char title[50];
    int done;
};

int add_task(struct Task tasks[], int count, const char *title) {
    tasks[count].id = count + 1;
    strcpy(tasks[count].title, title);
    tasks[count].done = 0;
    return count + 1;
}

void list_tasks(struct Task tasks[], int count) {
    for (int i = 0; i < count; i++) {
        printf("%d %s %d\n", tasks[i].id, tasks[i].title, tasks[i].done);
    }
}

void complete_task(struct Task tasks[], int id) {
    tasks[id - 1].done = 1;
}

int main(void) {
    struct Task tasks[10];
    int count = 0;
    count = add_task(tasks, count, "Shopping");
    count = add_task(tasks, count, "Sport");
    complete_task(tasks, 1);
    list_tasks(tasks, count);
    return 0;
}

Expected output

1 Shopping 1
2 Sport 0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match completion concepts

Term Meaning
done = 1 completed
done = 0 pending
id - 1 array index
tasks[i] task array

2 How do you mark a task as completed?

  • tasks[i].id = 1
  • tasks[i].title = "done"
  • tasks[i].done = 1 Correct answer
  • tasks[i] = NULL

Explanation: Setting the done field to 1 marks the task as completed.

3 Mark as done

Question
tasks[id].____;  /* mark task as completed */
Answer
tasks[id].done = 1;  /* mark task as completed */

Explanation: done = 1 marks the task as completed.

All lessons in Beginner