我正在尝试执行tranpose功能,但无法获得

I am trying to perform the tranpose function but can not get it

本文关键字:功能 tranpose 执行      更新时间:2023-10-16

我在最终的void函数上有问题,我试图将其调查为我声明为int l的变量行和列总和和toal总和的结果,我对我应该做什么或如何分开计算行或列感到困惑是问题

编写菜单驱动的C 程序,以执行以下操作在二维数组a的大小m x n上。您应该使用用户定义的功能,该功能接受2-D数组A及其大小M和N作为参数。选项是:

要输入元素中m x n的矩阵显示M X N大小的矩阵的元素大小m x n矩阵的所有元素的总和显示大小m x n的矩阵的行总和显示M X N大小的矩阵的列和创建大小n x n

的矩阵B的转置
#include<iostream>
using namespace std;
void showcases();
void input_element(int A[][20], int& m, int& n);
void display_matrix(int A[][20], int& m, int& n);
int sum(int A[][20], int& m, int& n);
int row_sum(int A[][20], int& m, int& n);
int col_sum(int A[][20], int& m, int& n);
void transp(int A[][20], int B[][20], int& m, int& n);
int main()
{
    int a = 0, b = 0, choice = 0, A[10][20], B[10][20], n = 0, x = 0, y = 0, z = 0, l = 0;
    for (int k = 0; choice <= 6; k++)
    {
        showcases();
        x = sum(A, a, b);
        y = row_sum(A, a, b);
        z = col_sum(A, a, b);
        cin >> choice;
        if (choice == 1) {
            input_element(A, a, b);
        }
        if (choice == 2) {
            display_matrix(A, a, b);
        }
        if (choice == 3) {
            cout << "The sum of the matrix elements is " << x << endl;
        }
        if (choice == 4) {
            cout << "The sum of the matrix rows is " << y << endl;
        }
        if (choice == 5) {
            cout << "The sum of the matrix columns is " << z << endl;
        }
        if (choice == 6) {
            transp(A, B, a, b);
            cout << endl;
        }
        if (choice >= 7 || choice <= 0) {
            cout << "Your choice is invalid" << endl;
        }
    }
    system("pause");
    return 0;
}
void input_element(int A[][20], int& m, int& n)
{
    int row, col;
    cout << "Enter the number of rows: ";
    cin >> m;
    cout << "Enter the number of columns: ";
    cin >> n;
    for (int r = 0; r < m; r++)
        for (int colum = 0; colum < n; colum++)
        {
            cout << "Enter the elements for the array: ";
            cin >> A[r][colum];
        }
}
void display_matrix(int A[][20], int& m, int& n)
{
    for (int r = 0; r < m; r++)
        for (int colum = 0; colum < n; colum++)
        {
            cout << "Entered element is " << ": ";
            cout << A[r][colum];
            cout << endl;
        }
}
int sum(int A[][20], int& m, int& n)
{
    int s = 0, r, colum;
    for (r = 0; r < m; r++)
    {
        for (colum = 0; colum < n; colum++)
        {
            s += A[r][colum];
        }
    }
    return s;
}
int row_sum(int A[][20], int& m, int& n)
{
    int r_s = 0, r, colum;
    for (r = 0; r < m; r++)
    {
        for (colum = 0; colum < n; colum++)
        {
            r_s += A[r][colum];
        }
    }
    return r_s;
}
int col_sum(int A[][20], int& m, int& n)
{
    int col_s(0), colum, r;
    for (r = 0; r < m; r++)
    {
        for (colum = 0; colum < n; colum++)
        {
            col_s += A[colum][r];
        }
    }
    return col_s;
}
void transp(int A[][20], int B[][20], int& m, int& n)
{
    int r, colum;
    for (r = 0; r < m; r++)
    {
        for (colum = 0; colum < n; colum++)
            B[r][colum] = A[colum][r];
    }
    for (r = 0; r < m; r++)
    {
        for (colum = 0; colum < n; colum++)
            cout << B[r][colum];
    }
    return;
}
void showcases()
{
    cout << "Menu" << endl;
    cout << "1. input elements" << endl;
    cout << "2. Display matrix" << endl;
    cout << "3. Sum of matrix elements" << endl;
    cout << "4. Sum of matrix rows" << endl;
    cout << "5. Sum of matrix elements" << endl;
    cout << "6. The transpose of elements" << endl;
    cout << "7. Exit" << endl;
    cout << "Choose among the options above: ";
}

我需要行和列总和不同,我试图修复转置部分

这是代码

 #include<iostream>
using namespace std;
// protype
void showcases();
void accept(int Array[][20], int& a, int& b);
void outputs(int Array[][20], int& a, int& b);
int sum(int Array[][20], int& a, int& b);
void row_sum(int Array[][20], int& a, int& b);
void col_sum(int Array[][20], int& a, int& b);
void transposed(int Array[][20], int& a, int& b);
int main()
{
    // variable declaration
    int a = 0, b = 0, choice = 0, A[10][20], n = 0, x = 0, y = 0, z = 0;
    // For loop execution
    for (int k = 0; choice <= 6; k++)
    {
        x = sum(A, a, b);
        showcases();
        cin >> choice;
        cout << "----------------------------------------------" << endl;
        cout << "----------------------------------------------" << endl;
        if (choice == 1) {
            accept(A, a, b);
            cout << "-----------------------------------------------------------" << endl << endl;
        }
        if (choice == 2) {
            outputs(A, a, b);
            cout << "-----------------------------------------------------------" << endl << endl;
        }
        if (choice == 3) {
            cout << "The sum of the matrix elements is " << x << endl;
            cout << "-----------------------------------------------------------" << endl << endl;
        }
        if (choice == 4) {
            col_sum(A, a, b);
            cout << "-----------------------------------------------------------" << endl << endl;
        }
        if (choice == 5) {
            row_sum(A, a, b);
            cout << "-----------------------------------------------------------" << endl << endl;
        }
        if (choice == 6) {
            cout << "Elements of matrix after transpose are: " << endl;
            transposed(A, a, b);
            cout << endl;
            cout << "-----------------------------------------------------------" << endl << endl;
        }
        if (choice >= 7 || choice <= 0) {
            cout << "Your choice is invalid" << endl;
            cout << "-----------------------------------------------------------" << endl << endl;
            return -1;
        }
    }
    system("pause");
    return 0;
}
// To input the elements 
void accept(int Array[][20], int& a, int& b)
{
    cout << "Enter  number of rows for matrix: ";
    cin >> a;
    cout << "Enter number of columns for matrix: ";
    cin >> b;
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
// to keep a track of your position in array
            cout << "Enter elements of matrix " << "[" << i << "]" << " [" << j << "]: ";
            cin >> Array[i][j];
        }
    }
}
// TO output the elements 
void outputs(int Array[][20], int& a, int& b)
{
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
            cout << Array[i][j] << " ";
        }
        cout << endl;
    }
}
// To find the total sum 
int sum(int Array[][20], int& a, int& b)
{
    int s = 0, i, j;
    for (i = 0; i < a; i++)
    {
        for (j = 0; j < b; j++)
        {
            s += Array[i][j];
        }
    }
    return s;
}
// To find the row sum
//you only need to find the row sum
void row_sum(int Array[][20], int& a, int& b)
{
    int row_s = 0;
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
            row_s += Array[i][j];
        }
        cout << "sum of" << i + 1 << " Row is " << row_s;
        row_s = 0;
        cout << endl;
    }
    return;
}

// To find the column sum 
//Same you were suming up all the elements
void col_sum(int Array[][20], int& a, int& b)
{
    int col_s = 0;
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
            col_s += Array[j][i];
        }
        cout << "sum of" << i + 1 << " column is " << col_s;
        col_s = 0;
        cout << endl;
    }
    return;
}
// To transpose (To change the elements of rows and columns) the matrix 
// you do not need to call b to store A
void transposed(int Array[][20], int& a, int& b)
{
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
            cout << Array[j][i] << " ";
        }
        cout << endl;
    }
    return;
}
// To display (To showcase) the available choiced 
void showcases()
{
    cout << "Menu" << endl;
    cout << "1. input elements" << endl;
    cout << "2. Display matrix" << endl;
    cout << "3. Sum of matrix elements" << endl;
    cout << "4. Sum of matrix rows" << endl;
    cout << "5. Sum of matrix elements" << endl;
    cout << "6. The transpose of elements" << endl;
    cout << "7. Exit" << endl;
    cout << "Choose among the options above: ";
}