473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.
Nov 12 '08 #1
10 1848
mac11
256 100+
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
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
469 Expert 256MB
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
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
arnaudk
424 256MB
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
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
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. while (true)
  2.    cin >> myInt;
When does this loop end?
Nov 12 '08 #8
boxfish
469 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. else if (myint == 0);{
Get rid of the semicolon here.
Nov 12 '08 #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
3,652 Expert 2GB
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

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

Similar topics

7
by: David | last post by:
I have an array that contains numbers in string elements , i want to convert this to integers, i have made a for loop that uses the number of elements in the array and then adds 0, thereby...
7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
1
by: /* frank */ | last post by:
Why I can omit numbers of rows, when I define a two-dimensions array i.e. : double table ; thanks
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
38
by: One Handed Man \( OHM - Terry Burns \) | last post by:
I have a basic question thats been niggling me, but I never found time to look at it before. If I have an enumeration such as this Fiend Enum TravelDirection North South East
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.