Not sure how to do this, but Im sure you know how

This is a discussion on Not sure how to do this, but Im sure you know how within the C++ Programming forums, part of the General Programming Boards category; Ok, lets say I have ten stones. In addition I have one pocket. I could easily write a program which ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    Question test

    Ok, lets say I have ten stones. In addition I have one pocket. I could easily write a program which lets me pick up stones, put them in a pocket, and take them out. In addition such a program could keep track of how many stones were in the pocket, and how many weren't.

    But lets say I have two pockets, in that case I would have to do:

    Code:
    int numberofstonesinpocketone=0;
    int numberofstonesinpockettwo=0;
    But what if, I had x pockets. It would be easy to write code which lets me change the number of stones in my system, a simple

    Code:
    cin>>numberofstones;
    would accomplish that, but what if I don't know the number of pockets? I could do:

    Code:
    int numberofstonesinpocketone=0;
    int numberofstonesinpockettwo=0;
    int numberofstonesinpocketthree=0;
    int numberofstonesinpocketfour=0;
    ...
    int numberofstonesinpockettenbillionthreehundredthousandandfiftytwo=0;
    int numberofstonesinpockettenbillionthreehundredthousandandfiftythree=0;
    which although a tedious task, could be done...however, the problem with that method is that I am then left with using excessive memory, if i make my program capable of knowing the contents of 50 pockets then, if I only use 15 pockets, I am wasting memory remembering that 45 pockets aren't being used.

    What can I do? I suspect that objects are what I need to use but it seems every tutorial I find doesnt really address any of my questions, always focusing on some other aspect of the concept.

    It would be beneficial if a group of users worked together to write tutorials, referring those who ask questions to the tutorials, and improving the tutorials over time.
    Last edited by Noobie; 05-14-2003 at 12:12 AM.
    AIM: MarderIII

  2. #2
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    You can do that? neato
    AIM: MarderIII

  3. #3
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    what if I want the array to have two values? For example, if I have red and blue stones, then I could maybe do

    pocket[1][1]=2; meaning that pocket 1 has two blue (1) stones

    or

    pocket[3][0]=6; meaning that pocket 3 has six red (0) stones
    Last edited by Noobie; 05-13-2003 at 11:46 PM.
    AIM: MarderIII

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Originally posted by Salem



    stones *pockets = new stones[numpockets];

    [/code] [/B]
    what does it mean?
    AIM: MarderIII

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It means you're allocating memory for numpockets stones-structures and setting pockets to point to this memory.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Now it makes even less sense to me!
    AIM: MarderIII

  7. #7
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    stones *pockets = new stones[numpockets];

    means:
    1. use the pointer names "pockets".

    2. Allocate memory for an array (called stones) with numpockets elements.

    3. make "*pockets" point to the start of the new array.

    this allows you to make an array of a specified size, rather than having to know the size before the program starts. A Dynamic Array.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    the array is not called "stones", the array elements are of type "stones".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    well i think that if he doesnt get arrays yet he wont get pointers

    try reading this tutorial
    http://www.cprogramming.com/tutorial/lesson8.html
    or
    http://cplus.about.com/library/weekly/aa050102a.htm

    cornedbee...same one from VBF?

    and salem..
    Code:
    int numpockets;
    cin >> numpockets;
    int *pockets = new int[numpockets];
    if i do something like that, VC tells me that numpockets need to be a constant..i tried to do that for my example in this thread, it didnt work so i just used vectors
    Last edited by the Wookie; 05-14-2003 at 05:20 PM.

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Thumbs up

    I hope he has a book and and many reference sites!!!
    Mr. C: Author and Instructor

  11. #11
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    that works..weird, perhaps i was declaring it as

    int *pockets[]=new pockets[size];

    i see now..thanks

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Originally posted by the Wookie
    cornedbee...same one from VBF?
    Yeah, same as VBF and Galahtech
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    ah cool, i dont know if you remember me, but i was nabeels786 over on vbf. its a small internet :P

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Doesn't anybody ever use map containers on this site? Does anybody see that a map would be an easy way to handle this problem? Either a map<int,int> container for the "one kind of stone in any number of pockets" or a map<int,stones> container for the "different kinds of stones in any number of pockets" problem.

    Code:
    map<int,int> pocket;
    pocket[1] = 5; // Pocket one has 5 stones
    pocket[2] = 2; // Pocket two has 2 stones
    Or...
    Code:
    struct stones
    {
        int red;
        int blue;
    }
    ...
    map<int,stones> pocket;
    pocket[3].blue = 5; // Pocket 3 has 5 blue stones
    pocket[6].red = 8;  // Pocket 6 has 8 red stones
    No need to worry about pointers and dynamically allocating space for anything, just create the container and start inserting stuff into it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I would rather use a vector here, it's too simple for a map. I mean, what do you need a tree for just 2 nodes?

    Wookie: Yes, I remember.
    And I think CornedBee is absolutly unique on the internet.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

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