|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<iostream>
- #define maxSize 1024
- using namespace std;
- void func(int A[][maxSize])//应该如何写才能接收主函数传入的数组
- {
- int i,j,count,temp;
- while(i<m)
- {
- count=0;
- for(j=0;j<n;j++)
- {
- if(A[i][j]>A[i][j+1])
- {
- temp=A[i][j];
- A[i][j]=A[i][j-1];
- A[i][j-1]=A[i][j];
- count++;
- }
- }
- if(count==0)
- {
- i++;
- }
- }
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<A[i][j];
- }
- cout<<'\n';
- }
- }
- int main()
- {
- int m,n,,i,j;
- cout<<"请输入阶数:";
- cin>>m>>n;
- int A[m][n];
- cout<<"请输入矩阵"<<endl;
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cin>>A[i][j];
- }
- }
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- cout<<A[i][j];
- }
- cout<<'\n';
- }
- func(A[m][n]);
- return 0;
- }
复制代码
这个程序是实现输入m*n阶矩阵,每行元素从小到大排序输出的,但是现在这个程序的二维数组无法按我的想法传递(m和n无法传递到子函数中),应该如何修改啊。
本帖最后由 bin554385863 于 2019-12-9 17:32 编辑
- #include <iostream>
- void func(int (*ptr)[3], int c)//二维数组做为函数的形参
- {
- for (size_t i = 0; i < 3; i++)
- {
- for (size_t j = 0; j < c; j++)
- {
- std::cout << ptr[i][j];
- }
- std::cout<<std::endl;
- }
- }
- int main(int argc, char const *argv[])
- {
- int arr[3][3] = {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[m][n]来说,它的类型是 int (*)[m].
|
|