注册 登录
编程论坛 C++教室

高手们,能不能帮我看看我的code问题在哪里呢,谢谢

weiwei859 发布于 2011-05-12 06:39, 941 次点击
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
// contains function prototypes for functions srand and rand
#include <cstdlib>
#include <ctime>   // contains prototype for function time
  // function prototype
int main()
{
   int m;
   int n;
   cout<<"please input the number of row"<<endl;
   cin>> m ;
   cout<<"please input the number of column"<<endl;
   cin>> n ;
      int* A=new int [m+n][m*n];
   //generate the A matrix//
   for (int i=0;i<m;i++) for (int j=0;j<n;j++) A[i][n*i+j]=1;
   for (int i=0;i<n;i++) for (int j=0;j<m;j++) A[m+i][n*j+i]=1;
   //Print A matrix for test//
   for (int i=0;i<(m+n);i++)
   {
       for (int j=0;j<m*n;j++) cout<<A[i][j]<<" ";
       cout<<endl;
   }


   delete [] rowsum;
   delete [] colsum;
   return 0;  // indicates successful termination
} // end main



[ 本帖最后由 weiwei859 于 2011-5-12 11:21 编辑 ]
10 回复
#2
qq10235692232011-05-12 07:08
程序代码:
   int m;
   int n;
   cin>>m>>n;
   int rowsum[m]={0};  //数组定义时用了变量,下同!原理上数组定义上不可以用变量的.
   int colsum[n]={0};
#3
weiwei8592011-05-12 07:14
那要怎么改呢。我在网上找到了要用new,但是不知道为什么还是不可以
#4
rjsp2011-05-12 08:44
将 cin>> n >>endl; 等改为
cin>> n;
#5
weiwei8592011-05-12 08:50
但是如果我不是外部输入的,怎么赋值呢


[ 本帖最后由 weiwei859 于 2011-5-12 10:56 编辑 ]
#6
pangding2011-05-12 11:10
不是外部输入的就直接
m = 3
呀这样的不就行了吗。
#7
weiwei8592011-05-12 11:20
但是我的code为什么跑出来有问题呢。
int* A=new int [m+n][m*n];
   int* B=new int [m+n];
   //generate the A matrix//
   for (int i=0;i<m;i++) for (int j=0;j<n;j++) A[i][n*i+j]=1;
   for (int i=0;i<n;i++) for (int j=0;j<m;j++) A[m+i][n*j+i]=1;
问题主要在于array
#8
诸葛修勤2011-05-12 16:35
程序代码:
  1 #include <iostream>
  2 using namespace std;
  3
  4 int main(void)
  5 {
  6     int m;
  7     int n;
  8     cout << "please input the number of row" << endl;
  9     cin >> m;

 10     cout << "please input the number of column" << endl;

 11     cin >> n;

 12

 13     int **A = NULL;

 14     A = new int*[m*n];

 15

 16     for (int i=0; i<m+n; i++)

 17     {

 18         A[i] = new int [m*n];

 19     }

 20

 21     for (int i=0; i<m+n; ++i)

 22     {

 23         for (int j=0; j<n*m; ++j)

 24         {  

 25             A[i][j] = i+j;

 26         }  

 27     }

 28

 29     for (int i=0; i<(m+n); ++i)

 30     {

 31         for (int j=0; j<m*n; ++j)

 32         {

 33             cout << A[i][j] << ' ';

 34         }

 35         cout << endl;

 36     }

 37

 38     for (int i=0; i<m+n; ++i)

 39     {

 40         delete [] A[i];

 41     }

 42

 43     delete []A;

 44     return 0;

 45 }
#9
诸葛修勤2011-05-12 16:41
对于多维数组  如果要使用指针表示的  则只能是换成对应的数组指针的形式  然后再new动态开辟

int a[5][6][7];              int (*pa)[6][7];  pa = new int [5][6][7];
int a[6][7];                 int (*pa)[7];     pa = new int [6][7];
int a[7];                    int *pa;          pa = new int [7];
#10
donggegege2011-05-12 21:40
既然用new定义了,就必须对应的进行delete操作,释放内存。
#11
weiwei8592011-05-14 11:00
谢谢,大家太好了,很有帮助。
1