Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

QUES) write a program in c, which meets the following requirements. Requirements . Write a function that matches the following declaration int InRectangle( float pt[2],

QUES) write a program in c, which meets the following requirements.image text in transcribedimage text in transcribed

Requirements . Write a function that matches the following declaration int InRectangle( float pt[2], float rect[4] Argument pt[2 ] defines a point on the plane: pt[ 0 ] S the x-coordinate, pt[1 ] is the y-coordinate Argument rect[ 4] defines a rectangle on the same plane. rect[0] and rect[1] define the x- and y- cordinates respectively of one corner of the rectangle rect[ 2] and rect [3] define the opposite corner . . . Coordinates may be any valid floating point value, including negative values . The function returns int 0 (false) for any point that lies outside the rectangle, and 1 (true) for any other point (i.e. points inside and on the boundary of the rectangle) Guide and Testing It is very common to represent geometric figures using small, fixed-size arrays like this. Note that the size of the arrays are specified in the function declaration. This allows the compiler to check that the function is called with a correctly-sized array Since this task calls for a function only, and not a complete program, you need to write a program to test your function. The grading robot uses a program that looks something like this 1. // declaration of function to test 2. int InRectangle( float pt[2, float rect[4] 4. int main( int argc, char* argv[) 6. / define a rectangle from (1,1) to (2,2) 7, float rect[4] = {1.0, 1.0, 2.0, 2.0 }; 9. // define a point that is inside the rectangle 10. float p in[2]1.5, 1.5 12. // define a point that is outside the rectangle 13. loat p_out[21-12.5,0.5 15. // define a point that is on the edge of the rectangle 16. float p_ edge[ 2]1.0, 1.0); 18. / InRectangle() should return 0 (false) for points that are NOT in 19. // the rectangle, and non-zero (true) for points that are in the 20. / rectangle. Points on the edge are considered in* the rectangle. 21. 22. test 1 23. if( InRectangle( p_in, rect) 0 24.- 25 26 27. puts "error: should return true for p in." return 1: 1/ indicate error 29. // test 2 30. if( InRec tangle( p-out, rect ) != 0 ) 31 32 puts "error: should return false for p out." return 1; // indicate error 34.h 35 36.// test 3 37. E( 1nRectangle( p-edge, rect )-0 ) 38. puts( "error: should return true for p edge." return 1; // indicate error 40. 42 43. return 0; // all tests passed CLOSE The code tests that the function works correctly with these parameters 2 pout p edge p in The actual test used by the grading robot is longer and more comprehensive than this. Above, we gave a simplified example for clarity. Note the syntax for initializing arrays with constant values at compile-time, e.g. line 7. This only works for constant values, unfortunately You should always write a test for your functions. Consider an untested function to be incorrect. It helps to assume the function was written by a complete idiot who has no idea what they are doing, even if the author was you. Then test the function to find out what craziness that daft person implemented. Once a function passes all the tests you can think of, you might cautiously believe it might be correct. In software engineering, like any quality-focused pursuit, it helps to be skeptical. Compiling two files into one program Since the file you submit must not contain a main() function (see Submission section below), it is convenient to make a test program from two source files: one containing the InRectangle() function, and one with main). You can start out with the code above, and extend it to be a more thorough test Recall from above that you can compile a single program from multiple source files by simply listing the sources on the compiler command line: gcc -o test inrect.cmain.c Submissior Submit your solution as a C source file called inrect.c. The file must not contain a main) function, even though you wrote one for testing. The grading robot will compile your file along with it's own main(). Since there can only be one main), if you provide a second, the compilation will fail

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

i need a MATLAB CODE THAT DETECT VEHICLE SPEED IN A VIDEO

Answered: 1 week ago