【问题标题】:Sum of the external elements - matrix to function C++外部元素的总和 - 函数 C++ 的矩阵
【发布时间】:2017-09-15 09:13:50
【问题描述】:

我需要创建一个计算矩阵外部元素的程序。矩阵应由用户设置。 我的想法是创建程序的第一部分,用户可以在其中编写矩阵的元素,然后将该矩阵提供给将内部元素设置为 0 的函数,然后将其返回给 main() 然后启动另一个计算所有元素总和的函数。 问题是我无法将在主 {} 中声明的矩阵传递给函数。 我几乎可以肯定我需要使用指针,但是我不知道在这种情况下如何使用它们......你能帮忙吗? 这就是我目前写的代码:

 #include <iostream>
#define MAX 10
using namespace std;
int zeroMatrix (int mat [][10], int, int);
int main()
{   //ask the user the number of rows and columns of the matrix.
    int rows,cols;
    cout << "please insert the number of rows: ";
    cin >> rows;
    cout << "insert the number of columns: ";
    cin >> cols;
    //accepting values
    int matrix [rows] [cols];
    for (int i=0; i<rows; i++)
        {
        for (int j=0; j<cols; j++)
            {
                cout <<"insert the element A(" << i <<","<< j << ") of the matrix " ;
                cin >> matrix[i][j];
            }
        }
    zeroMatrix (matrix [rows][cols]);
}

//change the previous matrix into a new matrix with the internal elements = 0

 int zeroMatrix (int mat [][10], int rows, int cols )
 {
    for (int i=1; i<rows-1; i++)
        { for (int j=1; j<cols-1 ; j++)
            {
                mat[i][j] = 0;
            }
        }
 }

【问题讨论】:

  • 一个明确的问题,一个可能的问题:C++ 没有variable-length arrays。使用std::vector 代替可移植性。而在zeroMatrix 中,为什么循环以索引1 开始并以比大小小一结束?
  • 顺便问一下,你在问什么?错误地调用zeroMatrix 应该得到的构建错误?还有什么?
  • 它从 1 开始,因为它必须改变矩阵的内部元素。所以从 1 开始到行 -1 它只改变内部。正确的?无论如何,我要求使用指针更正此代码。我无法将 main 中的矩阵传递给函数。
  • @LuigiRusso 不要在 C++ 中使用原始数组和原始指针。不如按照建议使用std::vector,或者创建自己的Matrix 课程,让您的生活更轻松。
  • @user0042 谢谢你的建议,我不知道什么是 std::vector。您能否给我一个链接以更好地理解它以及如何使用它的一些示例?我们会非常感谢谢谢你

标签: c++ function pointers matrix sum


【解决方案1】:
    #include <iostream>

    using namespace std;

    void zeroMatrix (int **mat, int, int);//makes the inner elements zero

    int sumExternals(int **mat, int rows, int cols);//sums the external elements without
                                               //using  zeroMatrix

    void printMatrix(int **mat, int, int);//prints a matrix on the console window

    int main()
    {   //ask the user the number of rows and columns of the matrix.
        int rows,cols;
        cout << "please insert the number of rows: ";
        cin >> rows;
        cout << "insert the number of columns: ";
        cin >> cols;
        //accepting values

        int **matrix = new int*[rows];

        for(int i=0; i<rows; ++i)
           matrix[i] = new int[cols];

        for (int i=0; i<rows; i++)
            {
            for (int j=0; j<cols; j++)
                {
                    cout <<"insert the element A(" << i <<","<< j << ") of the matrix " ;
                    cin >> matrix[i][j];
                }
            }

        printMatrix(matrix,rows,cols);          //print the matrix
        cout<<sumExternals(matrix,rows,cols)<<endl;  //get the sum of the external elements

        //Just so you can see how to proper call the function zeroMatrix
        zeroMatrix(matrix,rows,cols);
        printMatrix(matrix,rows,cols);
    }

    //changes the previous matrix into a new matrix with the internal elements = 0
    void zeroMatrix (int **mat, int rows, int cols )
     {
        for (int i=1; i<rows-1; i++)
             for (int j=1; j<cols-1; j++)
                mat[i][j] = 0;


     }

    //calculates the sum of the external elements of a matrix
    int sumExternals(int **mat, int rows, int cols)
    {
        int sum = 0;
        for(int j=0;j<cols;++j)
        {
            sum+=mat[0][j];         // adds the upper bound elements to sum
            sum+=mat[rows-1][j];    // adds the lower bound elements to sum
        }

        //Take caution not to add elements which are already taken into account
        for(int i=1;i<rows-1;++i)
        {
            sum+=mat[i][0];         //adds the left bound elements to sum
            sum+=mat[i][cols-1];    //adds the right bound elements to sum

        }

        return sum;
    }

    void printMatrix(int **matrix, int rows, int cols)
    {
        for(int i=0;i<rows;++i)
        {
            for(int j=0;j<cols;++j)
                cout<<matrix[i][j]<<" ";

            cout<<endl;
        }
   }

我添加了函数 sumExternals,它计算外部矩阵元素的总和,但没有使用您的 zeroMatrix 函数。

【讨论】:

    【解决方案2】:

    使用 vector 代替数组。此外,不需要两个单独的功能。一种是求外部元素之和,另一种是将内部元素设置为 0。您可以在同一个功能中实现这两项任务。请参阅下面编写的函数 external_elements_sum

    # include <iostream>
    # include <vector>
    using namespace std;
    
    int external_elements_sum(vector<vector<int> >&arr)    // function that finds sum of external elements and set internal elements to 0
    {
        int sum=0;
        for(int i=0; arr.size()>0 && i<arr[i].size();i++)
        {
            for(int j=0;j<arr[i].size();j++)
            {
                if(i==0 || i==arr.size()-1 || j==0 || j==arr[i].size()-1)  // external elements
                    sum+=arr[i][j];
                else arr[i][j]=0;     // internal elements
            }
        }
        return sum;
    }
    
    int main() {
        vector< vector<int> >arr;
        int i,j,number,rows=3,cols=4;
        for(i=0;i<rows;i++)
        {
            vector<int>v;
            for(j=0;j<cols;j++)
            {
                cin>>number;
                v.push_back(number);
            }
            arr.push_back(v);
        }
        int sum = external_elements_sum(arr);
        for(i=0;i<arr.size()>0 && arr[0].size();i++)   // arr.size()>0 to check corner case like if row is 0
        {
            for(j=0;j<arr[i].size();j++)
                cout<<arr[i][j]<<" ";
            cout<<endl;
        }
        cout<<"Sum of externale elements is: "<<sum;
        return 0;
    }
    
    Input:
    1 2 3 4
    5 6 7 8
    2 5 1 4
    
    Output:
    1 2 3 4 
    5 0 0 8 
    2 5 1 4 
    Sum of externale elements is: 35
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多