Write a program to swap an array element (array A) with the element at twice its
ID: 3767047 • Letter: W
Question
Write a program to swap an array element (array A) with the element at twice its index position. If twice the index position does not exist, no swapping will happen. Store this in a new array (array B) and store the sum of the original and new array in another array (array C). Find the minimum of the left half of the final array C. Input array A size and elements. Please submit output screenshots with both odd and even number array sizes.
For example,
Say, size = 10 and array A elements input are 4 5 8 12 34 56 1 0 22 7
A r r a y A = 4 5 8 1 2 3 4 5 6 1 0 2 2 7
Array B
Swap 0 and (2 * 0 = 0) indexed elements
4 5 8 1 2 3 4 5 6 1 0 2 2 7
Swap 1 and 2 indexed elements
4 8 5 1 2 3 4 5 6 1 0 2 2 7
Swap 2 and 4 indexed elements
4 8 34 1 2 55 6 1 0 2 2 7
Swap 3 and 6 indexed elements
4 8 3 4 15 5 6 12 0 2 2 7
Swap 4 and 8 indexed elements
4 8 3 4 1 2 2 5 5 6 0 57
5 * 2 = 10 which exceeds the last index (9) of the array, so swapping stops.
Array C= Array A + Array B = 8 13 42 13 56 61 6 0 27 14
Left half of array C = 8 13 42 13 56
M i n = 8
If odd number of elements in array (size is an odd number then left half= = (size+1)/2)
Explanation / Answer
PROGRAM:
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.