Question

In: Computer Science

I have to modify the following code to: 1. Use the unique algorithm to reduce the...

I have to modify the following code to:

1. Use the unique algorithm to reduce the array to unique values

2. Use the copy algorithm to display the unique results.

#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

int main() {

    //creating an array of 20 integers

    int array[20];

    //creating an empty vector

    vector<int> vec;

    //input from end user to get 20 ints and a for loop to interate through 20

    cout << "Enter 20 integers:" << endl;

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

        cin >> array[i];

    }
    //sorting them

    sort(array, array + 20);

    //using unique_copy algorithm and a back_inserter, copying and adding each

    //unique value from the array to vector

    unique_copy(array, array + 20, back_inserter(vec));

    //displaying the unique values.

    cout << "The unique values are: " << endl;

    for (int i = 0; i < vec.size(); i++) {

        cout << vec[i] << " ";

    }

    cout << endl;

    return 0;

Solutions

Expert Solution

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main() {

        //creating an array of 20 integers
        int array[20];

        //creating an empty vector
        vector<int> vec;

        //input from end user to get 20 ints and a for loop to interate through 20
        cout << "Enter 20 integers:" << endl;

        for (int i = 0; i < 20; i++) {
                cin >> array[i];
        }
        
        //sorting them
        sort(array, array + 20);
        
        // use unique algorithm to reduce array to unique value
        auto it = unique(array,array+20);
        
        // use copy algorithm
        copy(array,it,back_inserter(vec));
        
        // alternatively you can also do
        // copy(array,unique(array,array+20);,back_inserter(vec));

        //displaying the unique values.
        cout << "The unique values are: " << endl;
        for (int i = 0; i < vec.size(); i++) {
                cout << vec[i] << " ";
        }
        cout << endl;
        return 0;
}

Code screenshot:

Code output screenshot:

===============

There are two ways to do:

1)
   auto it = unique(array,array+20);
   copy(array,it,back_inserter(vec));
  

2)
   copy(array,unique(array,array+20);,back_inserter(vec));

YOu can use any one.

Code along with it's output and screenshot have been added. Please refer to them

==================

For any query comment.


Related Solutions

1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values. 2. Modify the Exercise 1 above to use the unique_copy algorith. The unique values should be inserted into a vector that's initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.
Hi, I have this code so far and need to modify it so that the output...
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 = For example if I input...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops to do the same thing this code does. Be sure that you understand how the code works and how the pointer arithmetic relates to the array expression form. Provide liberal comments to explain what your pointer arithmetic is computing. #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { int arg_count = 0; for (arg_count = 0; arg_count < argc; arg_count++) printf("%s\n", argv[arg_count]); }
Modify the following java code, utilizing a loop mechanism to enable the user to use the...
Modify the following java code, utilizing a loop mechanism to enable the user to use the calculator more than once. The program does the following:    It prompts the user to enter 2 numbers.    It prompts the user to choose an operation to perform on those numbers:    Operation 1: Addition.    Operation 2: Subtraction.    Operation 3: Multiplication.    Operation 4: Division.    It outputs the result of the operation.    It asks the user if they want...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the selection to the web page, the selection is also placed in the browser’s local storage. Use ‘select’ as the local-storage key. The value will be the name of the category that was selected or the empty string is no selection was made. (d) Add a button called ‘retrieve’. When it is clicked the local storage is read and the prior selection is shown on...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item on 1st column, price on 2nd) Stewing beef,15.45 Ground beef,11.29 Pork chops,11.72 Chicken,7.29 Bacon,7.12 Wieners,4.33 Canned salmon,5.68 Homogenized milk,5.79 Partly skimmed milk,5.20 Butter,4.99 Processed cheese slices,2.53 Evaporated milk,1.89 Eggs,3.11 Bread,2.74 Soda crackers,3.27 Macaroni,1.45 Flour,4.54 Corn flakes,5.72 Apples,4.71 Bananas,1.56 Oranges,3.70 ... a. In the function createPricesDict(), create a dictionary of each product mapped to its price. b. Suppose we have another dictionary for our cart...
I have to write an initial algorithm and a refined algorithm for the following problem: Display...
I have to write an initial algorithm and a refined algorithm for the following problem: Display the square of numbers from 0 to some user inputted value. For example if the user enters 3 then the program will need to display the square of 0, 1, 2 and 3. Must use a counter loop. Must use 2 methods. One method that gets the number from the user and returns it. A second method is passed that number as a parameter...
2.     Modify assignment 1 solution code I posted to do the following: a.     Change car class to bankAccount...
2.     Modify assignment 1 solution code I posted to do the following: a.     Change car class to bankAccount class and TestCar class to TestBank class b.     Change mileage to balance c.     Change car info (make, model, color, year, fuel efficiency) to customer first and last name and account balance d.     Change add gas to deposit (without limit) e.     Change drive to withdraw cash f.  Change test car menu to the following Bank Account Menu choices 1 - Deposit 2 - Withdraw 3 - Display account info 4...
If there are 32 concurrent processes, how will you modify the following code? Process i do...
If there are 32 concurrent processes, how will you modify the following code? Process i do { while (turn == j);                critical section; turn = j;                remainder section } while (true);
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS ....
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS . Add the following functions to the class arrayListType: Then, update the main function to test these new functions. removeAll - which removes ALL of the instances of a value in the list min - which returns the minimum value in the list max - which returns the maximum value in the list arrayListType.h : #ifndef H_arrayListType #define H_arrayListType class arrayListType { public: bool isEmpty()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT