使用二维动态数组写一个类 [英] Write a class using a two-dimensional dynamic array

查看:179
本文介绍了使用二维动态数组写一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个家庭作业。我不希望任何人都可以做我的工作,我只是有一个小麻烦的方面,但我会接受其他位的建议为好。

分配是:


  

使用二维动态数组写类


  
  

的构造通过在所述阵列的尺寸。构造函数也intializes动态数组的行索引乘以列索引中的所有值。


  
  

      
  1. 交换的二维阵列,其中该列索引被传递作为参数的两列。只是通过复制地址,不列elemnets值做到这一点。

  2.   
  3. 删除的二维阵列,其中,列索引传递作为参数的一列。不要只使用列阵列上的delete运算符,并设置水平数组元素为NULL。 1收缩水平数组的大小。

  4.   
  5. 创建一个打印功能的类打印出来的二维数组的值,并确保你的函数是否正常工作。你知道,他们工作正常后,删除打印功能。

  6.   

我需要帮助了解如何声明私人部分的二维数组。而且,如前所述,如果有人可以给我如何去做其他的暗示,这将是AP preciated。


解决方案

这是一段时间,因为我已经做C ++开发,但如果我没记错的话,你会做这样的事情届efollowing:

  INT行= 5;
INT COLS = 10;INT **阵列=新的INT * [行]
的for(int i = 0; I<行;我++){
     数组[我] =新的INT [COLS]
}

我可能是错了;我看帖相反在线,你必须假二维数组与一维数组,并改变你的下标:

<一个href=\"http://en.allexperts.com/q/C-1040/creating-2D-array-dynamically.htm\">http://en.allexperts.com/q/C-1040/creating-2D-array-dynamically.htm

I have a homework assignment. I'm not looking for anyone to do the work for me, I'm just having trouble with one little aspect, although I'd accept advice on other bits as well.

The assignment is:

Write a class using a two-dimensional dynamic array.

The constructor passes in the dimensions of the array. The constructor also intializes all values in the dynamic array to the row index multiplied by the column index.

  1. Swap two columns of the two-dimensional array, where the column indexes are passed in as parameters. Do this just by copying addresses, not values of column elemnets.
  2. Delete a column of the two-dimensional array, where the column index is passed in as a parameter. Do not just use the delete operator on the column array and set the horizontal array element to NULL. Shrink the size of the horizontal array by 1.
  3. Create a print function for the class to print out the values of the two-dimensional array and make sure that your functions are working correctly. After you know that they are working correctly, delete the print function.

I need help understanding how to declare the 2D array in the private section. And, as mentioned, if anyone could give me other hints on how to do it, that would be appreciated.

解决方案

It's been awhile since I've done C++ development, but if I remember correctly, you'd do something like th efollowing:

int rows = 5;
int cols = 10;

int** array = new int*[rows];
for (int i = 0; i < rows; i++) {
     array[i] = new int[cols];
}

I might be mistaken; I see posts to the contrary online where you have to fake 2D arrays with a single dimensional array and change your subscripting:

http://en.allexperts.com/q/C-1040/creating-2D-array-dynamically.htm

这篇关于使用二维动态数组写一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆