c++ - 根据用户输入计算数组中的可整除数

标签 c++ multidimensional-array

我刚开始使用 C++,这就是我遇到的问题。我需要编写一个程序来创建一个二维数组,然后允许用户输入一个数字,从那里我需要计算并列出该数组中可被用户输入整除的所有数字。我还没有开始代码的列表部分,但我遇到的问题是我尝试使用的计数函数被零除,所以它不会运行。这是我到目前为止所拥有的,我们将不胜感激任何帮助

void fillArray(int ar [][10], int size);
void printArray(int ar [][10], int size);
int getDivisible (int a [][10], int size, int num);


int main()
{
    srand((unsigned int) time(0));
    int ar[10][10];
    int count = 0;
    fillArray(ar, 10);
    printArray(ar, 10);

    int num;
    cout << "Enter a number to divide by" << endl;
    cin >> num;

    getDivisible(ar, 10, count);
    cout << " There are " << count << " evenly divisible numbers. They are : " << endl;

    cout << endl;

    return 0;
}

void fillArray(int ar [][10], int size)
{
    for (int row = 0; row < size; row++)
    {
        for (int col = 0; col < 10; col++)
        {
            ar[row][col] = rand() % 101;
        }

    }

}

void printArray(int ar [][10], int size)
{
    for (int row = 0; row < size; row++)
    {
        for (int col = 0; col < 10; col++)
        {
            cout << ar[row][col] << "\t";
        }
        cout << endl;
    }
}

int getDivisible(int ar [][10], int size, int num )
{

    int count = 0;
    for (int row = 0; row < size; row++)
    {
        for (int col = 0; col < 10; col++)
        {
            if ((ar[row][col]) % num == 0)
                count++;
        }

    }
    return count;
}

最佳答案

getDivisible(ar, 10, count);

你不是想把 count 传进去吧?

因为当你到达这里时,在 getDivisible 函数中:

if ((ar[row][col]) % num == 0)

这是个问题,因为 getDivisible 中的 nummain 中的 count,即 0

关于c++ - 根据用户输入计算数组中的可整除数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20012388/

相关文章:

c# - 如何在 C# 项目中使用 Clang?

Java for 循环没有正确填充数组

javascript - 如何使用base64转换所有数组/对象

c# - 如何获得二维数组的唯一 ID?

c++ - 你如何正确使用 WideCharToMultiByte

c++ - 如何使用 C++ 关闭所有子线程

java - 从文本文件创建二维数组并查找平均值

java - 3D多维数组输出与初始化数据不匹配

c++ - 将 long long 除以 int 会遇到什么问题?

c++ - 模板是如何实例化的?