(Duplicate Elimination with vector) Use a vector to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the vector only if it isn't a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Begin with an empty vector and use its push_back function to add each unique value to the vector.
Fix my C++ code please! Question and code is below as well as error picture.
(Duplicate Elimination with vector) Use a vector to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the vector only if it isn't a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Begin with an empty vector and use its push_back function to add each unique value to the vector.
My code;
#include <iostream>
#include <vector>
using namespace std;
int find(vector<int> &v, int num) {
for(int i = 0; i < v.size(); ++i) {
if(v[i] == num) {
return i;
}
}
return -1;
}
int main() {
vector<int> v;
int num;
for(int i = 0; i < 20; ++i) {
cout << "Enter an Integer : ";
cin >> num;
if(num >= 10 && num <= 100 && find(v, num) == -1) {
v.push_back(num);
}
}
// unique numbers entered are
cout << "User entered: ";
for(int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps