Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为循环中的数组设置特定值?-C_C_Arrays_Sorting - Fatal编程技术网

如何为循环中的数组设置特定值?-C

如何为循环中的数组设置特定值?-C,c,arrays,sorting,C,Arrays,Sorting,我正在学习一门初级编程课程,我们的任务是制作一个以10人开始的游戏程序。你从第一个人开始,数到三,然后放下那个人。你一直这样做直到只剩下一个人 #include <stdio.h> #include <stdlib.h> int main() { int n[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, i = 0; int * ptrN = &n[i]; for( i = 0; n[i]; n[i+3])

我正在学习一门初级编程课程,我们的任务是制作一个以10人开始的游戏程序。你从第一个人开始,数到三,然后放下那个人。你一直这样做直到只剩下一个人

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, i = 0;
    int * ptrN = &n[i];

    for( i = 0; n[i]; n[i+3]){

        if (n[i+3] = 1){ 
            *ptrN = 0; 
        }
        else if (n[i] = 0)
            continue;
        else
            printf("Wrong");
    }
    printf("The value of n0 %d\tn1 %d\tn2 %d\tn3 %d\tn4 %d\tn5 %d\tn6 %d\tn7 %d\tn8 %d\tn9 %d", n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[8], n[9]);
    return 0;
}
#包括
#包括
int main()
{
int n[10]={1,1,1,1,1,1,1,1,1},i=0;
int*ptrN=&n[i];
对于(i=0;n[i];n[i+3]){
如果(n[i+3]=1{
*ptrN=0;
}
else如果(n[i]=0)
继续;
其他的
printf(“错误”);
}
printf(“n0%d\tn1%d\tn2%d\tn3%d\tn4%d\tn5%d\tn6%d\tn7%d\tn8%d\tn9%d”,n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],n[9]);
返回0;
}

我现在离我的答案还很远,但我已经遇到了问题。当我运行上述代码时,它只会更改n[0]的值,然后退出程序。希望您能给予指导。感谢您

如评论中所述:

  • 使用
    =
    进行比较,
    =
    用于赋值
  • 您不需要使用指针。您可以使用
    n[i]
  • 您需要增加
    i
  • 您需要跳过
    0
  • 正如您所提到的,当只剩下一个
    1
    时,您还需要停止

    解决这个问题的方法不止一种,但你可以通过设置一个计数器来计算有多少个
    1
    ,并设置一个
    while
    -循环,当计数器降到
    1
    时结束:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, count = 10;
    
        /* `i` needs to start at -1 so that we start counting:
         *     "0, 1, 2. n[2] is out!"
         *  instead of counting:
         *     "1, 2, 3. n[3] is out!"
         */
        int i = -1;
    
        while (count > 1) {
    
            /* We need to count three of those persons that are not out.
             * That means we have to search for the next person that is not
             * out (by skipping over `0`) and repeat that three times.
             *
             * If we just increase `i` by 3 ( `(i + 3) % 10` ) then we are
             * not checking how many of those persons between `n[i]` and
             * `n[(i + 3) % 10]` needed to be skipped over.
             *
             * So repeat three times:
             */
            for (int j = 0; j < 3; j++) {
    
                /* This will search for the next person that is not out by 
                 * increasing `i` by one until `n[i]` is not `0`:
                 */
                do {
                    i = (i + 1) % 10;
                } while (n[i] == 0); // Check next person if this one is out.
    
            } // I forgot to close this bracket in my first version of the answer
    
            n[i] = 0;
            count--;
        }
    
        printf("The value of n0 %d\tn1 %d\tn2 %d\tn3 %d\tn4 %d\tn5 %d\tn6 %d\tn7 %d\tn8 %d\tn9 %d", n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[8], n[9]);
        return 0;
    }
    
    #包括
    #包括
    int main()
    {
    int n[10]={1,1,1,1,1,1,1,1,1},count=10;
    /*`i`需要从-1开始计算:
    *“0,1,2。n[2]已退出!”
    *而不是计算:
    *“1,2,3.n[3]出局了!”
    */
    int i=-1;
    而(计数>1){
    /*我们需要数一数那些没有被排除在外的人中的三个。
    *这意味着我们必须寻找下一个不是
    *跳出(跳过“0”)并重复三次。
    *
    *如果我们只是将'i'增加3('i+3)%10'),那么我们就是
    *不检查其中有多少人介于'n[i]`和'
    *`n[(i+3)%10]`需要跳过。
    *
    *所以重复三次:
    */
    对于(int j=0;j<3;j++){
    /*这将搜索下一个不在的人
    *将'i'增加一,直到'n[i]`不是'0':
    */
    做{
    i=(i+1)%10;
    }while(n[i]==0);//检查下一个人是否已退出。
    }//我忘了在我的第一个答案中关闭这个括号
    n[i]=0;
    计数--;
    }
    printf(“n0%d\tn1%d\tn2%d\tn3%d\tn4%d\tn5%d\tn6%d\tn7%d\tn8%d\tn9%d”,n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],n[9]);
    返回0;
    }
    
    噢,我试图在删除该人员后将n[]的值设置为0。一旦只剩下1“1”,我如何设置循环的出口?预期的输出是什么?
    ptrN
    始终指向数组的第一个元素。更新
    i
    不会自动更新
    ptrN
    。您需要使用
    =
    进行比较,
    =
    用于分配。如果您先用英语描述程序应该做什么,可能会更好。非常感谢。你能解释一下for循环吗?。我不知道j是从哪里来的。我相信第一个离开的人是n[2]对吗?对于i值,它不需要是i=(i+3)%10;?很抱歉问了这么多问题。D:我会添加更多的注释来更好地解释我的代码。