【问题标题】:Can Someone Help Me Troubleshoot This Program (It Transposes A Matrix Using Pointers)谁能帮我解决这个程序的问题(它使用指针转置矩阵)
【发布时间】:2023-03-18 09:35:02
【问题描述】:

我有一个我制作的程序,它应该转置一个矩阵,不管它是什么大小。但是它并没有像预期的那样工作,我不知道为什么,我确实从中得到输出,没有编译错误,但是 {1,2,3},{4,5,6} 的输出是 { 1,0,0},{0,0,4},这对我来说完全没有意义。我已经在纸上研究过很多次“记忆快照”,但就是找不到我缺少的东西,我现在真的只需要另一双眼睛,谢谢。

#include "stdafx.h"

#include <fstream>

#include <iostream>

#include <iomanip>

#include <string>

#include <cmath>

#include <vector>

using namespace std;

void squaretranspose(int &M, int &MT, int ROWS, int COLS);

int main(void)
{
    int M[2][3]={{1,2,3},{4,5,6}};
    int MT[3][2]={0};
    int ROWS(2),COLS(3);

int i,j;
cout << " The entries of the original matrix " << endl;
for(i=0;i<=ROWS-1;i++)
{
    for(j=0;j<=COLS-1;j++)
{
cout<<M[i][j]<<"\t";
}
cout << endl;
}
squaretranspose(M[0][0],MT[0][0],ROWS,COLS);
cout << " The entries of the transposed non-square matrix " << endl;
for(i=0;i<=COLS-1;i++)
{
    cout << endl;
    for(j=0;j<=ROWS-1;j++)
{
cout<<MT[i][j]<<"\t";
}
}
system ("PAUSE");
return 0;
}
void squaretranspose (int &M, int &MT, int ROWS, int COLS)
{
    // declare pointers to change the input matrice's values
    int *ptr,*ptrT;
// declare indices for a row by row process
    int i,j;
// declare placeholder 2d vectors for swapping the I,j, entries to ,j,i entries  
    vector < vector<int>> temp(ROWS,COLS);
    vector < vector<int>> tempT(COLS,COLS);
    vector < vector<int>> temp_T(ROWS,ROWS);
// set the pointers to point to the first entry of the input and output matrices
    ptr = &M;
    ptrT = &MT;

// if rows=cols we want to use 2d vector temp
    if (ROWS=COLS)
    {
// store all of the input matrice's values in the 2d vector "temp"
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {

// set the i,j th entry of the 2d vector "temp" equal to the value currently pointed to by the pointer
        temp[i][j]=*ptr;
// increment the pointer to the address of the next entry of the input matrix unless we are on the last entry
               if ((i!=ROWS-1)&&(j!=COLS-1))
               { 
               ptr++;
               }
        }
        }
    }
// reset pointer address to first entry
    ptr=&M;
// the for loop for swapping the j,i entries of the 2d vector "temp" with the i,j entries of the input matrix
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {
// if j is not equal to i swap the value pointed to by the pointer (the i,j entry of the input matrix) for the j,ith entries value of the 2d vector "temp"
        if (j!=i)
            {
            *ptr=temp[j][i];
            }
        // increment the pointer if it is not on the last entry
               if ((i!=ROWS-1)&&(j!=COLS-1))
               { 
               ptr++;
               }
        }
    }*/


// if ROWS<COLS we want to have 2d vector tempT
if (ROWS<COLS)
{
// store all of the input matrice's values in the 2d vector "tempT"
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {
// set the j,ith entry of the 2d vector "temp" equal to the value currently pointed to by the pointer
        tempT[j][i]=*ptr;
// increment the pointer to the address of the next entry of the input matrix
        if (((i!=(ROWS-1))&&(j!=(COLS-1))))
        {
            ptr++;
        }
        }
    }
    ptr=&M;
// transport the entries of tempT into the output matrix MT
    for(i=0;i<=COLS-1;i++)
    {
        for(j=0;j<=ROWS-1;j++)
        {
        *ptrT=tempT[i][j];
// increment the pointer
        if (((i!=ROWS-1)&&(j!=COLS-1)))
        {
            ptrT++;
        }
        }
    }
}
ptrT=&MT;

// if ROWS>COLS we want to use the 2d vector temp_T
if (ROWS>COLS)
{
// store all of the input matrice's values in the 2d vector "temp_T"
    for(i=0;i<=ROWS-1;i++)
    {
        for(j=0;j<=COLS-1;j++)
        {

// set the j,i th entry of the 2d vector "temp" equal to the value currently pointed to by the pointer
        temp_T[j][i]=*ptr;
// increment the pointer
        if (((i!=ROWS-1)&&(j!=COLS-1)))
        {
            ptrT++;
        }
    }

// the for loop for swapping the j,i entries of the 2d vector "temp" with the i,j entries of the input matrix
    for(i=0;i<=COLS-1;i++)
    {
        for(j=0;j<=ROWS-1;j++)
        {
// if j is not equal to i swap the value pointed to by the pointer (the i,j entry of the input matrix) for the j,ith entries value of the 2d vector "temp"
            if (j!=i)
            {
            *ptrT=temp_T[j][i];
            }
// increment the pointer
        if (((i!=ROWS-1)&&(j!=COLS-1)))
        {
            ptrT++;
        }
        }
    }
}
return;
}

// 就是这样,如果 COLS 非常小并且说 ROWS 非常大,因为中间的 2d 方形矢量,它不是很有效,但是对于其他一切,如果它工作正常,它应该是相当不错的。

【问题讨论】:

    标签: c++ pointers transpose


    【解决方案1】:

    你的代码有几个主要问题,这一行:

    if (ROWS=COLS)
    

    可能是:

    if (ROWS == COLS)
    

    在第一种情况下,您会将COLS 的值分配给ROWS,在第二种情况下,您将检查它们是否相等。在所有 for 循环中,当您应该使用 &lt; 时,您正在使用 &lt;=,否则您将访问数组边界之外的一个。

    除此之外,代码太复杂了,转置函数应该很简单,这是一种可能的方法:

    template <int n, int m>
    void squaretranspose( int a[n][m], int b[m][n])
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                b[j][i] = a[i][j];
            }
        }
    } 
    

    但是转置矩阵最快和最简单的方法是反转坐标,因此您可以访问(j,i),而不是访问(i,j)。另一方面,如果性能是您主要关心的问题,那么 previous thread 很好地涵盖了我的解决方案以及我的解决方案以及其他可能适合您的解决方案。

    【讨论】:

      【解决方案2】:

      使用 DIY 索引:ptrT[ index(j,i,COLS,ROWS) ] = ptr[ index(i,j,ROWS,COLS) ]。现在您所要做的就是编写将 2D indeces 转换为 1D indeces 的函数。

      【讨论】:

        猜你喜欢
        • 2012-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-03
        • 1970-01-01
        相关资源
        最近更新 更多