Write a program that swaps the elements at adjacent indexes: the elements 0 and 1 are swapped, elements 2 and 3 are swapped, and so on. If the array has an odd length, the final element should be left unmodified. For example { 10, 20, 30, 40, 50 } should become { 20, 10, 40, 30, 50 } after a call to your method. Problem based on self-check problem 17 of chapter 7 in text.
Your program will run three test cases:
Array Description | Input | Output |
---|---|---|
an empty array | { } | { } |
an odd length array | {2, 1, 4, 3, 5} | {1, 2, 3, 4, 5} |
an even length array | {2, 1, 4, 3, 6, 5} | {1, 2, 3, 4, 5, 6} |
Your program will include a method named swapPairs that accepts an array of integers and swaps the elements at adjacent indexes.
Print the result for each on the console using the exact output specified by ArrayManipQuizOutput.txt. Your program must EXACTLY match the designated output. Make sure you use the output comparision tool!
Name your class "<your name>_ArrayManipQuiz" and submit the source code to me via DropItToMe
Follow class programming standards and formatting/indentation rules. Make sure you comment your code!
The quiz is worth 35 points.
This is pretty simple...you get 100% if your output exactly matches the expected output...and...0% if it has ANY differences.
Make sure you get all the spelling, capitalization, punctuation, spacing, etc. correct! Hint: copy-n-paste is a wonderful thing! :)