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

C语言拆分整数至5/9倍数数组程序输出异常求助

问题描述

我想把输入的整数分别存入存储5的倍数的数组和存储9的倍数的数组,目前已经完成输入存储,但程序输出的数组结果不符合预期。附上代码及预期输出,请求排查错误并提供反馈。

预期输出示例

The multiple of 5 is: 15 10 5
The multiple of 9 is: 18 27 36

原代码

#include <stdio.h>

int main()
{
    //declaring variable for user input (input)
    int input = 0;
    
    //prompting user to input size of array
    printf("\n\n\tEnter number of values to store: ");
    scanf ("%d", &input);
    
    //declaring and initialising work storage
    int counter = 0, five = 0, nine = 0;
    int store[input];
    int multi5[five];
    int multi9[nine];
    
   // Input values into store[]
    for (counter = 0; counter < input; counter++)
        {
        if (counter == input - 1)
            {
            printf("\n\n\t Please enter the last number: ");    
            
            }
        else 
            {
            printf("\n\n\t Please enter input number %d: ", counter + 1);    
            }
          scanf("%d", &store[counter]);  
        }   
 
    // Separate multiple of 5 and 9 into separate arrays
    for (counter = 0; counter < input; counter++)
        {
        if (store[counter] %5 == 0)
            {
                multi5[five] = store[counter];
                five++;
            }

        if (store[counter] %9 == 0);    
        
            {
                multi9[nine] = store[counter];
                nine++;
            }    
        }

    // Print the multiples of 5
    printf("\n\n\tThe multiples of 5 is : ");
        for (counter = 0; counter < five; counter++)
            {
                printf("%d", multi5[five]);
            } 

    // Print the multiples of 9**
    printf("\n\n\tThe multiples of 9 are : ");
        for (counter = 0; counter < nine; counter++)
    {
        printf("%d", multi9[nine]);
    }

  return 0;
}

错误排查与修正

1. 动态数组初始化错误

int multi5[five]; 和 int multi9[nine]; 定义时,five 和 nine 的初始值为0,会创建长度为0的数组,后续赋值操作直接触发数组越界,属于未定义行为。

解决方法:直接将两个数组的长度设为输入的最大值input(最多所有输入都是5或9的倍数),避免越界问题。

2. 条件语句多余分号

if (store[counter] %9 == 0); 末尾的分号会让if条件成为空语句,后续代码块会无条件执行,导致所有输入数都被存入multi9数组。

解决方法:去掉该分号,让代码块仅在条件成立时执行。

3. 打印数组时索引错误

打印multi5和multi9时,使用了multi5[five]和multi9[nine],这是数组的越界索引(数组有效索引范围是0到长度-1),同时输出缺少空格导致格式混乱。

解决方法:替换为循环变量counter作为索引,并修改输出格式为"%d "添加空格分隔。

修正后的代码

#include <stdio.h>

int main()
{
    int input = 0;
    
    printf("\n\n\tEnter number of values to store: ");
    scanf("%d", &input);
    
    int counter = 0, five = 0, nine = 0;
    int store[input];
    // 用input作为数组最大长度,避免越界
    int multi5[input];
    int multi9[input];
    
    // 输入数据到store数组
    for (counter = 0; counter < input; counter++)
    {
        if (counter == input - 1)
        {
            printf("\n\n\t Please enter the last number: ");    
        }
        else 
        {
            printf("\n\n\t Please enter input number %d: ", counter + 1);    
        }
        scanf("%d", &store[counter]);  
    }   
 
    // 分离5和9的倍数
    for (counter = 0; counter < input; counter++)
    {
        if (store[counter] % 5 == 0)
        {
            multi5[five] = store[counter];
            five++;
        }

        // 去掉多余的分号
        if (store[counter] % 9 == 0)    
        {
            multi9[nine] = store[counter];
            nine++;
        }    
    }

    // 打印5的倍数
    printf("\n\n\tThe multiples of 5 are: ");
    for (counter = 0; counter < five; counter++)
    {
        printf("%d ", multi5[counter]);
    } 

    // 打印9的倍数
    printf("\n\n\tThe multiples of 9 are: ");
    for (counter = 0; counter < nine; counter++)
    {
        printf("%d ", multi9[counter]);
    }

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 22:42:11