Rectangle

Week 2, Rectangle Assignment 06_rect reference: struct and typedef for rectangles Intersection of Two Rectangles Completed Rectangle Intersection Code #include <stdio.h> #include <stdlib.h> //I've provided "min" and "max" functions in //case they are useful to you int min (int a, int b) { if (a < b) { return a; } return b; } int max (int a, int b) { if (a > b) { return a; } return b; } //Declare your rectangle structure here! struct rect {int x; int y; int width; int height;}; typedef struct rect rectangle; rectangle canonicalize(rectangle r) { //WRITE THIS FUNCTION if (r.width < 0) { r.x = r.x + r.width; r.width = -r.width; } if (r.height < 0) { r.y = r.y + r.height; r.height = -r.height; } return r; } rectangle intersection(rectangle r1, rectangle r2) { //the input value r of function intersection, which is the output value from function canonicalize above, please correct the code. r1 = canonicalize(r1); r2 = canonicalize(r2); rectangle r; // Find the max of the x and y values for the intersection r.x = max(r1.x, r2.x); r.y = max(r1.y, r2.y); // Find the min of the x+width and y+height values for the intersection int r1_x_end = r1.x + r1.width; int r2_x_end = r2.x + r2.width; int r1_y_end = r1.y + r1.height; int r2_y_end = r2.y + r2.height; int r_x_end = min(r1_x_end, r2_x_end); int r_y_end = min(r1_y_end, r2_y_end); r.width = r_x_end - r.x; r.height = r_y_end - r.y; // If there is no intersection, set width and height to 0 if (r.width < 0 || r.height < 0) { r.width = 0; r.height = 0; } // Return the intersection rectangle return r; } //You should not need to modify any code below this line void printRectangle(rectangle r) { r = canonicalize(r); if (r.width == 0 && r.height == 0) { printf("<empty>\n"); } else { printf("(%d,%d) to (%d,%d)\n", r.x, r.y, r.x + r.width, r.y + r.height); } } int main (void) { rectangle r1; rectangle r2; rectangle r3; rectangle r4; r1.x = 2; r1.y = 3; r1.width = 5; r1.height = 6; printf("r1 is "); printRectangle(r1); r2.x = 4; r2.y = 5; r2.width = -5; r2.height = -7; printf("r2 is "); printRectangle(r2); r3.x = -2; r3.y = 7; r3.width = 7; r3.height = -10; printf("r3 is "); printRectangle(r3); r4.x = 0; r4.y = 7; r4.width = -4; r4.height = 2; printf("r4 is "); printRectangle(r4); //test intersection of r2, r3, r4 with r1 rectangle i = intersection(r1,r1); printf("intersection(r1,r1): "); printRectangle(i); i = intersection(r1,r2); printf("intersection(r1,r2): "); printRectangle(i); i = intersection(r1,r3); printf("intersection(r1,r3): "); printRectangle(i); i = intersection(r1,r4); printf("intersection(r1,r4): "); printRectangle(i); //test everything with r2 i = intersection(r2,r1); printf("intersection(r2,r1): "); printRectangle(i); i = intersection(r2,r2); printf("intersection(r2,r2): "); printRectangle(i); i = intersection(r2,r3); printf("intersection(r2,r3): "); printRectangle(i); i = intersection(r2,r4); printf("intersection(r2,r4): "); printRectangle(i); //test everything with r3 i = intersection(r3,r1); printf("intersection(r3,r1): "); printRectangle(i); i = intersection(r3,r2); printf("intersection(r3,r2): "); printRectangle(i); i = intersection(r3,r3); printf("intersection(r3,r3): "); printRectangle(i); i = intersection(r3,r4); printf("intersection(r3,r4): "); printRectangle(i); //test everything with r4 i = intersection(r4,r1); printf("intersection(r4,r1): "); printRectangle(i); i = intersection(r4,r2); printf("intersection(r4,r2): "); printRectangle(i); i = intersection(r4,r3); printf("intersection(r4,r3): "); printRectangle(i); i = intersection(r4,r4); printf("intersection(r4,r4): "); printRectangle(i); return EXIT_SUCCESS; } the hard point is how to transfer canonicalize value to intersection. ...

2023-11-26 · 5 min · Atom.X

Squares

Week 2, Squares Assignment 05_squares // template/skeleton with empty implementations #include <stdio.h> #include <stdlib.h> /* - Determines if coord is in range between - offset (INCLUSIVE) and offset + size (EXCLUSIVE) */ int isInRange(int coord, int offset, int size) { // if coord is in range, return 1 // else, return 0 return 0; } /* - Determines if coord is at border of offset or - offset + size */ int isAtBorder(int coord, int offset, int size) { // if coord is equal to offest or offset + size // return 1, else return 0 return 0; } void squares(int size1, int x_offset, int y_offset, int size2) { //compute the max of size1 and (x_offset + size2). Call this w //compute the max of size1 and (y_offset + size2). Call this h //count from 0 to h. Call the number you count with y //count from 0 to w. Call the number you count with x //check if EITHER // ((x is between x_offset and x_offset +size2) AND // y is equal to either y_offset OR y_offset + size2 - 1 ) // OR // ((y is between y_offset and y_offset + size2) AND // x is equal to either x_offset OR x_offset + size2 -1) // if so, print a * //if not, // check if EITHER // x is less than size1 AND (y is either 0 or size1-1) // OR // y is less than size1 AND (x is either 0 or size1-1) //if so, print a # //else print a space //when you finish counting x from 0 to w, //print a newline } // complete implementation without comments. #include <stdio.h> #include <stdlib.h> int isInRange(int coord, int offset, int size) { if (coord >= offset && coord < offset + size) { return 1; } else { return 0; } } int isAtBorder(int coord, int offset, int size) { if(coord == offset || coord == offset + size) { return 1; } else { return 0; } } int max(int a, int b) { return (a > b) ? a : b; } void squares(int size1, int x_offset, int y_offset, int size2) { int w = max(size1, x_offset + size2); int h = max(size1, y_offset + size2); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if ((isInRange(x, x_offset, size2) && (y == y_offset || y == y_offset + size2 - 1)) || (isInRange(y, y_offset, size2) && (x == x_offset || x == x_offset + size2 - 1))) { printf("*"); } else if ((x < size1 && (y == 0 || y == size1 - 1)) || (y < size1 && (x == 0 || x == size1 - 1))) { printf("#"); } else { printf(" "); } } printf("\n"); } } int main() { squares(5, 2, 3, 4); return 0; } the other way to write code ...

2023-11-24 · 3 min · Atom.X

CountStars

Week 1, CountStars print stars Step 3: Repetitions Stars Example #include <stdio.h> int printTriangle(int size) { //start with starCount being 0 int starCount = 0; //count from 0 (inclusive) to size (exclusive), for each number i that you count for (int i=0; i< size; i++) { //count from 0 (inclusive) to i (inclusive), for each number j that you count for (int j=0; j<=i; j++) { //print a "*" printf("*"); //increment starCount starCount++; } //when you finish counting on j, //print a newline ("\\n") printf("\\n"); } //when you finish counting on i, //your answer is starCount return starCount; } int main(void) { int numStars; printf("Here is a triangle with height 4\n"); numStars = printTriangle(4); printf("That triangle had %d total stars\n", numStars); //now print "Here is a triangle with height 7\n" //then call printTriangle, passing in 7, and assign the result to numStars //finally, print "That triangle had %d total stars\n", such that the %d //prints the value of numStars return 0; } Tips: ...

2023-11-21 · 2 min · Atom.X

Writing, Running, and Fixing Code in C

Original Course link: Writing, Running, and Fixing Code in C Assignment GitHub - mvirgo/c-programming: Code worked on for Duke’s Intro to Programming in C course UNIX basic cat // output file to terminal emacs // open file in editor Emacs (be attention for the command, use lowercase, not uppercase) close a terminal tab, ctrl + shift + w close the entire terminal including all tabs, ctrl + shift + q githttp://git-scm.com/book/en/v2/ ...

2023-11-21 · 2 min · Atom.X

Overflow

Week 3 How to calculate binary, decimal, hexadecimal 3 parts of fields compound a float number. the most significant bit (MSB) 最高有效位 exponent 指数 mantissa 尾数 IEEE floating point Standard unsigned integer 无符号整数 that can only hold zero and positive numbers. For an n-bit integer, the range of values it can represent is[ 0 , 2^n ]. signed integer, For an n-bit integer, the range of values it can represent is[ -2^(n-1) , 2^(n-1) - 1]. ...

2023-11-18 · 5 min · Atom.X