/**************************************** * Program that shows how a more complex * example may be solved in C. By the * end of the semester, the code in this * program will be fully understood by * CSC230 students. ***************************************/ #include #include /**************************************** * Function header that takes the * difference of the two ints provided * as parameters ***************************************/ static int compute_length (int, int); /**************************************** * Starts the program and handles the * user interaction ***************************************/ int main (void) { //Create data structure for use in program typedef struct { int left; int right; int length; } seg_t; //Create pointers to instances of the //data structure seg_t *seg1, *seg2; //Allocate memory for each instance of the //data structure seg1 = (seg_t *) malloc (sizeof (seg_t)); seg2 = (seg_t *) malloc (sizeof (seg_t)); //Get user input and assign to appropriate //members of data structure printf ("Enter left edge of segment 1: "); scanf ("%d", &(seg1->left)); printf ("Enter right edge of segment 1: "); scanf ("%d", &(seg1->right)); printf ("Enter left edge of segment 2: "); scanf ("%d", &(seg2->left)); printf ("Enter right edge of segment 2: "); scanf ("%d", &(seg2->right)); //Use the computer_length method to calculate //appropriate values and assign to memeber seg1->length = compute_length (seg1->left, seg1->right); seg2->length = compute_length (seg2->left, seg2->right); //Do final comparison and print message if (seg1->length == seg2->length) { printf ("Segment lengths are equal\n"); } else{ printf ("Segment lengths are NOT equal\n"); } //Always return something return 0; } /*************************************** * Returns the difference between left * and right **************************************/ int compute_length (int left, int right) { return (right - left); }