根据数量大小向数组添加数字。 [英] Adding numbers to arrays depending on their size.

查看:69
本文介绍了根据数量大小向数组添加数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我自己做了一段时间的工作但是当它开始变得有趣时我很难挣扎:)


我已经得到了我的基本代码,我甚至得到了将数据输出到文件准备的代码(至少我认为:)。

问题是,软件需要做的是将值排序为一个数组(我相信应该使用。)取决于卡的价值。

卡deuce-6应分配给plusOne数组/向量w / e。

7到9中性和T-Ace到minusOne。


问题是如果我不知道将会有多少张卡,我真的不知道如何声明阵列添加,因为我需要计算平均值。


我可以这样做吗?


while(true)

输入数字


if(数字介于2和6之间){

我将此方法添加到数组的方法是什么? (如果那是应该使用的东西)

}


else if(number is TA){

add to minusOne数组

}


让我们说我的初学者需要在10张牌被处理后评估牌组的价值我该怎么办?想想?


很多愚蠢的问题。


谢谢。

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.

推荐答案

如果你正在使用C,你必须声明一个值是什么类型的指针(我想是整数?)然后使用malloc()或一些相关的函数(realloc()也很方便)动态成长你的阵列。


如果你正在使用C ++,你可以得到一个向量并使用push_back()。


如果你在Linux上,你可以阅读你的malloc和realloc的manpages(man malloc或man realloc在命令行上) - 否则只是google,你会发现更多帮助。
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.


确定。

我正在使用cpp。

试图让它运行但它似乎忽略了我添加的请求数字并将它们打印出来。

但是我对算法做了一些工作。

我只创建三个变量vector< int>我的载体

创建myint。


而true输入int的某些值。

如果myint在这个区间内我想要myvector.push_back(myint); (对吗?)

否则如果不将它添加到另一个矢量等...

}



cout(INT)myvector [0这里或者无论如何只是让它显示数字不依赖于它是否是]


嗯不应该有一个更容易那个方法?
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 ?


声明vector和push_back元素的方式看起来不错。我对最后一点不太确定;
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这里或者无论如何只是让它显示不依赖于的数字如果是'
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?


这篇关于根据数量大小向数组添加数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆