Removing vector int

Feb 22, 2017 at 10:07pm
closed account (3vX4LyTq)
I want to make a list of twenty integers. Then, I check each one for reapeats. However I think I may be doing this wrong and I need to complete this problem.

I get a bunch of jarbled gunk when I am done. The problem is definetly in my nested for loops.

If someone can help I would really appreciate this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <time.h>
using namespace std;
int main() {
    srand(time(NULL));///Instructor told me to use this so don't hate
    vector<int> listo;
    cout << "Here is Santa's list!\n";

    for(int i = 0; i<20; i++){
      listo.push_back(rand() % 20 + 1);///Fills vector with 20 random integers
      cout << listo[i] << " ";///Prints the vector
    }
    cout << "\n";
    system("PAUSE");

    /*http://www.cplusplus.com/reference/vector/vector/?kw=vector
    err is just a copy of listo*/

    cout << "\nMaybe Santa made a mistake! Here is the revised version. \n";

    for(int i = 0; i < 20; i++){
      for(int n = 0; n < listo.size(); n++){
        if(listo[n] == listo[n+i]){
            listo.erase(listo.begin() + n);///deletes value
        }
      }
    for(int i = 0; i < listo.size(); i++){
        cout << listo[i];///prints off revised vector
        cout << " ";
    }
    }

}


Any help or tips? Thanks
Feb 22, 2017 at 10:43pm
Line 23: The 'i' goes through values 0..19 no matter how many have already been erased.

Line 25: Lets assume that nothing was erased.
Therefore on the last iteration of the nested loops the listo.size() is still 20, i==19 and n==19.
That leads to:
if ( listo[19] == listo[ 19+19 ] )
The 38 < 20 is usually not true.

You do have plenty of out of range errors.
Feb 22, 2017 at 10:47pm
closed account (3vX4LyTq)
Ok so how do I fix that
Feb 23, 2017 at 5:41am
post removed due to offensive OP handle just noticed
Last edited on Feb 23, 2017 at 6:40am
Feb 23, 2017 at 6:06am
closed account (48T7M4Gy)
Why is this creep being replied to?

The user name is offensive and does not meet any sort of reasonable community standard. It's very disappointing that I am the only one prepared to say something about it.
Feb 23, 2017 at 6:19am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdlib> // <stdlib.h>
#include <vector>
#include <ctime> // <time.h>

// using namespace std;

constexpr auto SIZE = 20 ; // 10;
constexpr auto low_bound = 0;
constexpr auto up_bound = 20;

int main() {

    std::srand(time(nullptr/*NULL*/));///Instructor told me to use this so don't hate
    std::vector<int> listo;

    std::cout << "Here is Santa's list!\n";

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

        // listo.push_back(rand() % 20 + 1);///Fills vector with 20 random integers
        listo.push_back( std::rand() % (up_bound-low_bound) + low_bound + 1 ) ;

        std::cout << listo[i] << " ";///Prints the vector
    }
    std::cout << '\n' ; // "\n";
    // system("PAUSE");

    /*http://www.cplusplus.com/reference/vector/vector/?kw=vector
    err is just a copy of listo*/

    std::cout << "\nMaybe Santa made a mistake! Here is the revised version. \n";

    // for each number in the vector
    // since we are erasing duplicate numbers, the size may become less than SIZE
    // size_t: http://en.cppreference.com/w/cpp/types/size_t
    for( std::size_t i = 0; i < listo.size() /*20*/ ; ++i ) {

        // check if any number after this number is a duplicate
        // note: we do not have to look at numbers before this number
        //       duplicates of earlier numbers have already been taken care of
        for( std::size_t n = i+1;  n < listo.size() ; ++n ) {

            if( listo[n] == listo[i] ) { // found a duplicate number

                listo.erase( listo.begin() + n );///deletes value
            }
        }
    }

    for( std::size_t i = 0; i < listo.size() ; ++i ) {

        std::cout << listo[i] << ' ' ;///prints off revised vector
        // cout << " ";
    }
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/4987f9baa9876077
http://rextester.com/SAM87304
Feb 23, 2017 at 6:29am
closed account (48T7M4Gy)
Why is this creep being replied to?

The user name is offensive and does not meet any sort of reasonable community standard. It's very disappointing that I am the only one prepared to say something about it.

And again!
Feb 23, 2017 at 6:39am
sorry kemort, you're right. i just noticed it and removing my previous post. most of the time i just read the problem only but this is indeed very bad. thanks for raising your voice
Feb 23, 2017 at 6:45am
closed account (48T7M4Gy)
It's OK gunner I understand and it's no fault of anybody who has responded.

I'm not being judgmental about anybody other than this creep.

It's interesting he's been here many times and had good responses and 'service' but thinks he's cute with a name like that. Well he's left a substantial internet track that hasn't gone unnoticed.
Topic archived. No new replies allowed.