You are on page 1of 3

CS 100 – Computational Problem Solving

Fall 2017
Quiz

Time: 10 min Total Marks: 10

Name: ___________________________________________

Roll Number: _______________________ Section: __________

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>

using namespace std;

void rotate_right(int arr[], int size)

int last = size - 1;

int temp = arr[last];

for (int i = last; i > 0; i--)

arr[i] = arr[i - 1];

}
arr[0] = temp;

int main()

int randoms[10];

srand(time(0));

for (int i = 0; i < 10; i++)

randoms[i] = (rand() % 100 + 1);

cout << randoms[i] << " ";

cout << endl;

rotate_right(randoms, 10);

for (int i = 0; i < 10; i++)

cout << randoms[i] << " ";

cout << endl;

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

6. Return the second-largest element in the array

Example 1

44 33 46 33 60 65 52 57 76 31 Second largest number is 65

You might also like