Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function shallow_copy 1. Write a function shallow_copy(struct Double_Array *) that returns a copy of the Double_Array struct that is passed in (has a
Write a function shallow_copy
1. Write a function shallow_copy(struct Double_Array *) that returns a copy of the Double_Array struct that is passed in (has a different pointer value) but holds the same content. Any change to the interior of either the new or the old struct should be reflected in the other automatically, even though the pointers to the two structs are different. 2. Write a function deep_copy(struct Double_Array *) that returns a copy of the Double_Array struct that is passed in (has a different pointer value) but holds the same content. Now, however, any change to the interior of one struct should not be reflected in the other. 3. Write a function print_struct(struct Double_Array *, char *) that prints out a header string on the first line (the string that was passed in as the second parameter) "struct address = %p" on the second line "row_size = %d, col_size = %d" on the third line "array address = %p, with contents:" on the fourth line leave the fifth line blank print the array from the sixth line on (printed in the same way you did in print_array from Q1) after the array is printed, leave two blank lines 4. Create a main() called a2_42.c that performs the following: Q2a Print the following header Question 2a Declare a variable called a1 that holds a Double_Array struct that contains a 6x9 array (as in Q1) note: a1 should hold a struct pointer, not a struct itself Initialize al to random values between 0.0 and 10.0 Print the address of a1 using the printf command "the address of a1 is %p" Print the structure pointed to by a1 with the header "The structure pointed to by al is: Declare a variable called a2 that can hold the same struct pointer as a1, and set a2 to al Print the address of a2 using the printf command "the address of al is %p" Print the structure pointed to by a2 with the header "The structure pointed to by a2 is:" Create a shallow copy of a1 and store it in a variable called a_shallow Print the address of a shallow using the printf command "the address of a shallow is %p" Print the structure pointed to by a2 with the header "The structure pointed to by a shallow is:" Create a deep copy of a1 and store it in a variable called a_deep Print the address of a_deep using the printf command "the address of a_deep is %p" Print the structure pointed to by a2 with the header "The structure pointed to by a_deep is:" Print 3 blank lines Q2b Print the following header Question 2b In a1, set [0][1] to 100.0 note: the above instruction, for the purpose of this assignment, is shorthand for the following: set the element at the oth row and 1st column of the array in the struct held by al to the value 100.0 In a2, set [1][2] to 200.0 In a shallow, set [2][3] to 300.0 In a_deep, set [3][4] to 400.0 Print a1, a2, a_shallow and a deep Print 3 blank lines Explain the results observed in the readme file. Label your answer as Q2b Q2c Print the following header Question 2c Declare a variable called b1 that holds a Double_Array struct that contains a 6 x 9 array (as in Q2a) Initialize b1 to random values between 10.0 and 20.0 Set a2-> array = 11 -> array Print al, a2, a_shallow, a_deep, and b1 In a1, set (0][1] to 5000.0 In a2, set [1][2] to 6000.0 In a_shallow, set [2](3) to 700.0 In a deep, set (3][4] to 8000.0 In b1, set [4][5] to 9000.0 Print al, a2, a_shallow, a_deep, and b1 Explain the results observed in the readme file. Label your answer as Q2c Q2d Free all malloc'd pointers. Make sure you do not have a memory leak. In the readme file under Q2d explain which pointers need to be freed, in what order, and why the order is important (be specific). struct Double_Array 2 /* initialize the variables /* array with upperbound of double *karr; int rowsize; int colsize; da; 1. Write a function shallow_copy(struct Double_Array *) that returns a copy of the Double_Array struct that is passed in (has a different pointer value) but holds the same content. Any change to the interior of either the new or the old struct should be reflected in the other automatically, even though the pointers to the two structs are different. 2. Write a function deep_copy(struct Double_Array *) that returns a copy of the Double_Array struct that is passed in (has a different pointer value) but holds the same content. Now, however, any change to the interior of one struct should not be reflected in the other. 3. Write a function print_struct(struct Double_Array *, char *) that prints out a header string on the first line (the string that was passed in as the second parameter) "struct address = %p" on the second line "row_size = %d, col_size = %d" on the third line "array address = %p, with contents:" on the fourth line leave the fifth line blank print the array from the sixth line on (printed in the same way you did in print_array from Q1) after the array is printed, leave two blank lines 4. Create a main() called a2_42.c that performs the following: Q2a Print the following header Question 2a Declare a variable called a1 that holds a Double_Array struct that contains a 6x9 array (as in Q1) note: a1 should hold a struct pointer, not a struct itself Initialize al to random values between 0.0 and 10.0 Print the address of a1 using the printf command "the address of a1 is %p" Print the structure pointed to by a1 with the header "The structure pointed to by al is: Declare a variable called a2 that can hold the same struct pointer as a1, and set a2 to al Print the address of a2 using the printf command "the address of al is %p" Print the structure pointed to by a2 with the header "The structure pointed to by a2 is:" Create a shallow copy of a1 and store it in a variable called a_shallow Print the address of a shallow using the printf command "the address of a shallow is %p" Print the structure pointed to by a2 with the header "The structure pointed to by a shallow is:" Create a deep copy of a1 and store it in a variable called a_deep Print the address of a_deep using the printf command "the address of a_deep is %p" Print the structure pointed to by a2 with the header "The structure pointed to by a_deep is:" Print 3 blank lines Q2b Print the following header Question 2b In a1, set [0][1] to 100.0 note: the above instruction, for the purpose of this assignment, is shorthand for the following: set the element at the oth row and 1st column of the array in the struct held by al to the value 100.0 In a2, set [1][2] to 200.0 In a shallow, set [2][3] to 300.0 In a_deep, set [3][4] to 400.0 Print a1, a2, a_shallow and a deep Print 3 blank lines Explain the results observed in the readme file. Label your answer as Q2b Q2c Print the following header Question 2c Declare a variable called b1 that holds a Double_Array struct that contains a 6 x 9 array (as in Q2a) Initialize b1 to random values between 10.0 and 20.0 Set a2-> array = 11 -> array Print al, a2, a_shallow, a_deep, and b1 In a1, set (0][1] to 5000.0 In a2, set [1][2] to 6000.0 In a_shallow, set [2](3) to 700.0 In a deep, set (3][4] to 8000.0 In b1, set [4][5] to 9000.0 Print al, a2, a_shallow, a_deep, and b1 Explain the results observed in the readme file. Label your answer as Q2c Q2d Free all malloc'd pointers. Make sure you do not have a memory leak. In the readme file under Q2d explain which pointers need to be freed, in what order, and why the order is important (be specific). struct Double_Array 2 /* initialize the variables /* array with upperbound of double *karr; int rowsize; int colsize; daStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started