当行数和列数不匹配时,输出不正确

时间:2019-05-22 17:04:18

标签: c++

当行和列的数目匹配时,col-sum和tranpose函数可以很好地工作,但是当一个行与另一个列不同时,则不能工作

        #include<iostream>
        using namespace std;

        // protype
        void showcases();
        void accept(int Array[][40], int& a, int& b);
        void outputs(int Array[][40], int& a, int& b);
        int sum(int Array[][40], int& a, int& b);
        void row_sum(int Array[][40], int& a, int& b);
        void col_sum(int Array[][40], int& a, int& b); /// something wrong with this function. test it.
        void transposed(int Array[][40], int& a, int& b); /// something wrong with this function. test it.


        int main()
        {
            // variable declaration
            int a = 0, b = 0, choice = 0, A[20][40], n = 0, x = 0, y = 0, z = 0;

            // For loop execution
            for (int k = 0; choice != 7; k++)
            {
                //To showcase the given options 
                showcases();

                //input the user choice
                cin >> choice;

                cout << "----------------------------------------------" << endl;

                /// if else ? you don't like them? + the unreasonable empty lines between the ifs
                //To perform a certain operation when a number is selected
                if (choice == 1)
                {
                    accept(A, a, b);
                    cout << "----------------------------------------------" << endl << endl; /// remove this
                }
                else if (choice == 2)
                {
                    outputs(A, a, b);
                    cout << "----------------------------------------------" << endl << endl; /// remove this
                }
                else if (choice == 3)
                {
                    cout << "Matrix elements sum = " << sum(A, a, b) << endl;
                    cout << "----------------------------------------------" << endl << endl; /// remove this
                }
                else if (choice == 4)
                {
                    cout << "The sum of matrix rows are as following: -" << endl;
                    row_sum(A, a, b);
                    cout << "----------------------------------------------" << endl << endl; /// remove this
                }
                else if (choice == 5)
                {
                    cout << "The sum of matrix rows are as following: -" << endl;
                    col_sum(A, a, b);
                    cout << "----------------------------------------------" << endl << endl; /// remove this
                }
                else if (choice == 6)
                {
                    cout << "The transpose of the Matrix: " << endl;
                    transposed(A, a, b);
                    cout << "----------------------------------------------" << endl << endl; /// remove this
                }
                else if (choice == 7)
                {
                    cout << "The application will exit now!!" << endl;
                    cout << "----------------------------------------------" << endl << endl; /// remove this
                    return -1; /// remvoe this
                }
                else if (choice >= 8 || choice <= 0)
                {
                    cout << "Your choice is invalid" << endl;
                    cout << "----------------------------------------------" << endl << endl;
                }
                /// add something here. figure it out

                /// add something here. figure it out
            }

            /// add something here. figure it out
            system("pause");
            return 0;
        }

        // To input the elements 
        void accept(int Array[][40], int& a, int& b)
        {
            cout << "Enter  number of rows for matrix: ";
            cin >> a;
            cout << "Enter number of columns for matrix: ";
            cin >> b;

            cout << endl;

            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < b; j++)
                {
                    cout << "Enter elements of matrix " << "[" << i + 1 << "]" << " [" << j + 1 << "]: ";
                    cin >> Array[i][j];
                }
            }
        }

        // TO output the elements 
        void outputs(int Array[][40], int& a, int& b)
        {
            for (int i = 0; i < a; i++)
            {
                cout << "[";
                for (int j = 0; j < b; j++)
                {
                    cout << Array[i][j];

                    if (j != b - 1)
                        cout << " ";

                    else if (j == b - 1)
                        cout << "]";
                }
                cout << endl;
            }
        }

        // To find the total sum 
        int sum(int Array[][40], 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
        void row_sum(int Array[][40], 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 << "R" << i + 1 << " Sum" << " = " << row_s;
                row_s = 0;
                cout << endl;
            }
            return;
        }

        // To find the column sum 
        void col_sum(int Array[][40], 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 << "C" << i + 1 << " Sum" << " = " << col_s;
                col_s = 0;
                cout << endl;
            }
            return;
        }

        // To transpose (To change the elements of rows and columns) the matrix 
        void transposed(int Array[][40], int& a, int& b)
        {
            for (int i = 0; i < a; i++)
            {
                cout << "[";
                for (int j = 0; j < b; j++)
                {
                    cout << Array[j][i];

                    if (j != b - 1)
                        cout << " ";

                    else if (j == b - 1)
                        cout << "]";
                }
                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 columns" << endl;
            cout << "6. The transpose of elements" << endl;
            cout << "7. Exit" << endl;
            cout << "Choose among the options above: ";
        }


    when both the number of rows and columns are 3 this is my output when i select 2 to display the elements


[1 2 3]
[4 5 6]
[6 7 8]

and after transpose this is the new matrix
[1 4 6]
[2 5 7]
[3 6 8]

and col sum works fine
C1 Sum = 11
C2 Sum = 14
C3 Sum = 17

but if number of rows and columns differ this is the output 
Eg:  no of rows:4
 no of columns: 3

output after asking to display the output
[1 2 3]
[4 5 6]
[7 8 9]
[0 2 4]

output after the transpose
[1 4 7]
[2 5 8]
[3 6 9]
[-858993460 -858993460 -858993460]

sum of columns
C1 Sum = 12
C2 Sum = 15
C3 Sum = 18
C4 Sum = 1717986916

如果格式有误,我们将不胜感激,因为它格式不允许我上传,并且会一直告诉我我的代码未格式化或您的帖子大多为代码

1 个答案:

答案 0 :(得分:0)

现在我不再收到错误

{

// Variable Declaration
    int num1 = 0, num2 = 0, choice = 0, A[10][20], B[10][20], n = 0, x = 0;

    for (int k = 0; choice != 7; k++)
    {
        // Function Call
        showcases();

        // Input Choice Number
        cin >> choice;

        cout << "----------------------------------------------" << endl;

        // Output Choice & Function Call
        if (choice == 1)
        {
            input_element(A, num1, num2);
        }
        else if (choice == 2)
        {
            display_matrix(A, num1, num2);
        }
        else if (choice == 3)
        {
            sum(A, num1, num2);
        }
        else if (choice == 4)
        {
            row_sum(A, num1, num2);
        }
        else if (choice == 5)
        {
            col_sum(A, num1, num2);
        }
        else if (choice == 6)
        {
            cout << "The transpose of the matrix is: " << endl;
            transp(A, num1, num2);
            cout << endl;
        }
        else if (choice == 7)
        {
            cout << "The application will exit now!!" << endl;
        }
        else
        {
            cout << "Your choice is invalid" << endl;
        }

        cout << "----------------------------------------------" << endl << endl;
    }

    // Exit
    system("pause");
    return 0;
}

// Input Function
void input_element(int A[][20], int& m, int& n)
{
    cout << "Enter  number of rows for matrix: ";
    cin >> m;
    cout << "Enter number of columns for matrix: ";
    cin >> n;

    cout << endl;

    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << "Enter elements of matrix " << "[" << i + 1 << "]" << " [" << j + 1 << "]: ";
            cin >> A[i][j];
        }
    }
}

// Dispaly Matrix Function
void display_matrix(int A[][20], int& m, int& n)
{
    for (int i = 0; i < m; i++)
    {
        cout << "[";
        for (int j = 0; j < n; j++)
        {
            cout << A[i][j];

            if (j != n - 1)
                cout << " ";
            else if (j == n - 1)
                cout << "]";
        }
        cout << endl;
    }
}

// Sum Function
void sum(int A[][20], int& m, int& n)
{
    int sum = 0;

    for (int row = 0; row < m; row++)
    {
        for (int column = 0; column < n; column++)
        {
            sum += A[row][column];
        }
    }

    cout << "Matrix Elements Sum = " << sum << endl;
}

// Row Sum Function
void row_sum(int A[][20], int& m, int& n)
{
    cout << "The sum of the matrix rows are the following: -\n";

    int row_sum = 0;

    for (int i = 0; i < m; i++)
    {
        row_sum = 0;

        for (int j = 0; j < n; j++)
        {
            row_sum += A[i][j];
        }

        cout << "R" << i + 1 << " Sum = " << row_sum << endl;
    }

    return;
}

// Column Sum Function
void col_sum(int A[][20], int& m, int& n)
{
    cout << "The sum of the matrix columns are the following : -\n";

    int col_sum = 0;

    for (int j = 0; j < n; j++)
    {
        col_sum = 0;

        for (int i = 0; i < m; i++)
        {
            col_sum += A[i][j];
        }

        cout << "C" << j + 1 << " Sum = " << col_sum << endl;
    }

    return;
}

// Transpose Function
void transp(int A[][20], int& m, int& n)
{
    for (int j = 0; j < n; j++)
    {
        cout << "[";

        for (int i = 0; i < m; i++)
        {
            cout << A[i][j];

            if (i != (m - 1))
            {
                cout << " ";
            }
            else if (i == (m - 1))
            {
                cout << "]";
            }
        }

        cout << endl;
    }
}

// Showcase Menu Function
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 Columns" << endl;
    cout << "6. Transpose Of Matrix" << endl;
    cout << "7. Exit" << endl;
    cout << "Choose among the options above: ";
}
相关问题