Docsity

Assignment 7 for Programming Using C | CGS 3460, Assignments of Computer Science

Toggle sidebar
bg1
CGS 3460 - Programming Using C
Summer 2009
Assignment #7
20 Points
Assigned: 07/10/2009, Friday
Due: 07/17/2009, Fri, 11:59 PM
At this point in the semester we have discussed how to use header files. Most, if not all
of the remaining homework problems will require you to declare and define functions
using a pX.c and a pX.h file. The pX.h file will then be included from our file which
contains the main() function and your functions will be tested from our main() function.
Please be sure to pay close attention to the name of the files specified.
1. (2.5 Points) Write the MACRO
MY_ASSERT(condition, msg)
This macro should print msg and exit the program only if condition is true. An
example call to MY_ASSERT could be
MY_ASSERT(5 > 3, “This is the message”);
This would result in “This is the message” being printed to the screen and exiting
the program. Another case would be
MY_ASSERT(5 < 3, “This is the message”);
This call would have no effect on the program. Be sure to note the ';' after the calls
to MY_ASSERT.
Save this as p1.h. p1.h will be included from our driver file and tested using our
main() function.
2. (7.5 Points) The following structures are designed to store information about
objects on a graphics screen.
Note: HILITED text are corrections
struct point {int x, y; };
struct rectangle {struct point upper_left, lower_right; };
A point structure stores the x and y coordinates of a point on the screen. A
rectangle structure stores the coordinates of the upper left and lower right corners of a
rectangle. Define the following functions.
a. int getTheArea(struct rectangle r);- This will compute and return the area of
the given rectangle.
pf3

Partial preview of the text

Download Assignment 7 for Programming Using C | CGS 3460 and more Assignments Computer Science in PDF only on Docsity!

CGS 3460 - Programming Using C

Summer 2009

Assignment # 20 Points Assigned: 07/10/2009, Friday Due: 07/17/2009, Fri, 11:59 PM At this point in the semester we have discussed how to use header files. Most, if not all of the remaining homework problems will require you to declare and define functions using a pX.c and a pX.h file. The pX.h file will then be included from our file which contains the main() function and your functions will be tested from our main() function. Please be sure to pay close attention to the name of the files specified.

  1. (2.5 Points) Write the MACRO MY_ASSERT(condition, msg) This macro should print msg and exit the program only if condition is true. An example call to MY_ASSERT could be MY_ASSERT(5 > 3, “This is the message”); This would result in “This is the message” being printed to the screen and exiting the program. Another case would be MY_ASSERT(5 < 3, “This is the message”); This call would have no effect on the program. Be sure to note the ';' after the calls to MY_ASSERT. Save this as p1.h. p1.h will be included from our driver file and tested using our main() function.
  2. (7.5 Points) The following structures are designed to store information about objects on a graphics screen. Note: HILITED text are corrections struct point {int x, y; }; struct rectangle {struct point upper_left, lower_right; }; A point structure stores the x and y coordinates of a point on the screen. A rectangle structure stores the coordinates of the upper left and lower right corners of a rectangle. Define the following functions. a. int getTheArea(struct rectangle r); - This will compute and return the area of the given rectangle.

b. struct point getTheCenter(struct rectangle r); - This will compute the center of thegiven rectangle. If the x or y coordinate of the center point is not an integer, just store its truncated value in the point structure returned. c. struct rectangle shiftTheRectangle(struct rectangle r, int x, int y); - This will shift the given rectangle by the specified units in the respective direction. It returns the modified rectangle. d. int isItInYou(struct rectangle r, struct point p); This returns 1 if p lies within r and 0 otherwise. Put the declarations of these functions into p2.h and put there definitions into p2.c. p2.h will be included from our driver file and tested using our main() function.

  1. (10 Points) Write a function with the prototype: float stof(char* s); This will convert a numerical string into a floating point number. It should be able to handle exponential notation as well as fixed point notation. It should also handle negative numbers. So, the following strings would return 120: “120”, “1.2e2”, “12e1”, “12.e1”, “0.12e3”, “.12e3”. Similarly, the following strings would return -0.12: “-.12”, “- 0.12”, “-1.2e-1”, “-12e-2”, “-12.e-2”, “-0.12e0”, “-.12e0” If you wish, you may use the atoi function. This is available in the standard library and converts a string to an integer value. This may be helpful if you break up -12.54e-23 into 3 sections of “-12”, “54” and “-23”. You may NOT however use the standard library function atof. You may also assume the maximum size of s is 100 characters. (You may need this for using temporary string variables, depending on your implementation. Put the declarations of this function into p3.h and put its definition into p3.c. p3.h will be included from our driver file and tested using our main() function. Instructions for submitting Assignment 7
  2. First compress your program files into a tar file. For this, change to the directory where you have stored both the files. At the terminal command prompt (putty, Linux console, MAC terminal, etc.) type: tar cvf assign7.tar p1.h p2.c p2.h p3.c p3.h Let’s say you have stored your files in assign7 directory on your “rain” machine.

cd assign ls p1.h p2.c p2.h p3.c p3.h .. tar cvf assign7.tar p1.h p2.c p2.h p3.c p3.h ls