better ways to use a for loop:

This is a discussion on better ways to use a for loop: within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <ctime> #include <vector> #include <random> struct MyvecSort { bool operator () (int x, int y) {return ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466

    Thumbs down better ways to use a for loop:

    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.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    I really wasn't using a for loop in a "better" or special way. I am simply used to writing for loops where I can. I assigned a variable called next to walk in the update step of a for loop header. When you delete a linked list, you need to save the next link and assign it as the current node eventually, anyway. In a while loop, of course this would look a little different, but only slightly, since the exact same statement moved from the body to the header of a for loop.

    It is the same for any other algorithm. I might write your code one way, you will write it a different way, and we both ultimately express the same idea.

    Code:
    set count to 0
    take the first element of the vector, call it value
    for each item in the vector:
      if item == value:
        increase count by 1
      else:
        print count
        set count to 1
        set value to item
    end for
    print count
    These are probably the basic steps any implementation will take. I would go as far to say that any logically equivalent implementation is as good as something that I would do.

    There are also alternatives to counting everything up after you sort, but I didn't want to be the one to spoil your fun. Still, in the interest of sharing knowledge: there is a solution that works for a range of values and requires space proportional to the size of that range.
    Last edited by whiteflags; 01-08-2014 at 12:38 PM.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    There are also alternatives to counting everything up after you sort, but I didn't want to be the one to spoil your fun. Still, in the interest of sharing knowledge: there is a solution that works for a range of values and requires space proportional to the size of that range.
    If I would heading in the direction of hash maps would I be wrong? I am about to start learning about them soon, however I have read a little about it.

    I really wasn't using a for loop in a "better" or special way.
    I just never seen it done like that and a little light bulb went off when I seen it. I suppose I was looking for another one.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    If I would heading in the direction of hash maps would I be wrong? I am about to start learning about them soon, however I have read a little about it.
    A hash map would be overkill: a normal array would work.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    Ahhh yes an array I see it now.... Compare the array against the sorted numbers, if they are the, same count the location in the array.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jocdrew21 View Post
    Ahhh yes an array I see it now.... Compare the array against the sorted numbers, if they are the, same count the location in the array.
    If I'm thinking what whiteflags is thinking, then "sorted" is already doing too much work.

    You're doing just the same as all my statistics students: they insist on going through the list one time for every class and counting as they go, and if I'm lucky they sort it first so they only have to go through the list twice. But there's not really a point, when you're creating a frequency list, to go through the list more than once (which leaves sorting right out, since you have to go through the list once (at least!) to sort and then again to count). Just count the numbers as you come across them! In fact, for what you're doing here, storing the numbers at all is just a waste of space. "count[num]++" in place of the push_back and you're good to go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. different ways to program
    By herWter in forum Windows Programming
    Replies: 3
    Last Post: 07-07-2008, 12:41 AM
  2. Different ways to add
    By mike_g in forum C Programming
    Replies: 11
    Last Post: 03-18-2008, 08:32 AM
  3. Best ways to learn
    By MartijnG in forum C++ Programming
    Replies: 15
    Last Post: 08-29-2006, 10:51 AM
  4. Ways to turn men down!
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-29-2005, 12:08 PM
  5. HELP! im looking for different ways .
    By moenia in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21