- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想获取数组的每一行和每一列的总和,然后用C一张一张地打印出来。我完成了一半的工作,但无法继续进行下去。这是我的代码:
int n,m;
void sum_row_column(int array[n][m],int r,int c,int i,int j)
{
int sum_of_column1=0;
int sum_of_row1;
for(j=0,i=0;i<r;i++)
{
sum_of_column1=sum_of_column1+array[i][j];
if(i==r-1)
{
printf("\nSum of %d.column: %d\n",j+1,sum_of_column1);
j=j+1;
}
}
for(i=0,j=0;j<r;j++)
{
sum_of_row1=sum_of_row1+array[i][j];
if(j=c-1)
{
printf("\nSum of %d.row: %d\n",j+1,sum_of_row1);
i=i+1;
}
}
i=0;
printf("\nSum of %d.column: %d\n",j+1,sum_of_column1);
j=j+1
i=0;
最佳答案
任务的建议代码
你有:
int n,m;
void sum_row_column(int array[n][m],int r,int c,int i,int j)
{
n
和
m
设置不正确,则不必要地导致失败。不需要该级别的耦合,并且如果您确实使用该级别的耦合,则变量
r
和
c
是多余的。您应该使用:
void print_sum_rows_cols(int r, int c, int array[r][c])
{
n
和
m
应该是不必要的;参数
i
和
j
似乎也毫无意义,因为您忽略了传递给函数的值,并在循环开始时将它们都设置为
0
。
for (int col = 0; col < c; col++)
{
int colsum = 0;
for (int row = 0; row < r; row++)
colsum += array[row][col];
printf("Sum for col %d = %d\n", col, colsum);
}
for (int row = 0; row < r; row++)
{
int rowsum = 0;
for (int col = 0; col < c; col++)
rowsum += array[row][col];
printf("Sum for row %d = %d\n", row, rowsum);
}
#include <stdio.h>
static void print_sum_rows_cols(int r, int c, int array[r][c])
{
for (int col = 0; col < c; col++)
{
int colsum = 0;
for (int row = 0; row < r; row++)
colsum += array[row][col];
printf("Sum for col %d = %d\n", col, colsum);
}
for (int row = 0; row < r; row++)
{
int rowsum = 0;
for (int col = 0; col < c; col++)
rowsum += array[row][col];
printf("Sum for row %d = %d\n", row, rowsum);
}
}
static void dump_matrix(const char *tag, int rows, int cols, int matrix[rows][cols])
{
printf("%s (%dx%d):\n", tag, rows, cols);
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
printf("%3d", matrix[r][c]);
putchar('\n');
}
}
int main(void)
{
int a1[3][4] =
{
{ 68, 78, 50, 46, },
{ 64, 12, 47, 1, },
{ 86, 10, 84, 62, },
};
int a2[5][3] =
{
{ 4, 30, 19, },
{ 79, 58, 20, },
{ 95, 12, 24, },
{ 20, 37, 72, },
{ 17, 0, 53, },
};
dump_matrix("A1", 3, 4, a1);
print_sum_rows_cols(3, 4, a1);
dump_matrix("A2", 5, 3, a2);
print_sum_rows_cols(5, 3, a2);
return 0;
}
print_sum_rows_cols()
函数中的代码保留了问题的变量名,尽管它们不是我在自己的代码中使用的名称。 (对于这些,请参阅“其他答案的分析”部分。)
A1 (3x4):
68 78 50 46
64 12 47 1
86 10 84 62
Sum for col 0 = 218
Sum for col 1 = 100
Sum for col 2 = 181
Sum for col 3 = 109
Sum for row 0 = 242
Sum for row 1 = 124
Sum for row 2 = 242
A2 (5x3):
4 30 19
79 58 20
95 12 24
20 37 72
17 0 53
Sum for col 0 = 215
Sum for col 1 = 137
Sum for col 2 = 188
Sum for row 0 = 53
Sum for row 1 = 157
Sum for row 2 = 131
Sum for row 3 = 129
Sum for row 4 = 70
int n;
int m;
void sum_row_column(int array[n][m], int r, int c, int i, int j)
{
int sumOfColumn = 0;
int sumOfRow = 0;
printf("\n");
for (i = 0; i < r; i++)
{
sumOfRow = 0;
for (j = 0; j < c; j++)
{
sumOfRow += array[i][j];
}
printf("Sum of %d.row=%d\n", i + 1, sumOfRow);
}
printf("\n");
for (j = 0; j < r; j++)
{
sumOfColumn = 0;
for (i = 0; i < c; i++)
{
sumOfColumn += array[i][j];
}
printf("Sum of %d.column=%d\n", j + 1, sumOfColumn);
}
}
n
和
m
中的值标识。
r
和
c
标识。
i
和
j
中的值未使用;它们只是定义两个局部循环变量的一种特殊方式。
assert(n == r && m == c);
,则第一对嵌套循环(对行进行求和)就可以了。
assert(n == r && m == c && n == m):
(方阵),第二对嵌套循环(将列求和)是可以的,但这主要是偶然的而不是设计。
r
x
c
,但是该函数假定数组的大小为
m
x
n
。
bogus_sum_rows_cols()
)还有一个'假'变体,它使用
n
和
m
变量来调整数组的大小;当
n
和
m
与
r
和
c
不同步时,它将产生错误的答案。提议的答案(
bogus_sum_row_column()
)还有一个'假'变体,如果
r
和
c
中的数组大小与
n
和
m
都匹配,则至少可以正确地对列求和。
/* SO 47719-9719 - Abuse of VLA definitions (and notations) */
#include <stdio.h>
static int debug = 0;
// Original answer (renamed variables, reordered results, and print row/col + 1)
static void print_sum_rows_cols(int rows, int cols, int array[rows][cols])
{
for (int r = 0; r < rows; r++)
{
int rowsum = 0;
for (int c = 0; c < cols; c++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
rowsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for row %d = %d\n", r + 1, rowsum);
}
putchar('\n');
for (int c = 0; c < cols; c++)
{
int colsum = 0;
for (int r = 0; r < rows; r++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
colsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for col %d = %d\n", c + 1, colsum);
}
}
static int n; // Rows
static int m; // Columns
// Adapted print_sum_rows_cols with size from n, m
// Using n, m is unnecessary coupling (and can lead to erroneous results)
static void bogus_sum_rows_cols(int array[n][m], int rows, int cols)
{
for (int r = 0; r < rows; r++)
{
int rowsum = 0;
for (int c = 0; c < cols; c++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
rowsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for row %d = %d\n", r, rowsum);
}
putchar('\n');
for (int c = 0; c < cols; c++)
{
int colsum = 0;
for (int r = 0; r < rows; r++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
colsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for col %d = %d\n", c, colsum);
}
}
// Original answer from OP - bogus results (debug printing added).
// Using n, m is unnecessary coupling (and can lead to erroneous results)
// Using i, j as arguments is bogus; the values passed in are ignored
// The 'sum column' loops are completely bogus. The outer loop should
// iterate over columns [0..c) and the inner loop over rows [0..r).
// See print_sum_rows_cols() for the correct (minimal) interface.
static void sum_row_column(int array[n][m], int r, int c, int i, int j)
{
int sumOfColumn = 0;
int sumOfRow = 0;
printf("\n");
for (i = 0; i < r; i++)
{
sumOfRow = 0;
for (j = 0; j < c; j++)
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfRow += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.row=%d\n", i + 1, sumOfRow);
}
printf("\n");
for (j = 0; j < r; j++)
{
sumOfColumn = 0;
for (i = 0; i < c; i++)
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfColumn += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.column=%d\n", j + 1, sumOfColumn);
}
}
// Semi-fixed answer from OP - bogus results (debug printing added).
// Using n, m is unnecessary coupling (and can lead to erroneous results)
// Using i, j as arguments is bogus; the values passed in are ignored
// The 'sum column' loops are completely bogus. The outer loop should
// iterate over columns [0..c) and the inner loop over rows [0..r).
// See print_sum_rows_cols() for the correct (minimal) interface.
static void bogus_sum_row_column(int array[n][m], int r, int c, int i, int j)
{
int sumOfColumn = 0;
int sumOfRow = 0;
printf("\n");
for (i = 0; i < r; i++)
{
sumOfRow = 0;
for (j = 0; j < c; j++)
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfRow += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.row=%d\n", i + 1, sumOfRow);
}
printf("\n");
for (j = 0; j < c; j++) // c, not r
{
sumOfColumn = 0;
for (i = 0; i < r; i++) // r, not c
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfColumn += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.column=%d\n", j + 1, sumOfColumn);
}
}
static void dump_matrix(const char *tag, int rows, int cols, int matrix[rows][cols])
{
printf("Matrix %s (%dx%d):\n", tag, rows, cols);
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
printf("%3d", matrix[r][c]);
putchar('\n');
}
}
static void test_sequence(int v_debug, int v_n, int v_m, const char *tag,
int rows, int cols, int matrix[rows][cols])
{
debug = v_debug;
n = v_n;
m = v_m;
dump_matrix(tag, rows, cols, matrix);
printf("\nprint_sum_rows_cols():\n");
print_sum_rows_cols(rows, cols, matrix);
printf("\nbogus_sum_rows_cols() - (n = %d, m = %d):\n", n, m);
bogus_sum_rows_cols(matrix, rows, cols);
printf("\nsum_row_column()(n = %d, m = %d):\n", n, m);
sum_row_column(matrix, rows, cols, -1, -1);
printf("\nbogus_sum_row_column()(n = %d, m = %d):\n", n, m);
bogus_sum_row_column(matrix, rows, cols, -1, -1);
putchar('\n');
}
static void test_matrix_summation(const char *tag, int rows, int cols, int matrix[rows][cols])
{
test_sequence(1, 2, 2, tag, rows, cols, matrix);
test_sequence(0, rows, cols, tag, rows, cols, matrix);
}
int main(void)
{
int a1[3][4] =
{
{ 68, 78, 50, 46, },
{ 64, 12, 47, 1, },
{ 86, 10, 84, 62, },
};
int a2[5][3] =
{
{ 4, 30, 19, },
{ 79, 58, 20, },
{ 95, 12, 24, },
{ 20, 37, 72, },
{ 17, 0, 53, },
};
int a3[3][3] =
{
{ 96, 84, 13, },
{ 63, 29, 80, },
{ 97, 98, 48, },
};
test_matrix_summation("A1", 3, 4, a1);
test_matrix_summation("A2", 5, 3, a2);
test_matrix_summation("A3", 3, 3, a3);
return 0;
}
Matrix A1 (3x4):
68 78 50 46
64 12 47 1
86 10 84 62
print_sum_rows_cols():
a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum for row 1 = 242
a[1][0] = 64 a[1][1] = 12 a[1][2] = 47 a[1][3] = 1
Sum for row 2 = 124
a[2][0] = 86 a[2][1] = 10 a[2][2] = 84 a[2][3] = 62
Sum for row 3 = 242
a[0][0] = 68 a[1][0] = 64 a[2][0] = 86
Sum for col 1 = 218
a[0][1] = 78 a[1][1] = 12 a[2][1] = 10
Sum for col 2 = 100
a[0][2] = 50 a[1][2] = 47 a[2][2] = 84
Sum for col 3 = 181
a[0][3] = 46 a[1][3] = 1 a[2][3] = 62
Sum for col 4 = 109
bogus_sum_rows_cols() - (n = 2, m = 2):
a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum for row 0 = 242
a[1][0] = 50 a[1][1] = 46 a[1][2] = 64 a[1][3] = 12
Sum for row 1 = 172
a[2][0] = 64 a[2][1] = 12 a[2][2] = 47 a[2][3] = 1
Sum for row 2 = 124
a[0][0] = 68 a[1][0] = 50 a[2][0] = 64
Sum for col 0 = 182
a[0][1] = 78 a[1][1] = 46 a[2][1] = 12
Sum for col 1 = 136
a[0][2] = 50 a[1][2] = 64 a[2][2] = 47
Sum for col 2 = 161
a[0][3] = 46 a[1][3] = 12 a[2][3] = 1
Sum for col 3 = 59
sum_row_column()(n = 2, m = 2):
a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum of 1.row=242
a[1][0] = 50 a[1][1] = 46 a[1][2] = 64 a[1][3] = 12
Sum of 2.row=172
a[2][0] = 64 a[2][1] = 12 a[2][2] = 47 a[2][3] = 1
Sum of 3.row=124
a[0][0] = 68 a[1][0] = 50 a[2][0] = 64 a[3][0] = 47
Sum of 1.column=229
a[0][1] = 78 a[1][1] = 46 a[2][1] = 12 a[3][1] = 1
Sum of 2.column=137
a[0][2] = 50 a[1][2] = 64 a[2][2] = 47 a[3][2] = 86
Sum of 3.column=247
bogus_sum_row_column()(n = 2, m = 2):
a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum of 1.row=242
a[1][0] = 50 a[1][1] = 46 a[1][2] = 64 a[1][3] = 12
Sum of 2.row=172
a[2][0] = 64 a[2][1] = 12 a[2][2] = 47 a[2][3] = 1
Sum of 3.row=124
a[0][0] = 68 a[1][0] = 50 a[2][0] = 64
Sum of 1.column=182
a[0][1] = 78 a[1][1] = 46 a[2][1] = 12
Sum of 2.column=136
a[0][2] = 50 a[1][2] = 64 a[2][2] = 47
Sum of 3.column=161
a[0][3] = 46 a[1][3] = 12 a[2][3] = 1
Sum of 4.column=59
Matrix A1 (3x4):
68 78 50 46
64 12 47 1
86 10 84 62
print_sum_rows_cols():
Sum for row 1 = 242
Sum for row 2 = 124
Sum for row 3 = 242
Sum for col 1 = 218
Sum for col 2 = 100
Sum for col 3 = 181
Sum for col 4 = 109
bogus_sum_rows_cols() - (n = 3, m = 4):
Sum for row 0 = 242
Sum for row 1 = 124
Sum for row 2 = 242
Sum for col 0 = 218
Sum for col 1 = 100
Sum for col 2 = 181
Sum for col 3 = 109
sum_row_column()(n = 3, m = 4):
Sum of 1.row=242
Sum of 2.row=124
Sum of 3.row=242
Sum of 1.column=222
Sum of 2.column=130
Sum of 3.column=200
bogus_sum_row_column()(n = 3, m = 4):
Sum of 1.row=242
Sum of 2.row=124
Sum of 3.row=242
Sum of 1.column=218
Sum of 2.column=100
Sum of 3.column=181
Sum of 4.column=109
Matrix A2 (5x3):
4 30 19
79 58 20
95 12 24
20 37 72
17 0 53
print_sum_rows_cols():
a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum for row 1 = 53
a[1][0] = 79 a[1][1] = 58 a[1][2] = 20
Sum for row 2 = 157
a[2][0] = 95 a[2][1] = 12 a[2][2] = 24
Sum for row 3 = 131
a[3][0] = 20 a[3][1] = 37 a[3][2] = 72
Sum for row 4 = 129
a[4][0] = 17 a[4][1] = 0 a[4][2] = 53
Sum for row 5 = 70
a[0][0] = 4 a[1][0] = 79 a[2][0] = 95 a[3][0] = 20 a[4][0] = 17
Sum for col 1 = 215
a[0][1] = 30 a[1][1] = 58 a[2][1] = 12 a[3][1] = 37 a[4][1] = 0
Sum for col 2 = 137
a[0][2] = 19 a[1][2] = 20 a[2][2] = 24 a[3][2] = 72 a[4][2] = 53
Sum for col 3 = 188
bogus_sum_rows_cols() - (n = 2, m = 2):
a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum for row 0 = 53
a[1][0] = 19 a[1][1] = 79 a[1][2] = 58
Sum for row 1 = 156
a[2][0] = 58 a[2][1] = 20 a[2][2] = 95
Sum for row 2 = 173
a[3][0] = 95 a[3][1] = 12 a[3][2] = 24
Sum for row 3 = 131
a[4][0] = 24 a[4][1] = 20 a[4][2] = 37
Sum for row 4 = 81
a[0][0] = 4 a[1][0] = 19 a[2][0] = 58 a[3][0] = 95 a[4][0] = 24
Sum for col 0 = 200
a[0][1] = 30 a[1][1] = 79 a[2][1] = 20 a[3][1] = 12 a[4][1] = 20
Sum for col 1 = 161
a[0][2] = 19 a[1][2] = 58 a[2][2] = 95 a[3][2] = 24 a[4][2] = 37
Sum for col 2 = 233
sum_row_column()(n = 2, m = 2):
a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum of 1.row=53
a[1][0] = 19 a[1][1] = 79 a[1][2] = 58
Sum of 2.row=156
a[2][0] = 58 a[2][1] = 20 a[2][2] = 95
Sum of 3.row=173
a[3][0] = 95 a[3][1] = 12 a[3][2] = 24
Sum of 4.row=131
a[4][0] = 24 a[4][1] = 20 a[4][2] = 37
Sum of 5.row=81
a[0][0] = 4 a[1][0] = 19 a[2][0] = 58
Sum of 1.column=81
a[0][1] = 30 a[1][1] = 79 a[2][1] = 20
Sum of 2.column=129
a[0][2] = 19 a[1][2] = 58 a[2][2] = 95
Sum of 3.column=172
a[0][3] = 79 a[1][3] = 20 a[2][3] = 12
Sum of 4.column=111
a[0][4] = 58 a[1][4] = 95 a[2][4] = 24
Sum of 5.column=177
bogus_sum_row_column()(n = 2, m = 2):
a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum of 1.row=53
a[1][0] = 19 a[1][1] = 79 a[1][2] = 58
Sum of 2.row=156
a[2][0] = 58 a[2][1] = 20 a[2][2] = 95
Sum of 3.row=173
a[3][0] = 95 a[3][1] = 12 a[3][2] = 24
Sum of 4.row=131
a[4][0] = 24 a[4][1] = 20 a[4][2] = 37
Sum of 5.row=81
a[0][0] = 4 a[1][0] = 19 a[2][0] = 58 a[3][0] = 95 a[4][0] = 24
Sum of 1.column=200
a[0][1] = 30 a[1][1] = 79 a[2][1] = 20 a[3][1] = 12 a[4][1] = 20
Sum of 2.column=161
a[0][2] = 19 a[1][2] = 58 a[2][2] = 95 a[3][2] = 24 a[4][2] = 37
Sum of 3.column=233
Matrix A2 (5x3):
4 30 19
79 58 20
95 12 24
20 37 72
17 0 53
print_sum_rows_cols():
Sum for row 1 = 53
Sum for row 2 = 157
Sum for row 3 = 131
Sum for row 4 = 129
Sum for row 5 = 70
Sum for col 1 = 215
Sum for col 2 = 137
Sum for col 3 = 188
bogus_sum_rows_cols() - (n = 5, m = 3):
Sum for row 0 = 53
Sum for row 1 = 157
Sum for row 2 = 131
Sum for row 3 = 129
Sum for row 4 = 70
Sum for col 0 = 215
Sum for col 1 = 137
Sum for col 2 = 188
sum_row_column()(n = 5, m = 3):
Sum of 1.row=53
Sum of 2.row=157
Sum of 3.row=131
Sum of 4.row=129
Sum of 5.row=70
Sum of 1.column=178
Sum of 2.column=100
Sum of 3.column=63
Sum of 4.column=194
Sum of 5.column=107
bogus_sum_row_column()(n = 5, m = 3):
Sum of 1.row=53
Sum of 2.row=157
Sum of 3.row=131
Sum of 4.row=129
Sum of 5.row=70
Sum of 1.column=215
Sum of 2.column=137
Sum of 3.column=188
Matrix A3 (3x3):
96 84 13
63 29 80
97 98 48
print_sum_rows_cols():
a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum for row 1 = 193
a[1][0] = 63 a[1][1] = 29 a[1][2] = 80
Sum for row 2 = 172
a[2][0] = 97 a[2][1] = 98 a[2][2] = 48
Sum for row 3 = 243
a[0][0] = 96 a[1][0] = 63 a[2][0] = 97
Sum for col 1 = 256
a[0][1] = 84 a[1][1] = 29 a[2][1] = 98
Sum for col 2 = 211
a[0][2] = 13 a[1][2] = 80 a[2][2] = 48
Sum for col 3 = 141
bogus_sum_rows_cols() - (n = 2, m = 2):
a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum for row 0 = 193
a[1][0] = 13 a[1][1] = 63 a[1][2] = 29
Sum for row 1 = 105
a[2][0] = 29 a[2][1] = 80 a[2][2] = 97
Sum for row 2 = 206
a[0][0] = 96 a[1][0] = 13 a[2][0] = 29
Sum for col 0 = 138
a[0][1] = 84 a[1][1] = 63 a[2][1] = 80
Sum for col 1 = 227
a[0][2] = 13 a[1][2] = 29 a[2][2] = 97
Sum for col 2 = 139
sum_row_column()(n = 2, m = 2):
a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum of 1.row=193
a[1][0] = 13 a[1][1] = 63 a[1][2] = 29
Sum of 2.row=105
a[2][0] = 29 a[2][1] = 80 a[2][2] = 97
Sum of 3.row=206
a[0][0] = 96 a[1][0] = 13 a[2][0] = 29
Sum of 1.column=138
a[0][1] = 84 a[1][1] = 63 a[2][1] = 80
Sum of 2.column=227
a[0][2] = 13 a[1][2] = 29 a[2][2] = 97
Sum of 3.column=139
bogus_sum_row_column()(n = 2, m = 2):
a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum of 1.row=193
a[1][0] = 13 a[1][1] = 63 a[1][2] = 29
Sum of 2.row=105
a[2][0] = 29 a[2][1] = 80 a[2][2] = 97
Sum of 3.row=206
a[0][0] = 96 a[1][0] = 13 a[2][0] = 29
Sum of 1.column=138
a[0][1] = 84 a[1][1] = 63 a[2][1] = 80
Sum of 2.column=227
a[0][2] = 13 a[1][2] = 29 a[2][2] = 97
Sum of 3.column=139
Matrix A3 (3x3):
96 84 13
63 29 80
97 98 48
print_sum_rows_cols():
Sum for row 1 = 193
Sum for row 2 = 172
Sum for row 3 = 243
Sum for col 1 = 256
Sum for col 2 = 211
Sum for col 3 = 141
bogus_sum_rows_cols() - (n = 3, m = 3):
Sum for row 0 = 193
Sum for row 1 = 172
Sum for row 2 = 243
Sum for col 0 = 256
Sum for col 1 = 211
Sum for col 2 = 141
sum_row_column()(n = 3, m = 3):
Sum of 1.row=193
Sum of 2.row=172
Sum of 3.row=243
Sum of 1.column=256
Sum of 2.column=211
Sum of 3.column=141
bogus_sum_row_column()(n = 3, m = 3):
Sum of 1.row=193
Sum of 2.row=172
Sum of 3.row=243
Sum of 1.column=256
Sum of 2.column=211
Sum of 3.column=141
main()
程序中定义的矩阵的行和列的实际总和有关。
n
和
m
(并且既不需要
r
也
c
)。但是您不应该将函数与所有全局变量联系在一起。您应该定义
r
和
c
(如果矩阵必须为正方形,则只能定义其中之一)。
n
和
m
中的值未记录在问题或建议的答案中,因此有必要弄清楚它们表示的含义。
关于c - 二维数组(C)中每一行和每一列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47719719/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!