使用相同的值填充数组,循环到复位值 [英] Filling an array with the same value, looping to reset values

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

问题描述

我正在编写一个编程项目(mancala),并且在编写数组时遇到困难。我需要写一个数组,填充所有的板仓,值为4,然后将两个bin重置为0.到目前为止我只有

I'm working on a programming project (mancala) and have gotten stuck when writing an array. I need to write an array that fills all board bins with the value of four, and then resets two bins to 0. So far I only have

{

    int i;
    int beadArray[MAX] = {4};

    for (i = 0; i < MAX; i++)
    {
           beadArray[i] = -1;
    }

    for (i = 0; i < MAX; i++)
    {
            cout<<i<<"\t";

    }


推荐答案

你指定要使用循环来填充索引6和13?如果你只填充两个值,则不需要循环。

Why do you specify that you want to use a loop to fill in indices 6 and 13? If you're only filling in two values a loop is not necessary.

由于这只是一个mancala游戏,如果你不需要使用大于255(单个字节可以容纳的最大值),那么可以使用unsigned char数组和memset。

Since this is only for a mancala game, if you don't need to use numbers larger than 255 (the maximum value a single byte can hold) then you can use an unsigned char array and memset.

char beadArray[MAX];
memset( beadArray, 4, sizeof(beadArray));
beadArray[6] = beadArray[13] = 0;
for( int i = 0; i < sizeof(beadArray); i++)
    cout << beadArray[i] << "\t";

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

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