Code:
#include <iostream>
#include <ctime>
#include <vector>
#include <random>
struct MyvecSort {
bool operator () (int x, int y) {return x<y;};
}SortingVector;
int main(int argc, const char * argv[])
{
std::vector<int>MyVector;
for (int i=0; i<3600; ++i)
{
std::random_device rd;
//asking as if they are two dice
std::uniform_int_distribution<int> uniform_dist (2,12); //producing random #'s 2-12
std::mt19937 e1(rd());
int num = uniform_dist(e1);
MyVector.push_back(num); //pushing 3600 random #'s to vector
}
std::sort(MyVector.begin(), MyVector.end(), SortingVector); //sorting 3600 random numbers
size_t limit = MyVector.size();
for (int i=0; i<limit; ++i) {
int count =1;
while (i<limit && MyVector[i]==MyVector[i+1] ) {
count++;
i++;
}
std::cout<<MyVector.at(i)<<"\t"<<count<<"\n";
}
return 0;
}
This code is going to look strikingly familiar to the one I just made on counting numbers from a file. However now I am just producing random one each time.
I am just having a hard time thinking of different ways to do this. White Flags made me see that a for loop can be used is several other ways when he wrote:
Code:
void AddressBook::cleanup()
{
nodePtr next;
for (nodePtr walk = head; walk != NULL; walk = next) {
next = walk->next;
delete walk;
}
}
Am I facing the same situation here? Am I able to ditch the while loop and just use a for loop? Now I know I have been told place i++ into my while is bad programming practice. However I am wrestling with the idea of how to check the number and send the count to the proper number. Therefore I have this above.