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.
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <time.h>
usingnamespace 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 << " ";
}
}
}
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.
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.
#include <iostream>
#include <cstdlib> // <stdlib.h>
#include <vector>
#include <ctime> // <time.h>
// using namespace std;
constexprauto SIZE = 20 ; // 10;
constexprauto low_bound = 0;
constexprauto 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_tfor( 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' ;
}
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.
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
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.