Write a program that inputs ten integer values from the keyboard. Each value should be pushed onto a
vector. After inputting the values, remove from the vector any repetition of a value. Finally output the list of
values that remain.
this is my homework but I dont understand exactly question can anybody explain to me how I can solve this question please
Write a computer program.
The program will let the user enter ten numbers, using the keyboard.
You are to take each input value and store them in a vector
Then, look at that vector and remove any duplicate numbers.
Then, output each number from the vector to the screen.
nt n;
cout<<"input ten numbers:"<<endl;
vector <int> v(n);
What do you think is the value of n?
You should get 10 int from the keyboard not declaring them in code.
To remove duplicates you can sort the vector first and then use use std::unique http://www.cplusplus.com/reference/algorithm/unique/
int main()
{
vector<int>V1;
for (int x; V1.size() < 10; )
{
cin >> x;
V1.push_back(x);
}
sort(V1.begin(), V1.end());
for(int i = 0; i < V1.size(); ++i)
if (i == 0 || V1[i - 1] != V1[i])
cout << V1[i] << " ";
cin.get();
return 0;
}
I put cin.get() instead of getch() simply because my Visual Studio 2017 doesn't recognize getch. I'm probably not using a header or something.
The first "for" statement will read the first ten integers and put it into the vector. So even if you put more than ten, it'll just take the first ten.
After sorting, the second "for" followed by "if" statements is what will check for repeating values. First it checks to see if it's the first integer in the vector and prints it, then it checks to see if each integer after is equal to the one before it and only print if it isn't.