Fly With My Heart

誓言无声,人生无悔。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

//TwoDimension.h
#ifndef _TWO_
#define _TWO_
#include <iostream>
int** AllocateBuffer(int xDimension, int yDimension);
void ReleaseBuffer(int** myArray, int xDimension);
void InitializeMatrix(int** myArray,int xDimension,int yDimension);
void DisplayMatrix(int** myArray, int xDimension, int yDimension);
#endif


//TwoDimension.cpp
#include "TwoDimension.h"
using namespace std;
int** AllocateBuffer(int xDimension, int yDimension)
{
 int** temp = new int* [xDimension];
 for (int i = 0;i < xDimension; ++i)
 {
  temp[i] = new int [yDimension];
 }
 
 return temp;
}

void ReleaseBuffer(int** myArray, int xDimension)
{
 for (int i = 0; i < xDimension ; ++i)
 {
  delete [] myArray[i];
  myArray[i] = NULL;
 }

 delete [] myArray;
 myArray = NULL;
}

void InitializeMatrix(int** myArray, int xDimension, int yDimension)
{
 for (int i = 0; i < xDimension; ++i)
  for (int j = 0; j < yDimension; ++j)
   myArray[i][j] = ((i==j)||(j==yDimension-1))?1:0;
}

void DisplayMatrix(int** myArray, int xDimension, int yDimension)
{
 for (int i = 0; i < xDimension; ++i)
 {
  for (int j = 0; j < yDimension; ++j)
   cout<< myArray[i][j];
  cout<<endl;
 }
}




 

//test.cpp
#include "TwoDimension.h"
using namespace std;
void main()
{
 int** matrix;
 int L;
 cout<<"input L:"<<endl;
 cin>>L;
 int xDimension = 2*L;
 int yDimension = 2*L + 1;
 matrix = AllocateBuffer(xDimension,yDimension);

 InitializeMatrix(matrix,xDimension,yDimension);
 DisplayMatrix(matrix,xDimension,yDimension);

 ReleaseBuffer(matrix,xDimension);

}


posted on 2008-04-10 15:15  FlyWithMyHeart  阅读(293)  评论(0编辑  收藏  举报