Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: initialize arrays in all three tasks by random values. Ask user which task

ID: 3699585 • Letter: N

Question

Note: initialize arrays in all three tasks by random values.
Ask user which task to execute and your code should keep running until user
say so.
1. Block swap algorithm for array rotation
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.
Rotation of the above array by 2 will make array.
In your main function,
• Declare array of size 7.
• Initialize this array with random values.
• Display this array
• Ask user for number of elements to rotate ‘d’.
• Call the user defined function named ‘rotate’
• Display the array again.
2. k largest elements in an array
Write C++ code for printing k largest elements in an array of size 50. Display the array
after initialization and display k largest values in that array.
• For example, if given array is [1, 23, 12, 9, 30, 2, 50] and you are asked for the
largest 3 elements i.e., k = 3 then your program should print 50, 30 and 23.
• Take input from a user for value k.
3. Sorting
• Take an array of size 10 in main function.
• Write sorting(int arr[]) function to sort your array in decreasing order.
• Display the array before and after calling sorting function in main function.

Explanation / Answer

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

void rotate(int a[], int d , int n) {

d = d % n;

for (int i = 0; i < d ; i++) {

int temp = a[n - 1];

for (int j = n - 1; j > 0; j--) {

a[j] = a[j - 1];

}

a[0] = temp;

}

}

void sorting(int b[], int N) {

//using bubblesort algorithm

int nIter = N - 1;

for (int i = 0; i < N; ++i) {

// size of the remaining unsorted array

for (int curIdx = 0; curIdx < N; ++curIdx) {

if ((curIdx + 1 < N) && b[curIdx] < b[curIdx + 1]) {

// swap

int tmp = b[curIdx];

b[curIdx] = b[curIdx + 1];

b[curIdx + 1] = tmp;

}

}

}

}

int main() {

int choice;

cout << "1. Block swap algorithm for array rotaion ";

cout << "2. k largest elements in an array ";

cout << "3.Sorting ";

char ans;

label:

cout << "Enter choice : ";

cin >> choice;

switch (choice) {

case 1: int ar[7];

srand((unsigned)time(0));

for (int i = 0; i < 7; i++) {

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

cout << ar[i] << endl;

}

for (int i = 0; i < 7; i++) {

cout << ar[i] << " ";

}

int d;

cout << " Enter d: ";

cin >> d;

rotate(ar, d, 7);

cout << endl;

for (int i = 0; i < 7; i++) {

cout << ar[i] << " ";

}

cout << " Do you want to continue(Y/N) ";

cin >> ans;

if (ans == 'Y' || ans == 'y') {

goto label;

}

break;

case 2: int arr[50];

srand((unsigned)time(0));

for (int i = 0; i < 50; i++) {

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

cout << arr[i] << endl;

}

for (int i = 0; i < 50; i++) {

cout << arr[i] << " ";

}

cout << endl;

int k;

cout << "Enter k: ";

cin >> k;

sorting(arr, 50); //arrange arr in decending order

cout << endl;

for (int i = 0; i < k; i++) { //take first k elements

cout << arr[i] << " ";

}

cout << " Do you want to continue(Y/N) ";

cin >> ans;

if (ans == 'Y' || ans == 'y') {

goto label;

}

break;

case 3: int asort[10];

srand((unsigned)time(0));

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

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

cout << asort[i] << endl;

}

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

cout << asort[i] << " ";

}

cout << endl;

sorting(asort, 10);

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

cout << asort[i] << " ";

}

cout << " Do you want to continue(Y/N) ";

cin >> ans;

if (ans == 'Y' || ans == 'y') {

goto label;

}

break;

default:

cout << "Wrong choice";

cout << " Do you want to continue(Y/N) ";

cin >> ans;

if (ans == 'Y' || ans == 'y') {

goto label;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote