Adding numbers to arrays depending on their size.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jay123swe
    New Member
    • Oct 2008
    • 9

    Adding numbers to arrays depending on their size.

    Hi.
    Been doing some work on my own for a while but Im struggling when it starts to get fun :)

    I've got my basic code finished and I even got the code to output the data to a file ready (atleast I think :).
    The problem is, what the software need to do is to sort the values to an array (I believe that should be used.) depending on the value of a card.
    Cards deuce-6 should be assigned to the plusOne array/vector w/e.
    7-9 to Neutral and T-Ace to minusOne.

    The problem is that I dont really know how to declare the array if I have no idea how many cards that will be added as I need to calculate an average too.

    Could I maybe do it like this?

    while (true)
    enter numbers

    if (number is between 2 and 6) {
    What is my approach to add this to an array? (if that's what should be used)
    }

    else if (number is T-A){
    add to the minusOne array
    }

    Let's say that I for starters need to evaluate the value of the deck after 10 cards have been dealt what should I think about?

    Lots of stupid questions.

    Thanks.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    If you're using C you'll have to declare a pointer of whatever type the values are (I guess ints?) and then use malloc() or some related function (realloc() is handy as well) to dynamically grow your array.

    If you're using C++ you can get a vector and use push_back().

    If you're on Linux you can read your manpages for malloc and realloc (man malloc or man realloc on the command line) - otherwise just google and you'll find more help.

    Comment

    • jay123swe
      New Member
      • Oct 2008
      • 9

      #3
      Ok.
      Im using cpp.
      Tried getting it running but it just seem to ignore my request to add the numbers and print them out.
      Got a little work on the algorithm however.
      I just create three variables as vector<int> my vector
      Create myint.

      while true enter a some values of the int.
      if myint is in the interval that I want myvector.push_b ack (myint); (right?)
      else if it is not add it to another vector etc...
      }


      cout (INT) myvector [0 here or is there anyway to just let it show the number not depending on if it's]

      Hmm shouldn't there be an easier way to that ?

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        The way you declare the vector and push_back elements looks good. I'm not so sure about the last bit;
        Originally posted by jay123swe
        cout (INT) myvector [0 here or is there anyway to just let it show the number not depending on if it's]
        what is this supposed to mean?

        Comment

        • jay123swe
          New Member
          • Oct 2008
          • 9

          #5
          What I meant to say was that if there is any special way that I should declare the variable when i print it out on the screen using cout << if I have alot of numbers let's say 5 entered numbers? Been looking into arrays and if Im correct I need to declare it as array[0], array[1] and I cant really find if its something I should do with vectors that hold alot of numbers too?

          Comment

          • arnaudk
            Contributor
            • Sep 2007
            • 425

            #6
            Originally posted by jay123swe
            What I meant to say was that if there is any special way that I should declare the variable when i print it out on the screen using cout << if I have alot of numbers let's say 5 entered numbers?
            If you mean that you want to display the contents of the vector, you can do either of the following:[code=cpp]
            // using indices:
            for (int i = 0; i < vect.size(); i++)
            {
            if (i%10 == 0)
            std::cout << std::endl; // newline every 10 numbers
            std::cout << vect[i] << " ";
            }
            // or, using iterators:
            std::vector<int >::iterator p;
            for (p = vect.begin(); p != vect.end(); p++)
            std::cout << *p << " ";[/code]
            Originally posted by jay123swe
            Been looking into arrays and if Im correct I need to declare it as array[0], array[1] and I cant really find if its something I should do with vectors that hold alot of numbers too?
            If you're working in C++ and need a dynamic array, then stick to vectors as in your example above. Vectors can hold as many numbers as an array can. Unlike arrays, however, you don't need to tell the vector how many numbers it will need to hold in advance; it will resize itself if required.

            Comment

            • jay123swe
              New Member
              • Oct 2008
              • 9

              #7
              Ok I tried this but I just cant seem to get the input to end and the print to begin.
              I know it is not really done yet if the user enters something like an 8 or something however I just cant seem to get anything printed..
              Code:
              #include <iostream>
              #include <vector>
              using namespace std;
              
              int main ()
              {
                vector<int> myvector;
                vector<int> myvector2;
                int myint;
              
                cout << "Please enter some integers (enter 0 to end):\n";
              
              while (true)
              cin >> myint;
              if (myint > 9) {
              myvector.push_back (myint);
              }
              
              else if (myint < 7 ){
              myvector2.push_back (myint);
              }
              
              else if (myint == 0);{
               std::vector<int>::iterator p;
               for (p = myvector.begin(); p != myvector.end(); p++)
                  std::cout << *p << " is in vector1";
              
                   std::vector<int>::iterator q;
               for (q = myvector2.begin(); q != myvector2.end(); q++)
                  std::cout << *q << " is in vector2 ";
              
              }
              
              
                return 0;
              }
              Something is screwed up and I can see that I just cant seem to get what it is

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Code:
                while (true)
                   cin >> myInt;
                When does this loop end?

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  Code:
                  else if (myint == 0);{
                  Get rid of the semicolon here.

                  Comment

                  • jay123swe
                    New Member
                    • Oct 2008
                    • 9

                    #10
                    Ok I got it working when I fixed the loop.

                    By the way does anyone have a good resource about vectors?
                    I've looked at c++.com in their resource library but I was wondering for stuff like multiplying the content of the vector. Adding it. Seeing the longest "streak" of the same vector if there is anywhere good to look for that?
                    I.e. being able to print : Stats: The average count of your blackjack session was +0.7 (contentsaddedI nVector/plusVector.size () or anything similar?

                    Not asking for you to do any work, I just wonder if there's any other good source for that kind of snippets and explainations (been looking at learncpp but they dont really have alot on vectors.)

                    Thanks again for all the help.
                    Jay

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      It depends on what you mean by each of these things. Does 'multiply the contents of the vector' mean multiplying each element contained in the vector by some constant? Does it mean making the vector larger?

                      Since this sort of behavior isn't well defined, it's up to you to decide what it should mean and implement it. In this case, you probably want to use a loop to access every element in the array and replace its contents with the current element times 3, or whatever.

                      Other than just getting your hands dirty and doing some coding, figuring out your own bugs/errors, and experimenting with new methods, there are few websites (that I know of) that will have such an extensive repository of simple code.

                      Comment

                      Working...