Description
Step One
First, create an empty directory for lab2. There is no starter code for this lab.
In a new .cpp file in the directory, write a function that will read an integer array from the input (cin) and return it. The array input will be all
on one line. The first number on the line will be the number of elements of the array. The second number on the line will be the first element
of the array. The third number will be the second element, etc. For instance,
5 19 8 26 12 9
represents the 5-element array [19, 8, 26, 12, 9].
Your code should handle a 0-element array correctly. It should also handle large arrays correctly; there is no limit on the size of the array that
you may expect on the input.
Step Two
Write a separate function that takes an array of integers as an argument and prints it out. It should print it out in the following format:
[19, 8, 26, 12, 9]
That is, it should surround the elements with square brackets, and print a comma and a space after each element except the last. Your function
should handle length 0 (empty) and length 1 arrays correctly. Length 0 arrays should print as
[]
(no space between the brackets) and length 1 arrays should print as, for example:
[26]
In your main(), call your two functions, first reading an array from the input and then printing it. Be sure to delete any memory that you
allocated before the end of the program.
Now, test your program by running it and typing input arrays. (Each time you run the program, you only need to read in one array. Test
multiple different arrays by running the program more than once.)
Step Three
Write a third function which takes an integer array argument and sorts the array from largest to smallest. You may use any sorting algorithm
you like, but you may not use library functions. You must code the sort yourself.
Now, in your main(), after printing the input array, sort it and print it again. Test it with a few examples.
When you are satisfied that your code works, call over the TA and demonstrate it to get your mark for the lab.