Solving Programming Technique midterm test - CQ 22-23

Question 1:

Untitled

For the simplicity of this blog, I’ll use rand to generate random numbers.

struct Circle {
  int x, y, radius;
};

void generateAndStoreRandomCircles() {
  int n = 4;
  Circle* circles = new Circle[n];
  for (int i = 0; i < n; i++) {
    int x = rand() % 100, y = rand() % 100, radius = 1 + rand() % 100;
    Circle tmp{x, y, radius};
    circles[i] = tmp;
  }

  ofstream out("circles.bin", ios::binary);
  for (int i = 0; i < n; i++) {
    out.write((char*)&circles[i], sizeof(Circle));
  }
  out.close();
  delete []circles;
}

Question 2:

Untitled

Circle* getCirclesFromFile(int& n) {
  ifstream inp("circles.bin", ios::binary);
  if (!inp.is_open())
    return nullptr;
  inp.seekg(0, ios::end);
  int fileSize = inp.tellg();
  n = fileSize / sizeof(Circle);
  Circle* circles = new Circle[n];
  inp.seekg(0, ios::beg);
  for (int i = 0; i < n; i++) {
    inp.read((char*)&circles[i], sizeof(Circle));
  }
  inp.close();

  return circles;
}

int distance(Circle a, Circle b) {
  return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

bool isOverlappingOrIntersectingCircle(Circle a, Circle b) {
  if (distance(a, b) < abs(a.radius - b.radius) * abs(a.radius - b.radius)) return false;
  return distance(a, b) <= (a.radius + b.radius) * (a.radius + b.radius);
}

struct Pair {
  int i, j;
};

Pair* getOverlappingOrIntersectingCircles(Circle* circles, int n, int& m) {
  Pair* answer = new Pair[n * n];
  m = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (isOverlappingOrIntersectingCircle(circles[i], circles[j])) {
        answer[m] = Pair{i, j};
        m++;
      }
    }
  }

  return answer;
}
void printList(Circle* a, Pair* b, int m) {
  ofstream out("cuts.txt");
  out << m << '\n';
  for (int k = 0; k < m; k++) {
    out << a[b[k].i].x << ' ' << a[b[k].i].y << ' ' << a[b[k].i].radius << ' '
        << a[b[k].j].x << ' ' << a[b[k].j].y << ' ' << a[b[k].j].radius << '\n';
  }
  out.close();
}

Question 3:

Untitled

  • int *(*p)[50];

    This means that p is a pointer (definitely) points to a 3-dimensional array, which has 50 elements in the third dimension. For the first two dimension, their size can be dynamically assigned.

  • int (* const p[1000])();

    This means that p is a constant pointer points to an array that accepts functions as its elements (so weird but yes). But if p is constant and we don’t assign the value right now, we can’t change it later, so this declaration is not quite right.

  • int *(*p())();

    This means that p is a function doesn’t accept any arguments, and it returns another function, which also doesn’t accept any arguments, and returns int *.

Side notes:

If you’re wondering how tf can a pointer points to a function (like I was before writing this post) then you can keep in mind that functions has their own addresses in the memory, and pointers are capable of storing them.

Knowing this can be handy when we want to, for example, sort an array with different criteria (instead of ascending or descending).

We can define a function that either returns 1 if the criteria is met, or -1 otherwise, then pass it into our sorting function:

int isCloserToCenterThan(Circle a, Circle b) {
  if (a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y)
    return 1;
  return -1;
}

template <typename T>
void sortArrayWithCustomComparator(T* a, int n, int (*comp)(T, T)) {
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (comp(a[i], a[j]) < 0) {
        swap(a[i], a[j]);
      }
    }
  }
}

int main() {
  Circle a[] = {
      Circle{1, 2, 5},
      Circle{2, 2, 6},
      Circle{2, 1, 2},
      Circle{1, 3, 4},
  };
  sortArrayWithCustomComparator(a, 4, isCloserToCenterThan);
  for (int i = 0; i < 4; i++) {
    cout << a[i].x << ' ' << a[i].y << '\n';
  }
}

Recap:

Phew, such a challenging test. I think that with only 60 minutes, I’ll get like 6 or something.

I hope that you’ve learnt something new (and useful :D) knowledge in C++.

Also, I’d like to thank @pero10kai for helping me in the third question.

P/s: if you want to know more about template <typename T>, I suggest searching about it, or wait for my next post 😉.

Written on May 6, 2023