CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Problems
  • 1. Search
  • 2. Missing
  • 3. Group*
  • 4. Cluster*
  • Tips
Edit on GitHub
  1. Past Year Exam
  2. PE2 Review

PE2 (AY22/23)

PreviousPE2 (AY21/22)NextPE2 (AY23/24)

Last updated 6 months ago

Problems

Questions:

Official Answers:

1. Search

This is just a give-away question. Follow the instructions strictly will let you pass!

2. Missing

This can also be regarded as a give-away question. It just uses counting sort!

3. Group*

The key idea to solve this question is to draw a "tree", more details are discussed in Lab 09 - Backtracking.

But let's see how genius the answer is:

void group(size_t current, size_t num_groups, size_t students[], size_t n)
{
  if (current == n)
  {
    print(students, n);
    return;
  }
  // put current student in one of the exisitng groups

  for (size_t i = 1; i <= num_groups; i += 1)
  {
    students[current] = i;
    group(current + 1, num_groups, students, n);
  }
  // or start new group
  students[current] = num_groups + 1;
  group(current + 1, num_groups + 1, students, n);
}

By overwriting the value of students[current], we are actually doing our "back-tracking"! And once we have reached the end element, we print out the whole array. And our main should look like below:

int main()
{
  size_t n = cs1010_read_size_t();
  size_t *students = calloc(n, sizeof(size_t));
  if (students == NULL)
  {
    return 1;
  }
  // start with num_groups 0
  group(0, 0, students, n);
  free(students);
}

4. Cluster*

The idea of this problem you iterate through each pair in the network, if they are in contact, remove all their "cluster" contact by iterating through from 0 to n recursively.

#define CONTACT '1'
#define NO_CONTACT '0'

bool is_contact(char **network, size_t i, size_t j)
{
  if (j < i)
  {
    return network[i][j] == CONTACT;
  }
  return network[j][i] == CONTACT;
}

void remove_contact(char **network, size_t i, size_t j)
{
  if (j < i)
  {
    network[i][j] = NO_CONTACT;
  }
  else
  {
    network[j][i] = NO_CONTACT;
  }
}

void remove_cluster(char **network, size_t n, size_t i)
{
  for (size_t k = 0; k < n; k += 1)
  {
    if (is_contact(network, i, k))
    {
      remove_contact(network, i, k);
      remove_cluster(network, n, k);
    }
  }
}

long count_cluster(char **network, size_t n)
{
  long count = 0;
  for (size_t i = 0; i < n; i += 1)
  {
    for (size_t j = 0; j <= i; j += 1)
    {
      if (is_contact(network, i, j))
      {
        count += 1;
        remove_contact(network, i, j);
        remove_cluster(network, n, i);
        remove_cluster(network, n, j);
      }
    }
  }
  return count;
}

Tips

  1. Refer back to Lab 09 - Backtracking when doing the backtracking problems! Usually, an easier way to help you understand is to draw a "tree".

This question is a variant of , but the given solution provides a genius way to print out the result using array

This question seems to be a variant of , but actually it is a problem that tests your recursive thinking.

5. Stone
Ex5 Q6 Social
136KB
pe2-2223-s1.pdf
pdf
Questions
118KB
pe2-2223-s1-with-comments.pdf
pdf
Official Answers