#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
void Remove(vector<int> &a);
int main()
{
srand(time(0));
vector <int> number;
//get random number address 0 to 99 = 100 numbers
for (int i = 0; i < 100; i++)
{
int a = rand() % 500 + 1;
// put random number in each vector
number.push_back(a);
//erase duplicate...
if (number[i] == number[i + 1])
{
number.pop_back();
i--;
}
}
//puts in order to lowest to highest
sort(number.begin(), number.end());
// send vector in address form (reference)
Remove(number);
return 0;
}
void Remove(vector<int> &a)
{
for (int i = 0; i < a.size(); i++)
{
if (i % 10 == 0)
{
cout << endl;
}
cout << left << a[i] << " ";
}
cout << endl;
}
You are checking against number[i+1]. This will cause... problems. Think about it- you push back a at 0, then check of numbers[0] == numbers[1]... but what is numbers[1]?
I run it on code block(win) and no error run ok,
but line no 22 if (number[i] == number[i + 1]) is not ok;
i+1 is show undefined no.
it should be i-1 for previous one.