While loops and arrays

This is a discussion on While loops and arrays within the C++ Programming forums, part of the General Programming Boards category; Hello, I was going through a beginner's guide book on C++ and could not understand exactly how this example works, ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    While loops and arrays

    Hello, I was going through a beginner's guide book on C++ and could not understand exactly how this example works, even after pondering for hours.

    This program is a card-dealing program which uses a 52-element array in which each element corresponds to a card. Once a card is picked, this array is updated to show that the card should be skipped in the future.


    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    using namespace std;
    
    int rand_0toN1(int n);
    void draw_a_card();
    int select_next_available(int n);
    
    char *suits[4] =  {"hearts", "diamonds", "spades", "clubs"};
    char *ranks[13] = {"ace", "two", "three", "four", "five",
                     "six", "seven", "eight", "nine",
                     "ten", "jack", "queen", "king" };
    
    int card_drawn[52];
    int cards_remaining = 52;
    
    int main() {
        int n, i;
    
        srand(time(NULL));      // Set seed for random numbers.
    
        while (1) {
            cout << "Enter no. of cards to draw (0 to exit): ";
            cin >> n;
            if (n == 0)
                break;
            for (i = 1; i <= n; i++)
                draw_a_card();
        }
        return 0;
    }
    
    // Draw-a-card function
    // Performs one card-draw by getting a random 0-4 and a random
    //  0-12. These are then used to index the string arrays, ranks
    //  and suits.
    //
    void draw_a_card() {
        int r;         // Random index (0 thru 12) into ranks array
        int s;         // Random index (0 thru 3) into suits array
        int n, card;
    
        n = rand_0toN1(cards_remaining--);
        card = select_next_available(n);
        r = card % 13;            // r = random 0 to 12
        s = card / 13;            // s = random 0 to 3
        cout << ranks[r] << " of " << suits[s] << endl;
    }
    
    // Select-next-available-card function.
    // Find the Nth element of card_drawn, skipping over all those
    //  elements already set to true.
    //
    int select_next_available(int n) {
        int i = 0;
    
        // At beginning of deck, skip past cards already drawn.
    
        while (card_drawn[i])
            i++;
    
        while (n-- > 0) {     // Do the following n times:
            i++;                       // Advance to next card
            while (card_drawn[i])      // Skip past cards
                i++;                   //  already drawn.
        }
        card_drawn[i] = true;        // Note card to be drawn
        return i;                    // Return this number.
    }
    
    // Random 0-to-N1 Function.
    // Generate a random integer from 0 to N-1.
    //
    int rand_0toN1(int n) {
        return rand() % n;
    }
    I mainly have problems with the select_next_available card function.
    1.for the " while (card_drawn[i]) ", how is the condition card_drawn[i] used? The initial value of i is 0, so card_drawn[0] is false and the loop shouldnt even start in the first place?

    2.For the while (n-- > 0) loop, does that mean n is further reduced by one here? What is the purpose of doing n--, and if n is initially zero (the rand_0toN1 function picks 0 from the numbers 0 to 51), then n-- = -1??

    3.Lastly, why is i increased so many times in this function? I can see i++ three times here.

    This example is really getting on my nerves~

    Thanks!

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    92
    1. The value of card_drawn[i] is not 0 simply because i is 0. Whatever is in the i-th element of card_drawn[] is what the while() is testing.

    2. because the -- is after the n, the value of n is used first, and THEN it is decremented by 1. Since n is passed in to the function, n could be anything. This loop effectively counts from that n down to 0. (or as we'll see below, skips over n undrawn cards)

    3. i is increased several times for different purposes. The first while loop does what the comment states, so if it skips past 5 cards, i == 5 now (remember i started at 0). Next it advances through n undrawn cards (it skips over any card in that while(n--...) loop that was already drawn by doing the same thing that the first while loop did). When it is done, i has been incremented several times to the target value, which is then returned.
    C+/- programmer extraordinaire

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    I understand that the part while (n-- >0) means do the following n times.
    But why does
    Code:
    while (card_drawn[i])
            i++;
    means finding the first available card by skipping past all the drawn cards? Is it because all the drawn cards are tagged "true"? so for example element 3 is true, while(card_drawn[2]) is true and so i = i+1 =3, the computer skips to element 4?
    Last edited by Freshjunior; 05-15-2010 at 01:46 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    8
    btw, how does return i work? What do you mean when you "return" this number. Does it mean returning i to the beginning of the function, so i becomes 0 once again?

  5. #5
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    return, for most calling conventions, places the return code(0, in this case) in eax and executes a ret. lol
    Basically, it's telling the compiler to finish the function(or the whole program, as is the case if it's used in "int main()") and report the 0 for an error code, 0, in most cases, means everything went good. The WinAPI alternative is ExitProcess.

    Actually, does anyone know if there's some black magic in the return 0 in int main. I know for a fact that compilers add extra code to parse argc and argv from Window's style of command line to any non-Linux executable(Linux actually pushes argc and argv onto the stack). I wonder if compilers do OS specific things after return 0 too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with for loops and arrays
    By Skeeter in forum C Programming
    Replies: 6
    Last Post: 10-23-2009, 03:33 PM
  2. Help with Arrays and For loops
    By Phil- in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2009, 10:34 AM
  3. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 04:09 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. loops and arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-03-2001, 03:12 PM

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