C++ 将2D数组传递给函数并将其乘以2
我试图通过这个数组,然后将它乘以2。我的代码可以工作,但它将其乘以4,我认为这是因为另一个for循环中的for循环。有人能解释一下如何解决这个问题吗 5x5阵列:C++ 将2D数组传递给函数并将其乘以2,c++,c++11,C++,C++11,我试图通过这个数组,然后将它乘以2。我的代码可以工作,但它将其乘以4,我认为这是因为另一个for循环中的for循环。有人能解释一下如何解决这个问题吗 5x5阵列: int numbers [5][5] = { { 1, 3, 5, 7, 9}, { -2, -4, -6, -8, -10}, { 3, 3, 3, 3, 3},
int numbers [5][5] = { { 1, 3, 5, 7, 9},
{ -2, -4, -6, -8, -10},
{ 3, 3, 3, 3, 3},
{ 55, 77, 99, 22, 33},
{-15, -250, -350, -450, -550} };
// function
int multiply_bytwo(int n[5][5])
{
int total_times = 0;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
n[row][col] = n[row][col] * 2;
}
return total_times;
}
}
整数[5][5]={{1,3,5,7,9},
{ -2, -4, -6, -8, -10},
{ 3, 3, 3, 3, 3},
{ 55, 77, 99, 22, 33},
{-15, -250, -350, -450, -550} };
//作用
整数乘以二(整数n[5][5])
{
int total_times=0;
对于(int行=0;行<5;行++)
{
for(int col=0;col<5;col++)
{
n[行][col]=n[行][col]*2;
}
返回总次数;
}
}
当前您的返回总次数
只返回0,
我假设您想计算循环运行的次数,这将使用total_times++代码>
移动循环的返回端会使函数返回最终所需的数组
我建议在许多位置为类似问题添加打印,以便您可以在运行时调试问题,例如:
expected = VALUE
actual = VALUE
这会帮助你发现哪里出了问题
int numbers [5][5] = { { 1 , 3, 5, 7, 9},
{ -2, -4, -6, -8, -10},
{ 3, 3, 3, 3, 3},
{ 55, 77, 99, 22, 33},
{-15,-250,-350,-450,-550}
};
// function
int multiply_bytwo(int n[5][5])
{
int total_times = 0;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
n[row][col] = n[row][col] * 2;
total_times ++;
}
}
return total_times;
}
整数[5][5]={{1,3,5,7,9},
{ -2, -4, -6, -8, -10},
{ 3, 3, 3, 3, 3},
{ 55, 77, 99, 22, 33},
{-15,-250,-350,-450,-550}
};
//作用
整数乘以二(整数n[5][5])
{
int total_times=0;
对于(int行=0;行<5;行++)
{
for(int col=0;col<5;col++)
{
n[行][col]=n[行][col]*2;
总_次++;
}
}
返回总次数;
}
试试这个:
// function
int multiply_bytwo(int n[5][5])
{
int total_times = 0;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
n[row][col] = n[row][col] * 2;
total_times++;
}
}
return total_times;
}
//函数
整数乘以二(整数n[5][5])
{
int total_times=0;
对于(int行=0;行<5;行++)
{
for(int col=0;col<5;col++)
{
n[行][col]=n[行][col]*2;
总_次++;
}
}
返回总次数;
}
我将您的代码制作成一个MCV示例:
#include <stdio.h>
// function
int multiply_bytwo(int n[5][5])
{
int total_times = 0;
for (int row = 0; row < 5; row++)
{
printf("\n");
for (int col = 0; col < 5; col++)
{
n[row][col] = n[row][col] * 2;
printf("%d\n", n[row][col]);
}
return total_times;
}
}
int main(int argc, char *argv[])
{
int numbers [5][5] = { { 1,3,5,7,9},{-2,-4,-6, -8, -10},{3,3,3,3,3},{ 55, 77, 99, 22, 33 },{ -15, -250, -350, -450, -550 } };
multiply_bytwo(numbers);
}
我们在这里看到了什么?第一行乘以2({1,3,5,7,9}
->{2,6,10,14,18}
),然后函数退出
当我们将return语句移出循环时,结果是什么
代码:
我冒昧地手动调整了输出,但数字完全符合您的要求
我不知道你从哪里得到的*4
您实际上也不会返回任何内容—您在开始时设置了total_times=0
,并且从不修改它。您可能打算在执行乘法时增加它。返回total\u次代码>提前退出循环。但它会将循环乘以4。。。你怎么知道的???请编辑你的问题并给我们一个。@πάνταῥεῖ 所以当我移动return total_times时,除了现在所有的都乘以4外,没有任何变化。而不是2@CowboyCoder你能解释一下你是如何知道你的二维数组是乘以4而不是2的吗?谢谢!我的完整程序有一个菜单(do循环),我没有注意到我忘记了其中一个选项的中断。“它现在起作用了。”牛仔编码器如果对你有帮助,请随意投票。如果这是最有用的答案,请接受。我接受了,但由于我的代表不够高,我的选票没有显示出来。再次感谢你。
$ gcc temp.c -std=c99 && ./a.exe
2 6 10 14 18
int multiply_bytwo(int n[5][5])
{
int total_times = 0;
for (int row = 0; row < 5; row++){
printf("\n");
for (int col = 0; col < 5; col++){
n[row][col] = n[row][col] * 2;
printf("%d ", n[row][col]);
}
}
return total_times;
}
2 6 10 14 18
-4 -8 -12 -16 -20
6 6 6 6 6
110 154 198 44 66
-30 -500 -700 -900 -1100