CSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists Exercises on Structs (Solutions) (a) Define a struct data type location with integer members row, column Define another struct data type pixeltype with members: color (0 for black and 1 for white) and position (a struct of type location as defined above). (b) A 2-D array A [ ] [ ] is of type pixeltype and represents a black and white image of size (n x m). Implement a function flipud (A) that uses the above struct definitions to receive the image and return it upside down. struct location int row, column; ; struct pixeltype int color; location position; ; const int N =...; const M =...; int n = N; int m = M; void flipud (pixeltype A [ ] [M], int n, int m) pixeltype temp; for (i = 0; i < n/2; i++) for (j = 0; j < m; j++) temp = A[i][j]; A[i][j] = A[n-i-1][j]; A[n-i-1][j] = temp; Declare a struct type fraction for a fractional number with numerator num and a denominator denom, both are integers. Write a function fadd (x,y,z) to receive two fractional numbers (x) and (y) and return the fractional variable z = x + y according to the rule: If x = a / b and y = c / d, then z = (ad + cb) / (bd) struct fraction int num, denom; ; void fadd (fraction x, fraction y, fraction & z) z.num = x.num * y.denom + y.num * x.denom; z.denom = x.denom * y.denom;
Declare a struct type item for an item with weight and total price, both are of type float. Write a function maxval (x,y,z) to receive two items (x) and (y) and return the more valuable item (z), i.e. the item with the higher price per weight ratio. struct item float weight, price; ; void maxval (item x, item y, item & z) if ((x.price / x.weight) >= (y.price / y.weight)) z = x else z = y; Declare a struct type for a point in 3-D space with coordinates (x,y,z). Write a function distance(a,b) that receives two points (a) and (b) in 3-D space and returns the distance between them. struct point float x, y, z; ; float distance (point a, point b) float dx, dy, dz; dx = a.x b.x; dy =a.y b.y; dz =a.z b.z; return sqrt (dx*dx + dy*dy + dz*dz) Declare a struct type for a complex number with a real part and an imaginary part, both of type double. Write a function cmult (a,b,c) to receive two complex variables (a) and (b) and return the complex variable c = a*b according to the rule: If a = x1 + j y1 and b = x2 + j y2 then c = (x1.x2 y1.y2) + j (x1.y2 + x2.y1) struct complex double x, y; ; void cmult (complex a, complex b, complex &c) double Rec, Imc; Rec = a.x * b.x a.y * b.y; Imc = a.x * b.y + b.x * a.y; c.x = Rec; c.y = Imc;
A color image is stored as a 2-D array A[ ][ ] of pixels, with N rows and M columns. Each pixel A[i][j] is represented as a struct with three integer values representing its color components (Red, Green, Blue). To convert the pixel color to grey-level, we take the average of the three color components (rounded to the nearest integer) as the brightness. Write a function to receive such array and return another array B[ ][ ] containing the brightness of each pixel in the image. Consider N and M to be global constants. Solution const int N =...; const int M =...; struct pixel int red, green, blue; ; void Brightness (pixel A[ ][M], int B[ ][M]) int i, j, b; pixel p; for (i = 0; i < N; i++) for (j = 0; j < M; j++) p = A[i][j]; b = (p.red + p.green + p.blue)/3.0 + 0.5; // or b = floor((p.red + p.green + p.blue)/3.0 + 0.5); B[i][j] = b; Question 1 (15 points) A color image is stored as a 2-D array A[ ][ ] of pixels of N rows and M columns. Each pixel is represented as a struct with three integer values representing its color components (Red, Green, Blue). A sub-image is a block with the first H rows and first W columns, where H N and W M. Write a function to receive the image and the two integers H, W and return a pointer to a 1-D dynamic array containing the Green component of the sub-image defined by H and W. Consider N and M to be global constants. Declare a struct type item for an item with weight and total price, both are of type float. Consider the value of an item to be the ratio of price to weight. Write a recursive function to receive: a 1-D array of items, a starting index (s) an end index (e) a number (V) of type float a number (W) of type float The function should return the number of valuable items. We will define a valuable item as that having a weight less than (W) and a value greater than (V).
Declare a struct type item for an item with weight and total price, both are of type float. Consider the value of an item to be the ratio of price to weight. Write a function maxval to receive a 1-D array of items of size (N) and a number (k) of type float and return the item with the value nearest to (k).
Exercises on Dynamic Lists (Solutions) Assume the following declarations: struct node int info; node *next; node *p; Draw a diagram representing the nodes and pointers generated by the following code: node *h = NULL; int n = 1357; do p = new node; p-> info = n % 10; p -> next = h; h = p; n = n/10; while (n > 0); Final Result: h 1 3 5 7 Assume that the sequence of integers (17, 20, 50, 90) is already stored in a simple linked list of the structure given above in the order given ( first node contains 17). Trace the following: void What ( node *head ) node *p, *q, *r ; p = head ; q = NULL ; while (p!= NULL) r = q ; q = p ; p = p->ptr ; q->ptr = r ; head = q ; What will this function do if used on the above list? Left as an exercise. Assume the following declarations: struct node char info; node *next; node *p; node *h = NULL; node *c = h; Draw a diagram representing the nodes and pointers generated by the following code: string s = DATA LIST ; for (int i = 0; i < s.length( ); i++) p = new node; p-> info = s.at(i); p -> next = NULL; if (c == NULL) h = p; c = p; else c -> next = p; c = c -> next;
Solution The given code will insert the characters of the string s into a linked list of nodes in the same order, i.e., head BAT CAT DOG Given the above list and assuming the following declaration: struct node string info; node *ptr; Trace the following function to show the list after the function call: void What ( node *head ) node *p, *q, *r ; p = head ; q = NULL ; while (p!= NULL) r = q ; q = p ; p = p->ptr ; q->ptr = r ; head = q ; What is the objective of this function?