Pointers, Arrays, and Recursion 1

Original Course link: Pointers, Arrays, and Recursion Week 1, Read pointer 1 C pointer C语言指针详解,30分钟玩转C语言指针 (biancheng.net) C语言指针变量的定义和使用(精华)_54笨鸟 (54benniao.com) #include <stdio.h> #include <stdlib.h> void g(int x, int * y) { printf("In g, x = %d, *y = %d\n", x, *y); x++; *y = *y - x; y = &x; } void f(int * a, int b) { printf("In f, *a = %d, b = %d\n", *a, b); *a += b; b *= 2; // * multiply, not pointer identifier here; g(*a, &b); printf("Back in f, *a = %d, b = %d\n", *a, b); } int main(void) { int x = 3; int y = 4; f(&x, y); printf("In main: x = %d, y = %d\n", x, y); return EXIT_SUCCESS; } Output In f, *a = 3, b = 4 In g, x = 7, *y = 8 Back in f, *a = 7, b = 0 In main: x = 7, y = 4 ...

2023-12-10 · 2 min · Atom.X

Programming Testing

Week 3 statement coverage decision coverage path coverage Testing Asserts Black box testing https://www.javatpoint.com/black-box-testing automated tools for black box testing: Selenium: A widely-used open-source testing framework that allows testers to automate the testing of web browsers, making it a valuable tool for performing black box testing on web-based systems. LoadRunner: A performance testing tool that can be used for black box testing of non-functional requirements such as load, stress, and endurance. JMeter: An open-source tool that can be used for black box testing of non-functional requirements such as load, stress, and endurance. SilkTest: A tool that can be used for regression and functionality testing. IBM Rational Functional Tester (RFT): A tool that can be used for creating black box test scripts with a test recorder. GDB manual Debugging with GDB (sourceware.org) ...

2023-12-3 · 2 min · Atom.X

Retirement

Week 2, “retirement savings” calculator Assignment 07_retirement Start Age= 327 months (27 years, 3 months); Savings: 21,345 working 489 months (40 years, 9 months); Savings: 1529340.57 work until Age (68 years, 0 months(-1)) retired 384 months (32 years); Savings: 300234.07 retired until Age(99 years, 11 months(-1), mark every month from 0 - 11 ); Died Age at (100 years, so only compute the 11th month, and don’t compute the last 12th month) ...

2023-11-30 · 4 min · Atom.X

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