Menu
Coddy logo textTech

Recap: Point Manager

Part of the Object Oriented Programming section of Coddy's C journey — lesson 11 of 61.

challenge icon

Challenge

Easy

Let's build a complete Point module that manages a 2D coordinate. This recap challenge brings together everything you've learned about treating structs as objects—using pointers to modify state and const pointers for read-only operations.

You'll create three files:

  • point.h: Declare a Point struct with two integer members: x and y. Also declare two functions that act as methods:
    • point_move — takes a pointer to Point along with two integers (dx and dy), and shifts the point's position by adding these values to x and y
    • point_print — takes a const pointer to Point and displays its coordinates (this function should not modify the point)
    Use include guards with the symbol POINT_H.
  • point.c: Implement both functions. Think carefully about which function needs a regular pointer (to modify the point) and which uses a const pointer (to safely read without modification).
  • main.c: Create a Point, move it around, and display its position at different stages.

You will receive four integer inputs: the initial x, the initial y, the dx value (amount to move horizontally), and the dy value (amount to move vertically).

In your main file, initialize a Point with the starting coordinates, print its initial position, move it by the given amounts, then print its new position.

The point_print function should output in this format:

Point: ({x}, {y})

For example, with inputs 3, 5, 2, and -1, the output would be:

Point: (3, 5)
Point: (5, 4)

Try it yourself

#include <stdio.h>
#include "point.h"

int main() {
    // Read input values
    int x, y, dx, dy;
    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%d", &dx);
    scanf("%d", &dy);
    
    // TODO: Create a Point with initial coordinates (x, y)
    
    // TODO: Print the initial position using point_print
    
    // TODO: Move the point by (dx, dy) using point_move
    
    // TODO: Print the new position using point_print
    
    return 0;
}

All lessons in Object Oriented Programming