forever33221 发表于 2019-12-9 16:36:42

矩阵问题

#include<iostream>
#define maxSize 1024
using namespace std;

void func(int A[])//应该如何写才能接收主函数传入的数组
{
        int i,j,count,temp;
        while(i<m)
        {
                count=0;
                for(j=0;j<n;j++)
                {
                        if(A>A)
                        {
                                temp=A;
                                A=A;
                                A=A;
                                count++;
                        }
                }
                if(count==0)
                {
                        i++;
                }
        }
        for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        cout<<A;
                }
                cout<<'\n';
        }
}

int main()
{
        int m,n,,i,j;
        cout<<"请输入阶数:";
        cin>>m>>n;
        int A;
        cout<<"请输入矩阵"<<endl;
        for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        cin>>A;
                }
        }
        for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        cout<<A;
                }
                cout<<'\n';
        }
        func(A);
        return 0;
}

这个程序是实现输入m*n阶矩阵,每行元素从小到大排序输出的,但是现在这个程序的二维数组无法按我的想法传递(m和n无法传递到子函数中),应该如何修改啊。

Croper 发表于 2019-12-9 16:40:54

既然是c++,建议直接上vector#include <vector>

void func(vector<vector<int> > A){
...
}

bin554385863 发表于 2019-12-9 17:29:30

本帖最后由 bin554385863 于 2019-12-9 17:32 编辑

#include <iostream>
void func(int (*ptr), int c)//二维数组做为函数的形参
{
    for (size_t i = 0; i < 3; i++)
    {
      for (size_t j = 0; j < c; j++)
      {
            std::cout << ptr;
      }
      std::cout<<std::endl;
    }
}
int main(int argc, char const *argv[])
{
    int arr = {1,2,3,4,5,6,7,8,9};
    func(arr, 3);
    return 0;
}

-----------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-5l5uyoe2.wdh --stdout=Microsoft-MIEngine-Out-s4enc20k.axl --stderr=Microsoft-MIEngine-Error-qa5h0kwm.lzh --pid=Microsoft-MIEngine-Pid-ihivnbk2.0dg --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
123
456
789

E:\Users\admin\Documents\VScode\Code>
==================================
对于二维数组int arr来说,它的类型是int (*).

Croper 发表于 2019-12-9 17:49:43

      cin>>m>>n;
      int A;
另外,c++不支持变长数组吧

forever33221 发表于 2019-12-9 19:59:59

Croper 发表于 2019-12-9 17:49
另外,c++不支持变长数组吧

我刚知道不支持变长数组,但是这样写可以在Dev C++上实现,应该是程序的问题

forever33221 发表于 2019-12-9 20:00:46

bin554385863 发表于 2019-12-9 17:29
-----------------------------------------------------------------------------
Microsoft Windows [ ...

数组传入函数是必须用指针吗

bin554385863 发表于 2019-12-9 20:07:03

本帖最后由 bin554385863 于 2019-12-9 20:16 编辑

forever33221 发表于 2019-12-9 20:00
数组传入函数是必须用指针吗

C风格数组做为参数只能用指针的形式

forever33221 发表于 2019-12-9 20:10:15

bin554385863 发表于 2019-12-9 20:07
数组做为参数只能用指针的形式

好的,谢谢啦

forever33221 发表于 2019-12-10 21:09:27

bin554385863 发表于 2019-12-9 20:07
C风格数组做为参数只能用指针的形式

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char const *argv[])
{
        int m,n,i,j,h,count,temp;
        cout<<"请输入行数和列数";
        cin>>m>>n;
    vector<vector<int> >swp(m,vector<int>(n));
    for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        cin>>swp;
                }
        }
        for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        cout<<swp;
                }
                cout<<'\n';
        }
        for(i=0;i<m;i++)//代码有问题处
        {
                for(h=0;h<n;h++)
                {
                count=0;
                  for(j=0;j<n;j++)
                  {
                          if(swp>swp)
                          {
                                  temp=swp;
                                  swp=swp;
                                  swp=swp;
                                  count=1;
                          }
                  }
                  if(count==0)
                  {
                          break;
                  }
          }
        }
        for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        cout<<swp;
                }
                cout<<'\n';
        }
    return 0;
}

麻烦问一下,我这个排序有什么问题啊,为什么不能按升序输出啊

forever33221 发表于 2019-12-11 14:32:19

bin554385863 发表于 2019-12-9 20:07
C风格数组做为参数只能用指针的形式

我知道了,j+1造成数组越界了
页: [1]
查看完整版本: 矩阵问题