Thread: 2 FOR loops

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    UK
    Posts
    4

    2 FOR loops

    Hello everyone,

    I am trying to calculate my arrivalscount and departurescount using a vector but my second loop isnt outputing any value. I think its because of my for loops......but I'm not sure. CAN I WRITE 2 FOR LOOPS TOGETHER?? Please advice>>>Thanks.
    This is what I wrote in my main:
    insert
    Code:
    
    
    Code:
    int main()
    {
        srand(time(NULL));
    
    
        Customers c;
        c.generateBookings();
        vector<Booking> a = c.getbooking();
    
    
        c.generateBookings(0, 0, 0, 0, 0);
        vector<Booking> b = c.getbooking();
    
    
    for(int i=0; i < 10;i++)
    {
    cout << "Booking number " << i << ":" << endl;
    cout << "booking: " << b[i].bookingTime << endl;
    cout << "arrival: "<<b[i].arrivalTime << endl;
    cout << "departure: "<<b[i].departureTime << endl;
    }
    
    
    int arrivalscount[10];
    int totalarrivals = 0;
    
    
    for(int i = 0; i < 10; i++)
    {
        arrivalscount[i] = 0;
        for(int j = 0; j < a.size() - 1; j++)
        {
            if(a[j].arrivalTime >= i && a[j].arrivalTime < i + 1)
            {
                arrivalscount[i] += 1;
                totalarrivals += 1;
            }
        }
    }
    
    for(int i = 0; i < 10; i++)
    {
        cout << "Interval [" << i << "," << i + 1 << ") arrivals: " << arrivalscount[i] << '\n';
    }
    cout << "Total arrivals = " << totalarrivals << '\n' ;
    
    
    
    //*************************************************
    int departurescount[10];
    int totaldepartures = 0;
    
    for (int i = 0; i < 10; i++)
    {
        departurescount[i] = 0;
        for (int k = 0; k = a.size() - 1; k++)
        {
            if(a[k].departureTime >= i && a[k].departureTime < i + 1)
            {
                departurescount[i] += 1;
                totaldepartures += 1;
            }
        }
    }
    
    for (int i = 0; i < 10; i++)
    {
        cout << "Interval [" << i << "," << i + 1 <<") departures: " << departurescount[i] << '\n';
    }
    cout << "Total departures = " << totaldepartures << '\n';
    
    Last edited by Daive; 12-13-2011 at 07:17 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There's certainly nothing wrong with writing loops within loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Which "second loop" are you referring to?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  2. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  3. loops ?
    By St0rM-MaN in forum C Programming
    Replies: 3
    Last Post: 06-26-2007, 02:38 PM
  4. loops ?
    By newbie02 in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2003, 07:33 AM
  5. Loops
    By robjules in forum C++ Programming
    Replies: 7
    Last Post: 11-05-2002, 07:00 PM