Given that much information about two such rectangles, it is possible to determine whether they intersect.

Similar documents
A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

gcc o driver std=c99 -Wall driver.c bigmesa.c

gcc o driver std=c99 -Wall driver.c everynth.c

Decision Logic: if, if else, switch, Boolean conditions and variables

CS 1044 Program 6 Summer I dimension ??????

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

CS 2604 Minor Project 1 DRAFT Fall 2000

Pointer Casts and Data Accesses

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

CS 2604 Minor Project 1 Summer 2000

The Program Specification:

CS 1044 Program 2 Spring 2002

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

CS 1044 Project 1 Fall 2011

CS 1044 Project 2 Spring 2003

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

Simple C Dynamic Data Structure

Creating a String Data Type in C

Pointer Accesses to Memory and Bitwise Manipulation

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Pointer Accesses to Memory and Bitwise Manipulation

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

Pointer Accesses to Memory and Bitwise Manipulation

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

Both parts center on the concept of a "mesa", and make use of the following data type:

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

A Capacity: 10 Usage: 4 Data:

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

CS 2704 Project 1 Spring 2001

CS2304 Spring 2014 Project 3

CS 2604 Minor Project 3 DRAFT Summer 2000

CS 1044 Project 5 Fall 2009

File Navigation and Text Parsing in Java

CS 2505 Computer Organization I

Invoice Program with Arrays and Structures

Accessing Data in Memory

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

CpSc 1111 Lab 4 Formatting and Flow Control

a f b e c d Figure 1 Figure 2 Figure 3

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Stream Model of I/O. Basic I/O in C

CS 2704 Project 3 Spring 2000

CS 3114 Data Structures and Algorithms DRAFT Minor Project 3: PR Quadtree

File Navigation and Text Parsing in Java

cd h: mkdir -p CS101 cd CS101 curl -O unzip zipfile cd CS101_Exam4

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CSCE 206: Structured Programming in C++

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 2 Assigned: September 7, 2016 Due: Wednesday, September 14, 2016 by 2:30 pm

Project 1. due date Sunday July 8, 2018, 12:00 noon

Motivation was to facilitate development of systems software, especially OS development.

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 051 Homework Laboratory #2

Motivation was to facilitate development of systems software, especially OS development.

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

printf( Please enter another number: ); scanf( %d, &num2);

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 8 Assigned: October 26, 2016 Due: November 2, 2016 by 2:30pm

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

CS 142 Style Guide Grading and Details

5.Coding for 64-Bit Programs

ECE264 Spring 2013 Final Exam, April 30, 2013

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

CS 111X - Spring Final Exam - KEY

CpSc 1111 Lab 9 2-D Arrays

Collisions/Reflection

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CS 111X - Fall Test 1

Introduction. Table Basics. Access 2010 Working with Tables. Video: Working with Tables in Access To Open an Existing Table: Page 1

ECE15: Lab #4. Problem 1. University of California San Diego

CS 1044 Programming in C++ Test 1 Spring 2011 Form B Page 1 of 12 READ THIS NOW!

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

3. When you process a largest recent earthquake query, you should print out:

Topic 6: A Quick Intro To C

CSE 5A Introduction to Programming I (C) Homework 4

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

Assignment 6: The Power of Caches

CSE wi Midterm Exam 2/8/18. Name UW ID #

N1793: Stability of indeterminate values in C11

This is CS50. Harvard University Fall Quiz 0 Answer Key

ENCE 3241 Data Lab. 60 points Due February 19, 2010, by 11:59 PM

Assignment 3: Inheritance

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CpSc 1111 Lab 5 Formatting and Flow Control

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

All about flow control

PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

Problem Score Max Score 1 Syntax directed translation & type

COMP 105 Homework: Type Systems

CS 1044 Program 1 Fall 1998

Without fully opening the exam, check that you have pages 1 through 11.

Programming Assignment 2 ( 100 Points )

CS 2704 Project 2: Elevator Simulation Fall 1999

Transcription:

Decision-making in C (Possibly) Intersecting Rectangles Background A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of one corner and the height and width; for example: Given that much information about two such rectangles, it is possible to determine whether they intersect. Assignment For part of this assignment, you will use very basic C techniques to implement a C function to determine whether two rectangles (as described above) do intersect. For our purposes, consider each rectangle to be closed; i.e., the points on the border of the rectangle are considered to be part of the rectangle. So, it's possible for two rectangles to intersect in many ways, from a single point up to being entirely identical. You will provide an implementation for a C function that satisfied the conditions stated in the header comment: / Determines whether two rectangles, A and B, intersect. Pre: aswx and aswy specify the SW (lower, left) corner of A aheight specifies the vertical dimension of A awidth specifies the horizontal dimension of A bswx and bswy specify the SW (lower, left) corner of B bheight specifies the vertical dimension of B bwidth specifies the horizontal dimension of B Returns: true if A and B share at least one point; false otherwise / int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth); Note that it's perfectly valid for some, or all, of the coordinates to be negative. To be complete, you will finish the implementation of the short C program that is supplied later in this specification (and on the course website). The supplied program provides some code and comments indicating how it can be completed. The supplied program will not compile. You must add code in the indicated places. This is a purely individual assignment! 1

The program will read test data from an input file named TestData.txt. Here is a sample file: A B SWx SWy Height Width SWx SWy Height Width -2994-5134 5 5-2991 -5135 4 4 7903-4382 6 4 7895-4384 5 5-2453 -6046 3 4-2461 -6042 5 5-8714 -2687 4 4-8716 -2689 8 8 The file begins with a two-line header describing the contents. The header is followed by one or more lines of test data; each line contains eight integer values, specifying the SW and NE corners of two rectangles, in the order indicated by the header lines. The upper limit on the number of lines of test data is unspecified, so the program that reads the file must be designed so that it works correctly by detecting the end of the input data. The program will write results to a file named Results.txt. Here is a sample output file that corresponds to the input file shown above: Rectangle A Rectangle B SW corner Height Width SW corner Height Width ------------------------------------------------------------------- (-2994, -5134) 5 5 (-2991, -5135) 4 4 overlap detected ( 7903, -4382) 6 4 ( 7895, -4384) 5 5 no overlap detected (-2453, -6046) 3 4 (-2461, -6042) 5 5 no overlap detected (-8714, -2687) 4 4 (-8716, -2689) 8 8 overlap detected ------------------------------------------------------------------- The file begins with two lines describing the contents of the file and a line of hyphens marking the beginning of the results table; those are followed by pairs of lines of data corresponding to the test cases; for each test case, the rectangle data is logged, formatted precisely as shown, followed (on a separate line) by the string "overlap detected" or "no overlap detected", as appropriate. You should assume that the x and y coordinates will be restricted to the range -9999 to 9999 and format your output exactly as shown above. You should test your solution thoroughly; there is no guarantee that the supplied test data covers all cases. You may assume that the supplied data will be logically correct. You should not make any other assumptions about the test data values. Be warned: this problem may be harder than it appears. You should think carefully about your analysis of the possible relationships the two rectangles could have in the plane. That does not mean that a solution needs to be complicated. Mine involves a total of 6 C language statements, which could be reduced to a single statement, if I were willing to sacrifice clarity and readability. Make useful comments in your implementation of Overlap(). The same general guidelines for commenting that you have been taught in your Java courses should provide sufficient guidance. You will submit your completed.c file, making changes where indicated in the supplied code. Your submission will be compiled, tested and graded according to the formatting of your output and how many cases your solution handles correctly. You will be allowed up to ten submissions for this assignment. Use them wisely. Test your program thoroughly before submitting it. Make sure that your program produces correct results for every test case you can think of. If you do not get a perfect score, analyze the problem carefully and test your fix with the test data shown in the Curator grade report, before submitting again. The highest score you achieve will be counted. This is a purely individual assignment! 2

The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: http:www.cs.vt.edu/curator/ Supplied code: The following code is supplied on the course website. You should use this file as your starting point. Feel free to modify the design used here, but your solution must conform to the requirements stated above, and the Overlap() function must retain the specified interface. You are expected to provide useful comments in the body of the function Overlap(). / CS 2505 Spring 2014 Supplied framework for Intersecting Rectangles project. Your task is to complete the supplied code to satisfy the posted specification for this assignment. The occurrences of "???" in the code below indicate places where you must complete the implementation. Student: <ENTER YOUR NAME HERE> PID: <ENTER YOUR VT EMAIL PID HERE> / #include <stdio.h> for I/O functions #include <stdlib.h> generally useful #include <stdbool.h> for the bool type #include <stdint.h> for exact-width integer types #include <inttypes.h> for exact-width integer output #define MAXLEN 100 Declaration for function Overlap(): int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth); int main( ) { FILE Data / =??? /; FILE Log / =??? /; 1: Open the test data file 2: Open the results log file int32_t aswx, aswy, aheight, awidth, specification of 1st rectangle bswx, bswy, bheight, bwidth; specification of 2nd rectangle char Line[MAXLEN + 1]; fgets(line, MAXLEN, Data); fgets(line, MAXLEN, Data); Read header lines / fprintf(log,???); / 3: Write data header lines to the log file / fprintf(log,???); / 4: Read the data for first pair of rectangles: int32_t numread / = fscanf(data,??? ) /; while ( numread / ==??? /) { 5: Check if all values were read 6: Write data for current rectangles to log: / fprintf(log,??? ); / This is a purely individual assignment! 3

7: Test for an overlap and log the results: / if (??? ) { fprintf(log, " else { overlap detected\n"); fprintf(log, " no overlap detected\n"); / 8: Read data for next pair of rectangles: / numread = fscanf(data,??? ); / 9: Write the marker for the end of the table: / fprintf(log,???); / /???; / 10: Close the input and output files /???; / return 0; / Determines whether two rectangles, A and B, intersect. Pre: aswx and aswy specify the SW (lower, left) corner of A aheight specifies the vertical dimension of A awidth specifies the horizontal dimension of A bswx and bswy specify the SW (lower, left) corner of B bheight specifies the vertical dimension of B bwidth specifies the horizontal dimension of B Returns: true if A and B share at least one point; false otherwise / int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth) { bool overlapdetected = false; Your implementation goes here... return overlapdetected; This is a purely individual assignment! 4

Pledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: On my honor: - I have not discussed the C language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from an authorized source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator System. <Student Name> Failure to include this pledge in a submission is a violation of the Honor Code. Suggestions for implementing your solution: There are two different foci for this assignment. You must deal with implementing the input and output operations, and you must deal with implementing the correct logic to determine whether two rectangles overlap. I'd begin with the first issue. In the posted version of the supplied code, all the lines that you must complete have been commented so that the code will compile (albeit with warnings if you use Wall). Complete the code to open the input and output files (1, 2), and to close the input and output files (10). Note: you'll need to download one of the data files from the website, and save it to the same directory as your source code, and make sure it has the right name (TestData.txt). Next, complete the code to read a line of data (4, 8) into the appropriate variables. At this point, I'd add a printf() statement just at the beginning of the while loop body and write out the values that were placed into those variables; that way I can easily see whether I've got the input code right. You'll also have to complete the test for the while loop (5), although this may seem to work properly as it is given. Until you can correctly acquire the test data from the input file, there's not much point in worrying about the logic for the Overlap() function. Once I'm sure my input code is correct, I'd remove that printf() statement (or comment it out). Next, I'd focus on the implementation of Overlap(). You may find this is harder than you expect, because there's a seductive, inadequate way of thinking about this. Since you've got access to test data files and the correct results for those tests, it should be easy to determine whether you've got this right or not. In order to actually test this, you'll want to complete the if statement (7) that's commented out in the posted code. Once I've got Overlap() tested, I'd complete the code to write the header lines (3), and the rectangle data (6), and the marker for the end of the output table (9) to the results file. Use the sample results files to determine how to format your output. This is a purely individual assignment! 5