функция транспонирования пользовательской матрицы не работает (в блоках кода) - PullRequest
0 голосов
/ 10 января 2020
#include <iostream>
#include<conio.h>

using namespace std;``

void main()
void tranpose(int [10][10],int,int,int);
{
    clrscr();
    int a[10][10],b[10][10],sze,r,c;
    cout<"Enter size of the matrix";
    cin>>sze;
    cout<<"Enter the matrix";
    for(r=0;r<sze;r++)
   {
        for(c=0;c<sze;c++)
        {
    cin>>a[r][c];
        }
    }
   cout<<"The matrix is:";
    for(r=0;r<sze;r++)
    {
        for(c=0;c<sze;c++)
        {
    cout<<a[r][c];
        }
    }
    tranpose(a[10][10],sze,r,c);

}
void tranpose(int a[10][10],sze,r,c)

{
        int r,c,sze;
         for(r=0;r<sze;r++)
    {
          for(c=0;c<sze;c++)
        {
            a[r][c]=a[c][r];
        }
    }
    cout<<"After transpose, the matrix is:";
      for(r=0;r<sze;r++)
   {
        for(c=0;c<sze;c++)
        {
            cout<<a[r][c];
        }
    }
}

получение следующих ошибок, таких как

  • ожидаемый неквалифицированный идентификатор перед '{' token
  • ожидаемым инициализатором до 'void'
  • 'sze' не был объявлен
  • 'r' не был объявлен
  • 'c' не был объявлен

работал над этим, и я до сих пор не знаю что делать.

1 Ответ

0 голосов
/ 10 января 2020

Обратите внимание, что вы запрашиваете размер матрицы во время выполнения, поэтому вы не можете использовать массив фиксированного размера вместо того, чтобы использовать динамически распределенные, а clrsrc() не является стандартным.

В следующем коде используются динамически распределенные массивы для выполнения того, что вы хотите сделать

#include <iostream>

using namespace std;


void tranpose(int** ,int);//Accepts two argss the matrix and it's size (it's squared)

void displayMat(int **, int );//To display your matrix

int main()
{
    int sze;

    //clrscr(); It's not a C++ standard function.
    cout<<"Enter size of the matrix (It's a squared one): ";
    cin>>sze;

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

    for(int i=0; i<sze; i++)
    {

        matrix[i]=new int [sze];
        for(int j = 0; j<sze; j++)
        {
            cout<<"Enter the matrix element ("<< i <<","<< j<< "): ";
            cin >> matrix[i][j];
        }
    }


    cout<<"The matrix is:\n";

    displayMat(matrix,sze);
    tranpose(matrix,sze);

    //Deleting the dynamically allocated matrix
    for(int i=0; i<sze; i++)
    {
            delete[] matrix[i];
    }

    //Displaying the transpose
    cout<<"After transpose, the matrix is:\n";
    displayMat(matrix,sze);

    delete[] matrix;
    matrix=nullptr;

    return 0;

}


void displayMat(int ** matrix, int sze)
{
    for(int i=0; i<sze; i++)
    {   

        for(int j=0; j<sze; j++)
        {
            cout<<matrix[i][j]<<"\t";
        }
        cout<<"\n";
    }

}
void tranpose(int** matrix,int sze)

{
    //Copying the matrix to a temp one 
    //You need this because if you change some element in your matrix, you
    //won't be able to use it again 
    int** tempMatrix = new int*[sze];

    for(int i=0; i<sze; i++)
    {

        tempMatrix[i]=new int [sze];
        for(int j = 0; j<sze; j++)
        {
            tempMatrix[i][j] = matrix[i][j];
        }
    }


    //Now transpose
    for(int i=0; i<sze; i++)
    {
        for(int j=0; j<sze; j++)
        {
            matrix[i][j] = tempMatrix[j][i];
        }

    }

    //Deleting the dynamically allocated tempMatrix
    for(int i=0; i<sze; i++)
    {
            delete[] tempMatrix[i];
    }
    delete[] tempMatrix;
    tempMatrix=nullptr;

}

Но в современном CPP вам не следует использовать массивы, а использовать векторы, как в следующем коде

#include <iostream>
#include<vector>

using namespace std;


void tranpose(vector<vector<int>>&);//Accepts the matrix and the size can
                                    //be known using size() function

void displayMat(const vector<vector<int>>&);

int main()
{
    int sze;

    cout<<"Enter size of the matrix (It's a squared one): ";
    cin>>sze;

    vector<vector<int>> matrix;
    for(int i=0; i<sze; i++)
    {
        vector<int> ithRow;
        for(int j = 0; j<sze; j++)
        {
            cout<<"Enter the matrix element ("<< i <<","<< j<< "): ";
            int tempInt;
            cin >>tempInt;
            ithRow.push_back(tempInt);//Entring the element
        }
        matrix.push_back(ithRow);//Entring the ith row in the matrix
    }


    displayMat(matrix);


    tranpose(matrix);

    //Displaying the transpose
    cout<<"After transpose\n";
    displayMat(matrix);

    return 0;

}

void displayMat(const vector<vector<int>>& matrix)
{
    cout<<"\nThe matrix is:\n";
    int sze=matrix.size();

    for(int i=0; i<sze; i++)
    {
        for(int j=0; j < sze; j++)
        {
            cout << matrix[i][j]<<"\t";
        }
        cout<<"\n";
    }

}


void tranpose(vector<vector<int>>& matrix)
{
    //Copying the matrix to a temp one 
    //You need this because if you change some element in your matrix, you
    //won't be able to use it again 
    vector<vector<int>> tempMatrix = matrix;
    int sze = matrix.size();

    for(int i=0; i<sze; i++)
    {
        for(int j = 0; j<sze; j++)
        {
            tempMatrix[i][j] = matrix[i][j];
        }
    }


    //Now transpose
    for(int i=0; i<sze; i++)
    {
        for(int j=0; j<sze; j++)
        {
            matrix[i][j] = tempMatrix[j][i];
        }

    }
}
...