// Introducing you to the user defined data structure - STRUCT // It is basically a mixed bag of tricks that you are in charge of // Sasha Vassar Week 2 Lecture 3 // Three main points to a struct: // 1. Define (define what is in this mixed bag of tricks) // 2. Declare (let the program know I will be using this structure, and the variable name I will use that will use this type of structure) // 3. Initialise (Access the members of the struct to assign them some values // YAY! You are now a wizard (wizardess, witch?) who can define their // own data types #include //1. Define struct coordinate { int x; int y; }; // Problem: I have the coordinate points of two points and want // to compute the midpoint of the line the two points make (formula // for midpoint ((x1+x2)/2, (y1+y2)/2) int main (void) { //2. Declare struct coordinate point_one; struct coordinate point_two; double midpoint_x; double midpoint_y; int scanf_return; //3. Initialise printf("Please enter the x and y coordinates of point one: "); scanf_return = scanf("%d %d", &point_one.x, &point_one.y); if (scanf_return != 2) { printf("You did not enter two coordinate points.\n"); return 1; } printf("Please enter the x and y coordinates of point two: "); scanf("%d %d", &point_two.x, &point_two.y); if (scanf_return != 2) { printf("You did not enter two coordinate points.\n"); return 1; } midpoint_x = (point_one.x + point_two.x) / 2.0; midpoint_y = (point_one.y + point_two.y) / 2.0; printf("Point one (%d, %d)\nPoint two (%d, %d)\nThey have the midpoint (%.1lf, %.1lf)\n", point_one.x, point_one.y, point_two.x, point_two.y, midpoint_x, midpoint_y); return 0; }