【问题标题】:Adding two 2D arrays together in C++ - Why do this program crash?在 C++ 中将两个二维数组加在一起 ​​- 为什么这个程序会崩溃?
【发布时间】:2017-03-19 10:24:22
【问题描述】:

嘿,我是 C++ 编程的初学者。我制作了一个程序,旨在将两个二维数组添加在一起。但是,程序会输出这些值,直到程序崩溃。有人可以帮我找出问题吗?

#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    int a[10][10], c[10][10], i, j;
    for (i = 1; i <= 10; ++i)
    {
        for(j=0; j < 10; ++j)
        {
            a[i][j] = i * j; 
        }
    }

    // We are able to treat the individual columns as arrays
    for (int i = 0; i < 10; ++i)
    {
        int *b = a[i];
        for (int j = 0; j < 10; ++j)
        {
            cout << b[j] << " ";
        }

        cout << endl;
    }

    cout << "****" << endl;

    // Declare a multidimensional array on the heap
    int **b = new int*[10];

    // need to allocate all members individually
    for (int i = 0; i < 10; ++i)
    {
        b[i] = new int[10];
    }

    // Set the values of b
    for (int i = 0; i < 10; ++i)
    {
        for (j = 0; j < 10; ++j)
        {
            b[i][j] = (i * 10) + j; 
        }
    }

    for (i = 0; i < 10; ++i)
    {
        for (j = 1; j <= 10; ++j)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }

    for (i = 0; i < 10; ++i)
    {
        for (j = 1; j <= 10; ++j)
        {
            cout << c[i][j] << endl; 
        }
    }

    // Delete the multidimensional array - have to delete each part 
    for (int i = 0; i < 10; ++i)
    {
        delete[] b[i];
    }
    delete[] b;

    return 0; 
}

【问题讨论】:

  • for (j = 0; j &lt; 10; ++j)替换所有for (j = 1; j &lt;= 10; ++j)
  • 顺便说一句,std::transform 添加两个动态数组(又名std::vector)是一种方法,您不需要自己处理动态内存分配

标签: c++ arrays multidimensional-array sum integer


【解决方案1】:

我更正了您的代码。现在,它正在工作并且程序没有崩溃。你可以试试看。

#include<conio.h>
#include<iostream.h>

int main(int argc, char** argv)
{
    int a[10][10], c[10][10], i, j;
    for (i = 0; i <10; ++i)
    {
        for(j=0; j < 10; ++j)
        {
            a[i][j] = i * j;
        }
    }

    //We are able to treat the individual columns as arrays;
    for (i = 0; i < 10; ++i)
    {
        int *b = a[i];
        for (int j = 0; j < 10; ++j)
        {
            cout << b[j] << " ";
        }
        cout << endl;
    }
    cout << "****" << endl;
    //Declare a multidimensional array on the heap;
    int **b = new int*[10];
    //need to allocate all members individually
    for (i = 0; i < 10; ++i)
    {
        b[i] = new int[10];
    }
    //Set the values of b
    for ( i = 0; i < 10; ++i)
    {
       for (j = 0; j < 10; ++j)
       {
           b[i][j] = (i * 10) + j;
       }
    }
    for (i = 0; i < 10; ++i)
    {
        for (j = 0; j <10; ++j)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }

    for (i = 0; i < 10; ++i)
    {
        for (j = 0;j < 10; ++j)
        {
            cout << c[i][j] << " ";
        }
        cout<<endl;
    }

//  Delete the multidimensional array - have to delete each part
    for (i = 0; i < 10; ++i)
    {
        delete[] b[i];
    }
    delete[] b;
    return 0;
}

【讨论】:

  • 随时@QasimImtiaz。如果对您有帮助,请批准答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2023-03-11
  • 2021-07-23
  • 1970-01-01
相关资源
最近更新 更多