Page 1 of 1

3) Use the starter code and implement the three functions AllocateArray, SortArray, and DeallocateArray, following hints

Posted: Sat May 14, 2022 2:28 pm
by answerhappygod
3) Use the starter code and implement the three functions AllocateArray, SortArray, and DeallocateArray, following hints in the starter code.3A) you may need to add #include<> statements. This is intentional and you should get in the practice of using google or man to figure out which header to include for a function.
4) modify the main() function to call the three functions and also use gettimeofday() to time how long each takes. Also, study the main() function and figure out what it is doing.
5) compile and run your program. (Of course, you should be compiling and testing frequently before now.) Look at the output. What patterns do you see?
6) compile with "-O2" optimizations. How do the timings compare with the runs in (5)? How does the output compare with my sample output? (My output had optimizations)
include <stdio.h> int *AllocateArray( int N) /* Allocate an array with Nintegers. * The value of each element of the array should be a * random number between @ and 10N. * Hint: use the rand() function and a modulo operator. } void SortArray(int *A, int N) { /* Hint: pull your code from 2B */ } void DeallocateArray(int *A) } int main() int sizes [8] { 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000 }: For fun trt sizes: 111 - ( 1890, 2003, 4000800016000, 32000, 54309125000 256000 512000 1024000 for (int i = 0i < 8 ; i++) { double alloc_time = 0, sort_time = 0, dealloc_time=0. struct timeval startTime; struct timeval endTime:

void DeallocateArray( int *A) int main() int sizes[8] = { 1000, 2000, 4000, 8000, 16880, 32000, 64000, 128000); For fun: Int sizes (11) -{ 1000, 2000, 4000, 800816600, 32008, 64000, 128000, 256000, 512000, 1024000 */ for (int i = 0; i <s; i++) - double alloc_time = 0, sort_time=0., dealloc_time -0.: struct timeval start Time: struct timeval endTime; Call the three functions in a sequence Also gettineofday calls surrounding mach function and set the corresponding variable (alloctine, sort_tine, dealloctine printf("Timings for array of stze din sizes[0]); printf(" Time for allocation is g. time per element - g", alloc_time, alloc_time/sizes[i); printf(" Time for sort time is gi tine per element sort_time, sort_tine/sizes[0]); printfiltune for deallocation is , dealloc_time) TIECOT

S Timings for array of size 1000 Time for allocation is 2e-65, time per element = 2e-08 Time for sort_time is 0.006308, time per element 3.08e-07 Time for deallocation is 7e-06 Timings for array of size 2000 Time for allocation is 1.5e-05, time per element = 7.5e-09 Time for sort_time is 0.001198, time per element = 5.99-07 Time for deallocation is © Timings for array of size 4000 Time for allocation is 2.72-05, time per element 6.75e-09 Time for sort_time is 0.00467, time per element = 1.1675e-06 Time for deallocation is 42-06 Timings for array of size 8000 Time for allocation is 7.8e-05, time per element = 9.75e-09 Time for sort_time is 0.018147, time per element = 2.26838e-06 Time for deallocation is Timings for array of size 16000 Time for allocation is 0.000112, time per element = 7e-09 Time for sort_time is 0.071558, time per element = 4.47237e-06 Time for deallocation is 4e-06 Timings for array of size 32000 Time for allocation is 0.00025, time per element = 7.8125e-09 Time for sort_time is 0.298758, time per element = 9.33619-06 Time for deallocation is 4e-06 Timings for array of size 64000 Time for allocation is 0.000464, time per element 7.25e-09 Time for sort time is 1.25392, time per element = 1.95925e-05 Time for deallocation is 3e-06 Timings for array of size 128000 Time for allocation is 0.000869, time per element = 6.78906e-09 Time for sort time is 5.12276, time per element = 4.00216-05 Time for deallocation is 3e-06