Question

- Write a program that performs several operations on an array of positive ints. The program...

- 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #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!

Add a comment
Know the answer?
Add Answer to:
- Write a program that performs several operations on an array of positive ints. The program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help me understand this step by step (C++) Array Shifter and Array Reversal Write...

    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++ 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...

    (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

    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...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    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...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* 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...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (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...

    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...

    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...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Write a program that dynamically allocates an integer array of size of 20 to hold a...

    Write a program that dynamically allocates an integer array of size of 20 to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to • a function that calculates the average score, the minimal score, and the maximal score in the array, and returns these scores to the main function (hint: use pass by reference, I gave an example in the class) • a function that searches a specific number. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT