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
  • 4. List
  • 8. Search
  • 9. Substract
  • Tips
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Exercises

Exercise 4 - Dynamic Arrays and Strings

Problems

4. List

In this question, pay attention to how to traverse through a list using size_t type variable i.

8. Search

A really awesome question to practice "divide-and-conquer" thinking

9. Substract

A complex problem, don't know whether there will be a faster solution

My naive solution

Suppose before we do the subtraction, we already know which number is bigger. Let's say num1 is bigger than num2. The first thing we can derive is that the result (subtraction) will be at most the same length with num1. Then let's do the main part (substraction)

  1. We start from the last digit of each number, denote the last digit for num1 as d1 and the last digit for num2 as d2.

    1. If d1 is bigger than d2, we just store their difference into our result's last digit.

    2. Otherwise, we "borrow" one digit from the first nonzero digit in front of d1 and in this process, if we encounter zero digits, change them to 9.

    3. (Edge cases)

      1. num2 has run out of digits, that is we have done our substraction. We just move the remaining digits of num1 to our res.

      2. Otherwise, we have reached our very first digit of num1, since num1 is bigger than num2, we just set the corresponding digit of res to be the difference of the current digit of num1 and num2

  2. We move to the digit that is in front of the digit we have checked.

Since num1 is always greater than num2, we are sure that we can borrow successfully.

Tips

  1. Ask when to use malloc(), when to use calloc() and which one is recommended in CS1010?

  2. A tricky part in 9. Substract is the difference between char and long in C, don't mix them in ur code. Otherwise you will experience weird behavior.

  3. To avoid the trouble that size_t cannot be negative in the for loop, we can convert them into long explicitly.

PreviousExercise 3 - Fixed-Length ArraysNextExercise 6 - Searching and Sorting

Last updated 7 months ago