我需要帮助在c ++中使用2d数组 [英] I need help working with 2d arrays in c++
问题描述
我需要在数组的随机位置生成一个数字;
我有一个大小为8的数组。在我的程序开始时我想在任何一个中得到一个数字8个职位。请有人帮助我。在将某些约束应用到程序之前,我需要让这部分工作。顺便说一下约束已经有效了。
I need to generate a number in a random position of my array;
I have an array of size 8. At the beginning of my program I would like to get a number in any of those 8 positions. Please someone help me. I need to get this part working before I apply certain constraints to my program. By the way the constraints are working already.
推荐答案
我需要在我的数组的随机位置生成一个数字;
I need to generate a number in a random position of my array;
生成数字并选择数组的随机位置进行存储,例如
Generate the number and select a random position of your array for storing it, e.g.
int pos = rand() % 8;
a[pos] = n; // n is the 'generated number', whatever you meant with...
在开始我的程序我希望在这8个位置中的任何一个中获得一个数字。
At the beginning of my program I would like to get a number in any of those 8 positions.
这非常相似,在随机位置返回数组的项目,例如
this is very similar, return an item of the array at random position, e.g.
int pos = rand() % 8;
return a[pos];
这篇关于我需要帮助在c ++中使用2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!