定义一个一维数组,数组大小未知,将在程序运行过程中得到,如何实现?
例如:#include<iostream>
#include<fstream>
using namespace std;
#define M 3
#define N 2
#define P 2
#define n (M+1)*(N+1)*(P+1)
int main()
{
int i,j,k,count=0,m=0;
cout<<M<<" "<<N<<" "<<P<<endl;
cout<<n<<endl;
int Ap[n+1];
int a[n][n];
/*此处为稀疏矩阵的元素确定*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
if(a[i][j]!=0)
m+=1;
}
cout<<endl;
}
ofstream outfile1;
outfile1.open("D:\\2013IBM\\coefficient\\Array of A\\a[i][j].txt",ios::out);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
outfile1<<a[i][j]<<" ";
}
outfile1<<endl;
}
outfile1<<endl;
outfile1.close();
int Ai[m]; //数组大小m这样定义不正确,怎么办?m为矩阵a中的非零个数个数
double Ax[m];
Ap[0]=0;
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(a[i][j]!=0)
{
count+=1;
Ai[n-1]=i;
Ax[n-1]=a[i][j];
}
Ap[j+1]=count;
}
}
for(j=0;j<n+1;j++)
{
cout<<Ap[j]<<" ";
}
cout<<endl;
ofstream outfile2;
outfile2.open("D:\\2013IBM\\coefficient\\Array of A\\Ap.txt",ios::out);
for(j=0;j<n+1;j++)
{
outfile2<<Ap[j]<<" ";
}
outfile2<<endl;
outfile2.close();
for(j=0;j<count;j++)
{
cout<<Ai[j]<<" ";
}
cout<<endl;
ofstream outfile3;
outfile3.open("D:\\2013IBM\\coefficient\\Array of A\\Ai.txt",ios::out);
for(j=0;j<count;j++)
{
outfile3<<Ai[j]<<" ";
}
outfile3<<endl;
outfile3.close();
for(j=0;j<count;j++)
{
cout<<Ax[j]<<" ";
}
cout<<endl;
ofstream outfile4;
outfile4.open("D:\\2013IBM\\coefficient\\Array of A\\Ax.txt",ios::out);
for(j=0;j<count;j++)
{
outfile4<<Ax[j]<<" ";
}
outfile4<<endl;
outfile4.close();
}