Write a program that reads in 10 numbers and prints them out in the reverse order that they were input. Surprise, use an array!
Hint: You can use the array to store the numbers when you are reading them in and then access the array from the highest index to the lowest to output them in reverse order.
Hint: Start at boths ends of the array and interchange (or swap) the end elements. Then work your way to the center of the array, swapping as you go. When you've reached the middle of the array, you're finished. So, we're going to swap the first element with the last element, and then the second element with the second to last element, and then the third element with the third to last element, etc. This continues until we are at the middle of the array. So the loop, indexed by ``i'' starts at the beginning of the array and ends at the halfway mark.
The solution is to move all the numbers in position 4 and after, one position higher in the array. Thus, the number 20 is moved from position 10 to position 11, the number 18 is moved from position 9 to postion 10, the number 16 is moved from position 8 to position 9,..., and the number 8 is moved from position 4 to position 5. Now position 4 is vacant and can be used to insert the number 7, keeping the array in sorted order.
I have started a program for you that has an array of numbers declared and
initialized. It then asks the user to enter a new number to insert into the
list and the subscript of where the number is to be inserted in the list. In
the example above, the user would enter 7 and 4 to
indicate that the number 7 should be inserted in the array at
position 4. Finally, the program prints the new list with the
inserted number.
The program does not contain the code needed to do the insertion. Your
job is to complete the program by adding this code. Use the technique
illustrated above. Note that variable N tells you the number
of integers in the list (they have positions that run from 0
through N-1). Don't forget to increment N when
you insert the new number since the list is now one element longer.
When you write the code required to move numbers in the array elements following the insertion point one element to the right, be careful how you do it. In the example above, the 20 must be moved first, then the 18, then the 16, etc. If you try to move the 8 first, you will lose data.
The path to the parital program is: ~escobj/public/ex2.C