Question

(3) Problem P6.10. Implement a function void remove.duplicates (vector<int> & v) discussed in Problem 6.10 that removes dupli

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code

#include<iostream>
#include<vector>
#include<algorithm> // using sort() function
using namespace std;

// Implement readElements() function
void readElements(vector<int> &v)
{
   int n; // declare integer n value
   cout<<"Enter List of Integers: ";
   while(n!=-1) // create while loop until n=-1
   {
       cin>>n; // read n value
       if(n==-1) break; // check condition n=-1 then break this while loop
       v.push_back(n); // stored elements into vector v
   }
}
// Implement show() function
void show(vector<int> &v)
{
   cout<<"\nThe List without duplicates: ";

   for(int i=0;i<v.size();i++) // create for loop until size of vector
   cout<<v[i]<<" "; // display elements
}

// Implement remove_duplicates() function
void remove_duplicates(vector<int> &v)
{
sort(v.begin(),v.end()); // using sort() function first element and last element in the vector
// using unique() function to find duplicate and
// using erase() function remove duplicate until end of element in the vector
   v.erase(unique(v.begin(),v.end()),v.end() );

   }


int main()
{
  
  
   char ch; // declare character ch
  
   while(1) // create infinite while loop
   {
       vector<int> vec; // declare vector element vec
       readElements(vec); // calling readElements() function
       remove_duplicates(vec); // calling remove_duplicates() function
       show(vec); // calling show() function
  
       fflush(stdin);
       cout<<"\nContinue (y/n)? ";
       cin.get(ch); // reading custom character
       if(ch=='n'||ch=='N') break; // check ch='n' or ch='N' then break while loop
   }
   return 0;
}

OUTPUT

Enter List of Integers: 1 2 3 4 5 -1

The List without duplicates: 1 2 3 4 5
Continue (y/n)? y
Enter List of Integers: 1 4 9 16 9 7 4 9 11 -1

The List without duplicates: 1 4 7 9 11 16
Continue (y/n)? n

Add a comment
Know the answer?
Add Answer to:
(3) Problem P6.10. Implement a function void remove.duplicates (vector<int> & v) discussed in Problem 6.10 that...
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
  • C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int...

    C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int new) that replaces all occurrences of the integer old in v with the integer new. For example, if v is the vector {1,2,1,3}, then after calling replace(v, 1, 3) v should be {3,2,3,3}. (2) Write the main-function so that it prompts the user for a list of integers, and then uses your replace function to display the following lists: • The user's list with...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • Problem 8 In class, we discussed the difference between a pure (or effect-free) function (i.e. one...

    Problem 8 In class, we discussed the difference between a pure (or effect-free) function (i.e. one that returns a value, but does not produce an effect) and a mutating function i.e one that changes the value of its parameters). In this exercise, we ask you to demonstrate your understanding o this distinction by implementing the same functionality as both a pure function, and as a mutating function. Both functions take a list of int as a parameter. The pure variant...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • 13.13 Final - Problem 2 (Chapter 8) Google Docs (20 points) Given the Document class below,...

    13.13 Final - Problem 2 (Chapter 8) Google Docs (20 points) Given the Document class below, you will write a basic version of Google Drive. Your program will store two lists of documents, one for documents the user has created and one for created documents the user has shared with a friend. The user will have a few options which you will need to implement (note: the menu will only be displayed once at the beginning of the program): Add...

  • c++ implement a student class Determine the final scores, letter grades, and rankings of all students...

    c++ implement a student class Determine the final scores, letter grades, and rankings of all students in a course. All records of the course will be stored in an input file, and a record of each student will include the first name, id, five quiz scores, two exam scores, and one final exam score. For this project, you will develop a program named cpp to determine the final scores, letter grades, and rankings of all students in a course. All...

  • The following problem concerns the following, low-quality code: void foo(int x) { int a[3]; char buf[1];...

    The following problem concerns the following, low-quality code: void foo(int x) { int a[3]; char buf[1]; a[1] = x; a[2] = 0xA0B1C2D3; gets(buf); printf("a[0] = 0x%x, a[2] = 0x%x, buf = %s\n", a[0], a[2], buf); } In a program containing this code, procedure foo has the following disassembled form on an x86/64 machine: 000000000040057d <foo>: 40057d: push %rbp 40057e: mov %rsp,%rbp 400581: sub $0x30,%rsp 400585: mov %edi,-0x24(%rbp) 400588: mov -0x24(%rbp),%eax 40058b: mov %eax,-0xc(%rbp) 40058e: movl $0xa0b1c2d3,-0x8(%rbp) 400595: lea -0x11(%rbp),%rax 400599:...

  • I need help fixing this code and adding a loop please. Here is the original problem: #include <iostream> using namespace std; //function to print seating chart void printSeatingChart(int **cha...

    I need help fixing this code and adding a loop please. Here is the original problem: #include <iostream> using namespace std; //function to print seating chart void printSeatingChart(int **chart) {    int i, j;    cout << "\nROW\t";    for (i = 1; i <= 10; i++)    {        cout << i << "\t";    }    cout << "\n-----------------------------------------------------------------------------------\n";    for (i = 8; i >= 0; i--)    {        cout << "\n" << i + 1 << "\t";        for (j = 0; j < 10; j++)       ...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops...

    Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops. A synthetic linear recursive procedure for this problem is provided in Code Fragment 1. A recursive function such as the one described is intuitive and easily understandable. On calling myRecursion(input), the execution first checks for the stopping condition. If the stopping condition is not met, then operations inside the recursive call are performed. This can include operations on local variables. The operations inside the function...

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