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

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

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:

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

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

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

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:

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

Pointer Casts and Data Accesses

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

Creating a String Data Type in C

CS 2604 Minor Project 1 DRAFT Fall 2000

CS 2604 Minor Project 1 Summer 2000

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.

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

CS 1044 Program 2 Spring 2002

Pointer Accesses to Memory and Bitwise Manipulation

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.

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

The Program Specification:

Pointer Accesses to Memory and Bitwise Manipulation

A Capacity: 10 Usage: 4 Data:

CS 1044 Project 1 Fall 2011

CS 1044 Project 2 Spring 2003

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

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:

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

Simple C Dynamic Data Structure

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

CS 2704 Project 1 Spring 2001

Accessing Data in Memory

File Navigation and Text Parsing in Java

// 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.

CS 2604 Minor Project 3 DRAFT Summer 2000

File Navigation and Text Parsing in Java

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

CS 1044 Project 5 Fall 2009

CS2304 Spring 2014 Project 3

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

CS 2505 Computer Organization I

CpSc 1111 Lab 4 Formatting and Flow Control

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

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

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

Invoice Program with Arrays and Structures

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

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

Topic 6: A Quick Intro To C

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

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

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

CS 2704 Project 3 Spring 2000

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

N1793: Stability of indeterminate values in C11

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

CSE 333 Midterm Exam July 24, Name UW ID#

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

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

CS 142 Style Guide Grading and Details

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

CS 2505 Fall 2018 Data Lab: Data and Bitwise Operations Assigned: November 1 Due: Friday November 30, 23:59 Ends: Friday November 30, 23:59

Do not start the test until instructed to do so!

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

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

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

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

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

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

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

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

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

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.

CS 051 Homework Laboratory #2

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

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

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

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

Exercise Session 2 Systems Programming and Computer Architecture

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

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

ECE264 Spring 2013 Exam 1, February 14, 2013

ENCM 369 Winter 2015 Lab 6 for the Week of March 2

EE 352 Lab 3 The Search Is On

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub.

CSE 333 Midterm Exam 5/10/13

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

CS 2505 Fall 2013 Data Lab: Manipulating Bits Assigned: November 20 Due: Friday December 13, 11:59PM Ends: Friday December 13, 11:59PM

ECE264 Summer 2013 Exam 1, June 20, 2013

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

ECE264 Fall 2013 Exam 1, September 24, 2013

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: So, we may consider the SW corner, height and width to form a set of defining attributes of such a rectangle. Given that much information about two such rectangles, it is possible to compute he attributes of their intersection, if any. 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, and if so specify the intersection by giving the lower-left corner, the height and the width. 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 test driver and for a C function that satisfies the conditions stated in the header comment: / Determines whether two rectangles, A and B, intersect, and writes an appropriate message to the supplied output stream. Pre: Log is open on a file 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 / void Intersection(FILE Log, int32_t aswx, int32_t aswy, uint32_t aheight, uint32_t awidth, 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. It's also perfectly valid for the height and/or width of the intersection to be zero (but not negative). To be more specific, 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. This is a purely individual assignment! 1

The completed 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 5 10 5 15 10 5 5 5 4 4 4 4 10 5 5 5 9 2 8 2 8 4 4 4 5 2 8 5 8 4 4 4 5 5 5 5 5 5 5 5 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 corners of two rectangles, along with their heights and widths, 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 ------------------------------------------------------------------- ( 5, 10) 5 15 ( 10, 5) 5 5 ( 10, 10) 0 5 ( 4, 4) 4 4 ( 10, 5) 5 5 no intersection ( 9, 2) 8 2 ( 8, 4) 4 4 ( 9, 4) 4 2 ( 5, 2) 8 5 ( 8, 4) 4 4 ( 8, 4) 4 2 ( 5, 5) 5 5 ( 5, 5) 5 5 ( 5, 5) 5 5 ------------------------------------------------------------------- The file begins with two lines describing the contents of the file and a line of hyphens marking the beginning of the results table. For each test case there are three lines of output. The first line contains the coordinates of the SW corner, height and width of each rectangle; the second line contains the coordinates of the SW corner, the height and the width of the rectangular intersection (or the string "no intersection"). The third line is blank. 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 four C functions and about 40 lines of code, not counting main(). You should consider writing helper functions, even for a short program like this; they clarify your logic. Make useful comments in your implementation of Intersection() and any helper functions. The same general guidelines for commenting that you have been taught in your Java courses should provide sufficient guidance. This is a purely individual assignment! 2

What to turn in: You will submit a Linux tar file containing your completed driver.c, Intersection.c, and Intersection.h, 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. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: Pledge: http:www.cs.vt.edu/curator/ 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. This is a purely individual assignment! 3

Suggestions for implementing and testing 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, and with determining the correct attributes of the intersection when they do. 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 Intersection() 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 Intersection(). 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 call to the function (7), as well as the function itself. Once I've got Intersection() tested, I'd complete the code to write the header lines (3), and the rectangle data (6), and 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! 4

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 Intersection() function must retain the specified interface. You are expected to provide useful comments in the body of the function Intersection() and any helper functions you incorporate into your solution. The supplied code will compile, with warnings: CentOS code> gcc -o driver -std=c99 -Wall driver.c Intersection.c driver.c: In function main : driver.c:57: warning: statement with no effect driver.c:33: warning: unused variable bwidth driver.c:33: warning: unused variable bheight driver.c:33: warning: unused variable bswy driver.c:33: warning: unused variable bswx driver.c:32: warning: unused variable awidth driver.c:32: warning: unused variable aheight driver.c:32: warning: unused variable aswy driver.c:32: warning: unused variable aswx driver.c:25: warning: unused variable Log driver.c:23: warning: unused variable datafname driver.c:22: warning: unused variable logfname driver.c:29: warning: Data is used uninitialized in this function driver.c:46: warning: numread is used uninitialized in this function (The warnings above were issued by gcc version 4.8.3; a different version may issue somewhat different warnings.) But executing it will produce a segfault until you have completed certain parts of it. driver.c: / CS 2505 Summer 2015: driver.c Supplied driver 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 #include "Intersection.h" for declaration of Intersection() #define MAXLEN 100 maximum number of chars in an input line int main() { char logfname[] = "Results.txt"; char datafname[] = "TestData.txt"; FILE Log / =??? /; FILE Data / =??? /; 1: open output file 2: open input file This is a purely individual assignment! 5

char Line[MAXLEN + 1]; fgets(line, MAXLEN + 1, Data); fgets(line, MAXLEN + 1, Data); read header lines int32_t aswx, aswy, aheight, awidth, specification of 1st rectangle bswx, bswy, bheight, bwidth; specification of 2nd rectangle 3: Write output header: 4: Read data for first two rectangles; return value is how many values were actually assigned to variables: int32_t numread / = fscanf(data,???) /; 5: The logic here is that as long as we've read the expected number of values, we will process what we've just read, and try to read more data. while ( numread / ==??? / ) { 6: Write data for current rectangles to log (one or two statements): 7: Compute intersection, if any, and log the results: / Need an appropriate function call here. / } 8: Try to read data for next pair of rectangles: numread / = fscanf(data,???) /; 9: Write the marker for the end of the results table: 10: Close the input and output files: /??? / } Exit the program: return 0; Intersection.h: #ifndef INTERSECTION_H #define INTERSECTION_H #include <stdio.h> #include <stdint.h> for I/O functions for exact-width integer types void Intersection(FILE Log, int32_t aswx, int32_t aswy, uint32_t aheight, uint32_t awidth, int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth); #endif This is a purely individual assignment! 6

Intersection.c: / CS 2505 Summer 2015: Intersection.c Supplied framework for Intersecting Rectangles project. Your task is to complete the supplied code to satisfy the posted specification for this assignment. Student: <ENTER YOUR NAME HERE> PID: <ENTER YOUR VT EMAIL PID HERE> / #include "Intersection.h" #include <stdlib.h> #include <inttypes.h> generally useful for exact-width integer output Declarations for helper functions should go here: / Determines whether two rectangles, A and B, intersect, and writes an appropriate message to the supplied output stream. Pre: Log is open on a file 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 / void Intersection(FILE Log, int32_t aswx, int32_t aswy, uint32_t aheight, uint32_t awidth, int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth) { } Your implementation goes here: Implementations of helper functions should go here: This is a purely individual assignment! 7