- Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array and the size of the array. The function creates a copy of the received array and stores the elements in reverse order. Return a pointer to the new array. b) expand: This function accepts a pointer to an array (the original array), a pointer to the reversed array, and the size of the array as its parameters. The function should create a new array that is twice the size of the argument arrays. The function should copy the contents of the original array to the new array in the first half and the reversed array in the second half. The function should then return a pointer to the new array. c) shift: This function accepts a pointer to an array and the size of the array. The function should create a new array that is two elements larger than the argument array. Store -1 in the first two elements of the new array. The rest of the elements will be shifted by two to the right; first element of the argument array will be third element in the new array, second element of the argument array will be fourth element in the new array, and so on. The function should return a pointer to the new array. d) mode: This function accepts a pointer to an array and its size as parameters and finds the mode in that array. The mode is the element that occurs the most. If the array has no mode, the function should return -1.
#include <iostream>
using namespace std;
int *reverse(int *arr, int size) {
int *result = new int[size];
for(int i=0; i<size; i++) {
result[size-i-1] = arr[i];
}
return result;
}
int *expand(int *arr, int size) {
int *result = new int[2*size];
for(int i=0; i<size; i++) {
result[2*size-i-1] = arr[i];
result[i] = arr[i];
}
return result;
}
int *shift(int *arr, int size) {
int *result = new int[2 + size];
result[0] = -1;
result[1] = -1;
for(int i=0; i<size; i++) {
result[2 + i] = arr[i];
}
return result;
}
int mode(int *arr, int n) {
int maxCount = 1;
int maxCountElem = -1;
for(int i=0; i<n; i++) {
int elem = arr[i];
int count = 0;
for(int j=0; j<n; j++) {
if(elem == arr[j]) {
count++;
}
}
if(count > maxCount) {
maxCount = count;
maxCountElem = elem;
}
}
return maxCountElem;
}
void printArr(int *arr, int n) {
for(int i=0; i<n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int *arr;
int n;
cout << "Enter size: ";
cin >> n;
while(n <= 0) {
cout << "Enter size: ";
cin >> n;
}
arr = new int[n];
int i = 0;
while(i < n) {
cout << "Enter value: ";
cin >> arr[i];
if(arr[i] >= 0) {
i++;
} else {
cout << "Invalid value." << endl;
}
}
cout << "Original array: ";
printArr(arr, n);
cout << "After reverse: ";
int *revArr = reverse(arr, n);
printArr(arr, n);
cout << "After expand: ";
int *expArr = expand(revArr, n);
printArr(expArr, n*2);
cout << "After shift: ";
int *shiftArr = shift(arr, n);
printArr(shiftArr, n + 2);
cout << "Mode: " << mode(arr, n) << endl;
}

Please upvote, as i have given the exact answer as asked in
question. Still in case of any concerns in code, let me know in
comments. Thanks!
- Write a program that performs several operations on an array of positive ints. The program...
Can someone help me understand this step by step (C++)
Array Shifter and Array Reversal Write a function that accepts an array of doubles and the array's size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array...
C++ Array Expander and Element Shifter Thank you so much to anybody that can help! Here are our two homework prompts: PART A: Array Expander Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0....
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named...
C++ Write a function called reverseArray(char[] , int) that accepts in an array of chars and the size of the array.as parameters. The function should then create a new array that is the same size as the original array. The function should copy the elements from the first array into the second array in reverse order. So if the initial array is {'a' , 'b', 'c'} then the new array will be {'c', 'b', 'a'} Then in your main(), create...
/* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...
IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...
(C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...
3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...
C++. Write a program that copies the contents of one array into another array but in reverse order using pointers. - in main() - define 2 arrays of type int, 10 elements each - initialize one array with numbers 1 through 10 - initialize all elements of the second array to 0 - call function reverseCopy with both arrays as arguments - display the values of array number 2 (reversed order) - reverseCopy...