多维数组的和列

时间:2014-07-08 01:26:26

标签: c++ arrays

我是C ++的新手,目前正在学习数组。我正在尝试生成一个5x5的随机数数组,然后对第6行中每列(第1行到第5行)的元素求和。

如下:

1  2  3  4  5
1  2  3  4  5
1  2  3  4  5
1  2  3  4  5
1  2  3  4  5 
5 10 15 20 25  (this is what I'm trying to append)

我的代码目前正在输出两个独立的数组。第一个5x5数组是正确的(因为它输出随机数,但没有第6行总结上面列中的每个元素)。问题是它输出的所有0的SECOND 5x5数组。我想要实现的只是一个矩阵。

以下是代码:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

const int M = 5;
const int N = 5;

int myArray [M] [N] = {0};

void generateArray();
void sumColumns(int [] [M]);
void printSumColumns(int [] [M]);


int main()
{
    generateArray();

    sumColumns(myArray);

    return 0;
}

void generateArray()
{

    // set the seed
    unsigned setSeed = 1023;
    srand(setSeed);

    // generate the matrix using pseudo-random numbers

    for(int i=0; i < 5; i++)
    {
        for(int j=0; j < 5; j++)
        {
            myArray[M][N] = rand() % 100;

            // output the matrix
            cout << left << setw(4) << myArray[M][N] << " ";
        }
        cout << endl;
    }
}

void sumColumns(int [] [M])
{
    cout << endl;
    for (int i=0; i < 5; i++)
    {
        for(int j=0; j < M; ++j)
            myArray[i][M] += myArray[i][j];
    }

    printSumColumns(myArray);
}

void printSumColumns(int [] [N])
{
    cout << endl;
    for(int i=0; i < M; i++){
        for (int j=0; j < M; j++){
            cout << left << setw(4) << myArray[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

知道如何只追加第6行总结列元素吗?

谢谢, 莱恩

2 个答案:

答案 0 :(得分:1)

在行myArray[i][M] += myArray[i][j];中,您访问数组的边界。当大小为M时,有效索引为01,...,M-1。 (这也就是写到第6列,而你的描述说你要写到第6行)。

您不能在C ++中向数组追加行,它们必须在编译时修复大小。

此外,这些功能应该int [] [N],而不是int [] [M]。 (它只能用于M == N)。

您将获得两个数组作为输出,因为在printSumColumns函数中,您为每个条目输出一个值(请参阅如何使用两个嵌套循环,并且输出位于两者之内)。

要解决此问题,您可以:

  • 根本不使用数组(我的建议) - 使用vector
  • 将数组声明为6行5列;并使用第6行存储总和
  • 将总和存储在单独的1x5阵列中
  • 根本不存储总和,只需一次计算并打印它们

答案 1 :(得分:0)

你有几个错误:

  • 在数组的索引中。您应该myArray[m][n] = rand() myArray[i][j],但应该更加关注这些,特别是off-by-one错误。
  • 您还需要为总和添加另一个row,以便将它们添加到矩阵中。
  • 您的随机生成器不是随机的。种子是常数,这使得它总是与“随机”数字的值相同。
  • 您不需要将数组作为函数变量传递到:void generateArray(); void sumColumns(); void printSumColumns(); - 它们已在实现中使用全局变量。

这是修补后的代码

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
using namespace std;

const int M = 5;
const int N = 5;

//M+1 for the sums row
int myArray [M+1] [N] = {0};

//No need to pass any arguments since you're using global vars
void generateArray();
void sumColumns();
void printSumColumns();


int main()
{
    generateArray();    
    sumColumns();    
    return 0;
}

void generateArray()
{

    // set the seed=time for a better rand generation
    srand(time(NULL));

    // generate the matrix using pseudo-random numbers

    for(int i=0; i < M; i++)
    {
        for(int j=0; j < N; j++)
        {
            myArray[i][j] = rand() % 100;

            // output the matrix
            cout << left << setw(4) << myArray[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl<< endl;
}

void sumColumns()
{
    cout << endl;
    for (int i=0; i < 5; i++)
    {
        for(int j=0; j < M; ++j)
            myArray[M][j] += myArray[i][j];
    }

    printSumColumns();
}

void printSumColumns()
{
    cout << endl;
    //print the whole matrix with +1 row of sums.
    for(int i=0; i < M+1; i++){
        for (int j=0; j < N; j++){
            cout << left << setw(4) << myArray[i][j];
        }
        cout << endl;
    }
    cout << endl;
}