CS-211 Lab 4 -- Using Control Flow


This week, we are going to write a C program that uses control flow. You may want to reference the presentation L07 Control Flow lecture.

Today, we are going to make a very simple minded computer game, similar to the game called battleship. Our users will give us the x and y coordinates of a point on a grid, and we will test to see if that point is within specific rectangles that are on the grid, and let the user know if his point either falls within one of the rectangles, or falls outside of all of the rectangles.

Getting Started

Make a lab4 sub-directory of your cs211 directory. Create and edit a file called hit.c.

We will use the C library functions printf and atof so at the top of your factors.c file, add:

#include <stdio.h>
#include <stdlib.h>
We also need the standard main function, with the standard parameters to collect the command line argument. Our program is going to take two command line arguments, so make a check, like last week, to make sure we get the right number of arguments. If not, print an error message telling the user how to invoke your program, and return a "1" to indicate something bad happened.

Once you are sure you have the right number of arguments, convert both arguments from a strings into an floating point numbers using the atof function to convert to a floating point number.

This week, we won't worry about invalid input values. If the input value is incorrect, the atof function will return a zero, and that's good enough to keep going.

Write a function called isHit which takes six arguments, as follows:

  1. Point x coordinate
  2. Point y coordinate
  3. Rectangle lower left x coordinate
  4. Rectangle lower left y coordinate
  5. Rectangle upper right x coordinate
  6. Rectangle upper right y coordinate

The isHit function should return a one if the point (x,y) falls within the rectangle, and a zero if the point is outside the rectangle.

Your main function should invoke the isHit function with the user specified coordinate checked against the following three rectangles (specified by the lower left coordinate and upper right coordinate):

If the point the user specified is within any of those rectangles, your program should print the following message:

  printf("Point (%f,%f) hit one of the rectangles!\n",px,py);

If the point the user specific does not fall within any of the rectangles, your program should print the following message:

  printf("Point (%f,%f) missed all of the rectangles.\n",px,py);

(I used px and py as the variable names for the user specified x and y coordinates. If you used something different, change the print statements above to use your variables.)

Test your code with various different command line arguments to make sure it works.

Lab Submission

Go to MyCourses, and open the CS-211 page, and choose the "Content" hyperlink on the left list of options. You will see a "Lab 04 Submission" icon to upload your hit.c file. Make this submission before the due date of Friday, September 29 at 11:59 PM. You may submit as many times as you want before the due date, but only the latest submission will be graded.

Lab Grading

This lab is worth 10 points. You will get deductions as follows: