高手帮小弟我来看看,很简单的C++代码,输出有错!帮忙解决,一些
高手帮我来看看,很简单的C++代码,输出有错!帮忙解决,一些!
一下有两种输出方法,
第一种正确,第二种输出不对,
请问怎么回事?????
好像是display()函数写的有问题
#include <iostream.h>
#include <math.h>
const int max=5;
class matrix
{
public:
int m,n;
float array[max][max];//数组的下表在编译时必须确定
matrix()
{
m=n=0;
array[m][n]=0.0;
}
void set(int p, int q)
{
m=p;
n=q;
for(int i=0;i <m;i++)
{
for(int j=0;j <n;j++)
{
array[i][j]=i+j;
}
}
}
void display()
{
cout < < "the row of matrix is: " < < ' ' < <m < < '\n ';
cout < < "the list of matrix is: " < < ' ' < <n < < '\n ';
cout < < "the numbers in the matrix are: " < < '\n ' < < '[ ' < < '\n ';
for(int i=0;i <m;i++)
{
for(int j=0;j <n;j++)
{
cout < < ' ' < <array[i][j] < < ' ';
}
cout < < '\n ';
}
cout < < '] ' < < '\n ';
}
};
void main()
{
matrix A;
A.set(5,5);
//***********************Method1:output A.array***********************************************//
cout < < "A.array is:\n " < < "[\n ";
for(int i=0;i <A.m;i++)
{
for(int j=0;j <A.n;j++)
{
cout < < " " < <A.array[i][j];
}
cout < <endl;
}
cout < < '] ' < < '\n ';
//**********************Method 2:output A.array*************************************************//
A.display();
}
------解决方案--------------------
A.array is:
[
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
]
the row of matrix is: 5
the list of matrix is: 5
the numbers in the matrix are:
[
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
]
Press any key to continue
一下有两种输出方法,
第一种正确,第二种输出不对,
请问怎么回事?????
好像是display()函数写的有问题
#include <iostream.h>
#include <math.h>
const int max=5;
class matrix
{
public:
int m,n;
float array[max][max];//数组的下表在编译时必须确定
matrix()
{
m=n=0;
array[m][n]=0.0;
}
void set(int p, int q)
{
m=p;
n=q;
for(int i=0;i <m;i++)
{
for(int j=0;j <n;j++)
{
array[i][j]=i+j;
}
}
}
void display()
{
cout < < "the row of matrix is: " < < ' ' < <m < < '\n ';
cout < < "the list of matrix is: " < < ' ' < <n < < '\n ';
cout < < "the numbers in the matrix are: " < < '\n ' < < '[ ' < < '\n ';
for(int i=0;i <m;i++)
{
for(int j=0;j <n;j++)
{
cout < < ' ' < <array[i][j] < < ' ';
}
cout < < '\n ';
}
cout < < '] ' < < '\n ';
}
};
void main()
{
matrix A;
A.set(5,5);
//***********************Method1:output A.array***********************************************//
cout < < "A.array is:\n " < < "[\n ";
for(int i=0;i <A.m;i++)
{
for(int j=0;j <A.n;j++)
{
cout < < " " < <A.array[i][j];
}
cout < <endl;
}
cout < < '] ' < < '\n ';
//**********************Method 2:output A.array*************************************************//
A.display();
}
------解决方案--------------------
A.array is:
[
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
]
the row of matrix is: 5
the list of matrix is: 5
the numbers in the matrix are:
[
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
]
Press any key to continue