为了账号安全,请及时绑定邮箱和手机立即绑定

这个程序执行起来结果不正确 无法转换成字符串 请各位指点一下

这个程序执行起来结果不正确 无法转换成字符串 请各位指点一下

PHP
白衣染霜花 2022-12-15 22:14:16
#include<iostream> using namespace std; void int_to_str(int n,char str[]) { char c; int i=0; while(
查看完整描述

1 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

问题主要是二维数组的创建问题函数可改为下面的函数,加的语句我注释了,动态分配内存。
其次是释放内存的函数最好也改一下,这我不太清楚,我写的贴在下面了。
最后,main()函数最好加上return 0;不然会有警告。(个人看法)
#include<iostream>
#include <cstdlib>
#include<ctime>
#include<cstring>
using namespace std;

void create( char **&ap, int n )

srand(time(0)); // 可简化
ap = new char*[n]; // 此语句不可少
int a;
int i,j;
a=rand()%2; 
for(i=0;i<n;i++) {
ap[i] = new char[n];
for(j=0;j<n;j++) {
if (a==0)
ap[i][j]='#';
else
ap[i][j]='.';
}
}


void output(char**&ap,int n )
{ int i,j;
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
cout<<ap[i][j]<<" ";  
cout<<endl; 

}
}

void freeArr(char**&ap,int n)
{  
for( int i = 0; i < n; i++ )
delete [] ap[ i ];

delete [] ap;
}

int main()
{
char **ap = NULL;

int n;
cout << "输入矩阵的阶:";
cin >> n;
create( ap, n );

cout << "\n形成矩阵:\n";
output( ap, n );

freeArr(ap, n);  
return 0;
}


查看完整回答
反对 回复 2022-12-18
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号