使用输入填充数组 [英] Filling in arrays with input

查看:67
本文介绍了使用输入填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我希望有一个数组填充了一定数量的用户输入数字,我将如何解决这个问题?

此外,我可能会找出输入一定数量的数字的次数数组?


我有一个小小的想法,但有可能有

cin>> n

array [n]并让它工作......

还是需要输入10个单独的变量?

谢谢你!

解决方案


如果我希望有一个数组填充一定数量的用户输入数字我将如何关于这个?

我还可以找出一定数量输入阵列的次数吗?


我有一个小小的想法,但是它可以有

cin>> n

array [n]并让它工作......

还是需要输入10个单独的变量?

谢谢您!



取决于您希望用户输入多少变量。


我会做一段时间陈述并且有sentinel值(例如-1或0)表示退出,或者在数组中有一个设定的数字。你也不想这样做:

展开 | 选择 | Wrap | 行号


如果您正在谈论使用用户定义的数组声明元素的数量,那么你将不得不使用指针。您不能声明包含10个元素的数组,然后向用户询问他们想要的多少值。当您开始写入数组[10],数组[11],...,数组[2175638675]时,您的编译器将不会给您带来任何问题,您实际上是在覆盖内存,这可能会严重弄乱您的计算机。您必须确保在阵列中有空间时才允许输入数据。一个while循环不会这么做 - 你应该使用for循环,如下所示:

展开 | 选择 | 换行 | 行号


< blockquote> Ganon11是对的。我试图表明你不能用它然后用它作为索引。如果您需要读取未定义数量的条目并保存它们,您可能需要一个列表或某种类型的容器,它不需要在实例化时显式声明其大小。


If I wish to have an array filled with a set number of user inputed numbers how would i go about this?
Also I could possibly then seek out how many times a certain number was input into the array?

I have a slight idea but is it possible to have
cin >> n
array[n] and get that to work......
or would it require inputting 10 seperate variables?
thank you!

解决方案

If I wish to have an array filled with a set number of user inputed numbers how would i go about this?
Also I could possibly then seek out how many times a certain number was input into the array?

I have a slight idea but is it possible to have
cin >> n
array[n] and get that to work......
or would it require inputting 10 seperate variables?
thank you!

Depends on how many variables you want the user to put in.

I would either do a while statement and have a sentinel value (such as -1 or 0) mean exit, or have a set number in the array. You also don''t want to do:

Expand|Select|Wrap|Line Numbers


If you are talking about declaring an array with a user-defined amount of elements, then you will have to use pointers. You cannot declare an array with 10 elements and then ask the user for however many values they want. Wile your compiler will give you no problems when you start writng to array[10], array[11], ..., array[2175638675], you are actually overwriting memory, which could seriously mess up your computer. You must be sure you are only allowing data to be entered while there is room in the array. A while loop won''t do this well - you should use a for loop, as follows:

Expand|Select|Wrap|Line Numbers


Ganon11 is right. I was trying to show that you can''t cin n and then use it as an index. If you need to read in an undefined number of entries, and save them, you probably want a list or some sort of container that doesn''t need to have its size explicitly declared on instantiation.


这篇关于使用输入填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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