Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6626153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:51:36+00:00 2026-05-25T21:51:36+00:00

I have a Deck object (deck of cards) which is a double-ended queue implemented

  • 0

I have a Deck object (deck of cards) which is a double-ended queue implemented as a doubly-linked list. I would like to be able to shuffle the queue at will, but the way I would go about it is beyond me. So instead I’ve opted to pre-shuffle an array a pointers to the cards and enqueue them after the fact. Problem is, the code I have now doesn’t seem to be initializing the pointers at all.

void BuildDeck(Deck* deck) {
    Card** cards = new Card*[20];
    const size_t MAX_INTEGER_LENGTH = sizeof(int) * 4;
    char szPostfix[] = "_Card.bmp"; 

    for(int i = 1; i < 21; i++) {
        char path[MAX_INTEGER_LENGTH + sizeof(szPostfix) + 1];
        sprintf(path,"%d%s",i, szPostfix);
        cards[i-1] = new Card(i,path);
    }
    ShuffleArray(cards);
    for (int i = 0; i < 20; i++) {
        deck->PushTop(cards[i]);
    }
}

void Swap(Card* a, Card* b) {
    Card temp = *a;
    *a = *b;
    *b = temp;
}

void ShuffleArray(Card** cardArray) {
    srand(dbTimer());
    for (int i = 0; i < 20; i++)
        Swap(cardArray[i],cardArray[rand()%20]);
}

I think where I screwed up is in the card[i] = new Card(...) line, but it somehow looks right to me.

Any suggestions would be appreciated.

DISCLAIMER: I know I should be using the standard library for most of this stuff, but I’m trying to teach myself the hard stuff first. It’s just the way I learn.

EDIT: I fixed the index problem. Now I’ve just gotta figure out why some image aren’t drawing now… :/ Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:51:36+00:00Added an answer on May 25, 2026 at 9:51 pm

    Your code has many problems

    1. You are looping with 1 <= i <= 20 but for an array of 20 elements indexing goes from 0 <= index <= 19. You need to use cards[i-1] = new Card(i,path);

    2. You are allocating the array of pointers cards but you are not deallocating it (memory leak). Either deallocate it with delete[] cards; once you are done or just use a stack based array with Card *cards[20]; instead of allocating it with new.

    3. The way you compute MAX_INTEGER_LENGTH shows you don’t really understand what sizeof does.

    4. This is the reason for which the cards don’t get shuffled. You wrote a function that swaps two pointers, but the pointers it is swapping are local variables (parameters) of the function, not the elements of the array. One solution is to pass the parameters as pointer references by declaring swap with void Swap(Card *& a, Card *& b), another solution would be passing pointers to pointers (but this would require a more complex syntax of the implementation because of the double indirection and would also require a change in the way you call the function).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my slide deck hidden on every page but I would like it
I have just had a brain block, I have a Deck object and want
I have several objects set up in Core Data, one of which is Deck
I have a method to generate a Deck object (NSObject subclass with an NSMutableArray
I have a standard deck of 52 cards is represented in an array. Each
I have the following model: class Deck < ActiveRecord::Base has_many :cards serialize :stats attr_accessible
Hi I have created a List of Objects. Each object contains a Set. I
I have my linq code formatted like: <Deck> <Treasure> <card> ..... </card> ...... </treasure>
I have classes Pile which represents a card deck, and so contains instances of
I have a standard deck of cards and then have removed a few, from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.