COMP 1917 Computing 1
Session 2, 2016

Tutorial - Week 8


Presentation Topic for Week 8

Discuss how and why abstraction is used in computer science? Can you think of an example of a function you used in this course where the internal complexity was abstracted away?


    In the previous tutorial, we created a struct point with the following definition:
        struct point {
          double x;
          double y;
        };
    
    In the following questions, we will refer to the struct through pointers, using typedef struct point * Point for convenience.

  1. Rewrite this expression using the "arrow" notation:

    (*((*speeding[s]).date)).year

  2. Write a function

    Point newPoint(double x, double y)

    that creates a new point by initialising its values and uses malloc to dynamically allocate the structure.

  3. Write a function

    void destroyPoint(Point p)

    that deletes a point by freeing its underlying memory.

  4. Add functions double getX(Point p) and double getY(Point p) that returns the corresponding coordinate of a given point.

  5. Write a function

    void setPoint(Point p, double x, double y)

    that sets the corresponding coordinates of a given point.

  6. Write a function

    void shiftPoint(Point p, double xDist, double yDist)

    that adds given distances to the corresponding coordinates of a point.

  7. Separate the implementation from the user's function calls by placing the function declarations and Point typedef in a Point.h file and the function and struct definitions in Point.c. Only the main function should be left in your original file, which you can name testPoint.c (make sure to #include "Point.h" at the top of each .c file). How would you now compile this program?

  8. Add the following lines to your main function
    
    	Point p = newPoint(0,0);
    	shiftPoint(p,1,-1);
    	printf("x value is: %f\n", getX(p));
    	printf("y value is: %f\n", getY(p));
    	destroyPoint(p);
    
    
    What is the output?

    1. Change the implementation of shiftPoint() to call setPoint() (or not call it if your implementation already does). Does the output change? Should it?

    2. Why would changing getX(p) to p->x not work?

  9. In terms of memory, what is the difference between our previous struct definition and our new method of handling pointers to the struct? Discuss the pros and cons of both.

  10. Why is it important to release dynamically allocated memory after we are done with it? Can we still use this memory or assert anything about it after freeing it?


Presentation Topic for Week 9

What is the difference between black box and white box testing? Demonstrate how both work by writing corresponding tests for the point.c program we developed in the Week 8 tutorial.