gpt4 book ai didi

C:如何将连续的数字相加,然后添加到最后?

转载 作者:行者123 更新时间:2023-11-30 17:03:43 25 4
gpt4 key购买 nike

所以我已经有了一个随机整数的二维(方形)数组,但我需要创建一个函数来将每一行相加,然后将总和放在最后一列中。有点像:(这里只是使用随机数。正如我所说,它会生成一个随机矩阵。)

1 2 3 4
5 6 7 8
9 1 2 0

需要:

1 2 3 10
5 6 7 26
9 1 2 12

到目前为止我有这个:

void newmatrix(int array[][], int sizeofmatrix)
{
int row, col;
int sum = 0;
for(col = 0; col < size; col++)
{
for(row = 0; row < size; row++)
{
sum += array[row][col];
}
}
}

我的想法正确吗?我该如何进行?

最佳答案

(假设方阵)像这样修改你的代码:

void newmatrix(int array[][], int sizeofmatrix)
{
int row,col;
int sum;
for(row = 0; row < sizeofmatrix; row++)
{
sum=0; // sum should be initialized to 0 for each row
for(col = 0; col < sizeofmatrix; col++)
{
sum += array[row][col];
}
array[row][col-1]=sum; // Add the sum in last col
}
}

关于C:如何将连续的数字相加,然后添加到最后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36045654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com