Professional Documents
Culture Documents
Fall 2017
Quiz
Name: ___________________________________________
Write array functions that carry out the following tasks for an array of integers:
1. Shift all elements by one to the right and move the last element into the first
position
Example 1 Example 2
5 36 12 27 2 32 29 28 35 72 31 77 68 95 72 87 74 68 85 35
72 5 36 12 27 2 32 29 28 35 35 31 77 68 95 72 87 74 68 85
#include <iostream>
#include <ctime>
}
arr[0] = temp;
int main()
int randoms[10];
srand(time(0));
rotate_right(randoms, 10);
return 0;
}
2. Replace all even elements with 0
Example 1 Example 2
63 31 37 58 72 60 77 52 89 82
63 31 37 0 0 0 77 0 89 0
3. Replace each element except the first and last by the larger of its two
neighbors
Example 1 Example 2
77 16 23 65 68 54 92 81 69 80 62 82 11 33 8 3 18 32 48 78
77 77 77 77 77 92 92 92 92 80 62 62 62 62 62 62 62 62 78 78
4. Remove the middle element if the array length is odd, or the middle two
elements if the length is even
Example 1 Example 2
6 24 18 32 63 64 50 14 70 15 3 18 3 50 69 29 94 72 49 25 36 75 16
6 24 18 32 63 50 14 70 15 3 18 3 50 69 29 49 25 36 75 16
5. Move all even elements to the front, otherwise preserving the order of the
elements
Example 1 Example 2
27 13 36 55 20 96 70 22 46 86 76 98 87 58 51 72 43 84 23 49
36 20 96 70 22 46 86 27 13 55 76 98 58 72 84 87 51 43 23 49
Example 1