You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言统计数组中连续3和6的for循环未执行问题求助

统计数组中3和6连续出现次数的问题分析与修正

我是C语言新手,需要编写代码统计含随机整数的array[100]数组中,数字3和6连续出现的次数。尝试了三种写法,但统计相关的循环或判断始终无法正常执行,以下是三次尝试的代码:

1. 新增for循环并在内部加入if条件

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

#define SIZE 100
#define MAX 990
#define MIN 100

int main(void) {

  int index;
  int array[SIZE];
  int q;

  srand(time(NULL));

  for (index = 0; index < SIZE; index++){
    
    array[index] = (rand() % (MAX - MIN + 1) + MIN) / 10;

    printf("Índice [%i]: %i\n", index, array[index]); 
  }

  for(index = 0; index < SIZE; index++){
    if (array[index] == 3 && array[index + 1] == 6){
      q++;
      printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.", q);
    }
  }

  
  return 0;
}

2. 移除for循环仅保留if语句

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

#define SIZE 100
#define MAX 990
#define MIN 100

int main(void) {

  int index;
  int array[SIZE];
  int q;

  srand(time(NULL));

  for (index = 0; index < SIZE; index++){
    
    array[index] = (rand() % (MAX - MIN + 1) + MIN) / 10;

    printf("Índice [%i]: %i\n", index, array[index]); 
  }

  
    if (array[index] == 3 && array[index + 1] == 6){
      q++;
      printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.", q);
    }


  
  return 0;
}

3. 将if条件加入第一个for循环(仅该写法的判断被执行)

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

#define SIZE 100
#define MAX 990
#define MIN 100

int main(void) {

  int index;
  int array[SIZE];
  int q;

  srand(time(NULL));

  for (index = 0; index < SIZE; index++){
    
    array[index] = (rand() % (MAX - MIN + 1) + MIN) / 10;

    printf("Índice [%i]: %i\n", index, array[index]); 

    if (array[index] == 3 && array[index + 1] == 6){
      q++;
      printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.", q);
    }
  }


  
  return 0;
}

错误分析

三次尝试都存在共性问题,也有各自的逻辑错误:

共性问题

  • 计数器q未初始化:C语言中局部变量默认不会初始化为0,q的初始值是内存中的随机垃圾值,统计结果必然错误。

各版本的特有错误

  1. 第一个版本

    • 循环越界:第二个for循环条件index < SIZE,当index = 99时,array[index+1]就是array[100],超出了数组的有效范围(数组下标是0~99),会触发未定义行为。
    • 打印时机错误:每次找到匹配就打印报告,会重复输出多次,应该在统计完成后统一打印最终结果。
  2. 第二个版本

    • 无遍历逻辑:第一个循环结束后index的值是100,此时array[index]和array[index+1]都是越界访问,且只做了一次无效判断,完全没有遍历数组中的所有连续元素对。
  3. 第三个版本

    • 循环越界:同样存在index = 99时访问array[100]的越界问题。
    • 判断时机错误:在生成当前元素时就检查下一个元素,但下一个元素还未被赋值(比如index=0时,array[1]还没执行随机生成的代码),此时的判断完全无效。

修正后的代码

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

#define SIZE 100
#define MAX 990
#define MIN 100

int main(void) {

  int index;
  int array[SIZE];
  int q = 0; // 初始化计数器为0

  srand(time(NULL));

  // 第一步:生成完整数组并打印
  for (index = 0; index < SIZE; index++){
    array[index] = (rand() % (MAX - MIN + 1) + MIN) / 10;
    printf("Índice [%i]: %i\n", index, array[index]); 
  }

  // 第二步:遍历数组统计连续的3和6,循环到SIZE-1避免越界
  for(index = 0; index < SIZE - 1; index++){
    if (array[index] == 3 && array[index + 1] == 6){
      q++;
    }
  }

  // 第三步:统一打印最终统计结果
  printf("Relatório: 3 e 6 foram encontrados consecutivamente %i vezes.\n", q);

  return 0;
}

内容的提问来源于stack exchange,提问作者user18912852

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:45:40