# 行の合計の結果は正しいですが、列の合計は3 x4配列に対して間違った結果をもたらします

Blazer234

``````9 1 2 4
2 2 8 0
3 3 3 3
Sum of row 0 is 16
Sum of row 1 is 12
Sum of row 2 is 12
Sum of column 0 is 14
Sum of column 1 is 6
Sum of column 2 is 13
Sum of column 3 is 7
``````

しかし、私のものは

``````Sum of row 0 is 16
Sum of row 1 is 12
Sum of row 2 is 12
Sum of column 0 is 565359518
Sum of column 1 is 32772
Sum of column 2 is 13
Sum of column 3 is 7
``````
``````#include <iostream>

using namespace std;

int main()
{

int value[3][4];
int sum = 0;
int row, col;
int i, j;

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << "Enter a value : " << endl;
cin >> value[i][j];
}
}
cout << "Value of the array: " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << value[i][j] << " ";
}

cout << endl;
}

void row_sum(int value[i][j]);
{
int i, j, sum = 0;

for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {

sum = sum + value[i][j];
}

cout
<< "Sum of the row "
<< i << " is " << sum
<< endl;

sum = 0;
}
void col_sum(int value[i][j]);
{
int i, j, sum = 0;

for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {

sum = sum + value[j][i];
}

cout
<< "Sum of the column "
<< i << " is " << sum
<< endl;

sum = 0;
}
}

return 0;
}
}
``````

（元々はi = 3でしたが、コードの問題だと思ったので4に変更しましたが、それでも列に奇妙な結果が表示されます。）

マイケル・ドーガン

``````#include <iostream>

using namespace std;

// Use Constants to declare the size of your array to make it easier to iterate in loops.
// Also allows the compiler to know at compile-time the array sizes of functions.
const int ColSize = 4;
const int RowSize = 3;

// Forward declare functions here so that main() knows what they are.
void col_sum(int value[RowSize][ColSize]);
void row_sum(int value[RowSize][ColSize]);

int main()
{
// Replace those nasty magic number 3 and 4s here with constants
int value[RowSize][ColSize];
// Notice I have removed all the unused vars here (i,j,sum, etc.)

// Replace magic numbers
for (int i = 0; i < RowSize; i++) {
for (int j = 0; j < ColSize; j++) {
cout << "Enter a value : " << endl;
cin >> value[i][j];
}
}

cout << "Value of the array: " << endl;

// Replace magic numbers
for (int i = 0; i < RowSize; i++) {
for (int j = 0; j < ColSize; j++) {
cout << value[i][j] << " ";
}

cout << endl;
}

// Actually call the functions...
row_sum(value);
col_sum(value);

return 0;
}

// Make actual functions to be called using our constants.
void row_sum(int value[RowSize][ColSize])
{
// Notice I have removed all the repeated vars here (i,j,sum, etc.)
// They are all declared within the for loops

// Replace magic numbers
for (int i = 0; i < RowSize; i++) {

int sum = 0;  // Moved the sum 0 here as we only need it once.
for (int j = 0; j < ColSize; j++) {
sum += value[i][j];
}

cout
<< "Sum of the row "
<< i << " is " << sum
<< endl;

}
}

void col_sum(int value[RowSize][ColSize])
{
// Notice I have removed all the repeated vars here (i,j,sum, etc.)
// They are all declared within the for loops

// Replace magic numbers
for (int i = 0; i < ColSize; i++) {
int sum = 0;  // Moved the sum 0 here as we only need it once.

for (int j = 0; j < RowSize; j++) {
sum = sum + value[j][i];
}

cout
<< "Sum of the column "
<< i << " is " << sum
<< endl;
}
}
``````

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

0