By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Adding numbers to arrays depending on their size.

P: 9
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.
Nov 12 '08 #1
Share this Question
Share on Google+
10 Replies


100+
P: 256
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.
Nov 12 '08 #2

P: 9
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_back (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 ?
Nov 12 '08 #3

boxfish
Expert 100+
P: 469
The way you declare the vector and push_back elements looks good. I'm not so sure about the last bit;
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?
Nov 12 '08 #4

P: 9
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?
Nov 12 '08 #5

100+
P: 424
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:
Expand|Select|Wrap|Line Numbers
  1. // using indices:
  2. for (int i = 0; i < vect.size(); i++)
  3. {
  4.   if (i%10 == 0)
  5.     std::cout << std::endl; // newline every 10 numbers
  6.   std::cout << vect[i] << " ";
  7. }
  8. // or, using iterators:
  9. std::vector<int>::iterator p;
  10. for (p = vect.begin(); p != vect.end(); p++)
  11.    std::cout << *p << " ";
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.
Nov 12 '08 #6

P: 9
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..
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7.   vector<int> myvector;
  8.   vector<int> myvector2;
  9.   int myint;
  10.  
  11.   cout << "Please enter some integers (enter 0 to end):\n";
  12.  
  13. while (true)
  14. cin >> myint;
  15. if (myint > 9) {
  16. myvector.push_back (myint);
  17. }
  18.  
  19. else if (myint < 7 ){
  20. myvector2.push_back (myint);
  21. }
  22.  
  23. else if (myint == 0);{
  24.  std::vector<int>::iterator p;
  25.  for (p = myvector.begin(); p != myvector.end(); p++)
  26.     std::cout << *p << " is in vector1";
  27.  
  28.      std::vector<int>::iterator q;
  29.  for (q = myvector2.begin(); q != myvector2.end(); q++)
  30.     std::cout << *q << " is in vector2 ";
  31.  
  32. }
  33.  
  34.  
  35.   return 0;
  36. }
  37.  
Something is screwed up and I can see that I just cant seem to get what it is
Nov 12 '08 #7

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. while (true)
  2.    cin >> myInt;
When does this loop end?
Nov 12 '08 #8

boxfish
Expert 100+
P: 469
Expand|Select|Wrap|Line Numbers
  1. else if (myint == 0);{
Get rid of the semicolon here.
Nov 12 '08 #9

P: 9
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 (contentsaddedInVector/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
Nov 13 '08 #10

Ganon11
Expert 2.5K+
P: 3,652
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.
Nov 13 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.