2010 Lab-10

Elements of this lab...
   . arrays
   . pointers
   . functions
   . dynamic memory allocation
   . command-line arguments

Write your week-10 programs in your /2010/a/ folder.

Start
Log on to Odin server...
 $ cd 2010
 $ ./lab-start.sh
 $ cd a


expander.cpp ------------ Start with your resize.cpp program from Monday. The program will... Store n random numbers in an array at the request of the user. 1. To start, dynamically allocate your array to a size of 1. --------------------------------------------------------- 2. Get the user's number from the command-line. -------------------------------------------- 3. Store a random number in your array. ------------------------------------ If the user's request is greater than 1, call your resize() function. As you enlarge the array, insert random numbers instead of zeros. If the array is still too small to hold the user's request, continue to enlarge it over and over. (Use a loop) 4. Display the complete array just before expanding it each time. -------------------------------------------------------------- But, display it only if it is small and will not scroll the window. See the sample output. 5. Display the array's last value. ------------------------------- sample program run is here...
$ ./a.out 25000 Lab-10 array expander --------------------- 49 49 36 49 36 81 30 49 36 81 30 63 66 47 14 49 36 81 30 63 66 47 14 82 28 40 26 8 89 86 8 Final array has 25000 values stored. 15 array expansions were needed. Last array value is 11 at index 24999 Program complete. Thanks.
Run your expander program using valgrind. Here's how... $ valgrind --leak-check=full ./a.out 20 Your goal is no memory errors.
bigsmall.cpp ------------ Write a program that will dynamically allocate an array. 1. Get the size of the array from the command-line arguments. ---------------------------------------------------------- If the user did not enter a command-line argument, show a "Usage" statement and end the program. 2. Declare a int, short, long, float, or double array of the user's size. ---------------------------------------------------------------------- Create the array using dynamic memory allocation. 3. Populate the whole array with random values. -------------------------------------------- Small numbers will be ok. 4. Send the array to a function that will sort it in ascending sequence. --------------------------------------------------------------------- You should use the Bubblesort sorting algorithm we learned in class. 2010/7/mysort.cpp> The function will not display anything to the screen. Only sort. 5. Display the 3 smallest and the 3 largest values in the array. ------------------------------------------------------------- If user's request is less than 6, change it to 6. sample output...
$ ./a.out Usage: ./a.out <size of array>
$ ./a.out 200 Lab-10 extreme array values... 3 smallest values: 0.109 0.213 0.469 3 largest values: 97.373 98.829 99.246 program complete.