Dynamic Arrays and Two-Dimensional Arrays in C++ Programming

cs1201 programming language 2 n.w
1 / 13
Embed
Share

Explore dynamic arrays and two-dimensional arrays in C++ programming, covering concepts such as array size, pointers, array initialization, and dynamic memory allocation. Learn how to create dynamic two-dimensional arrays and effectively manage memory allocation for multi-dimensional arrays in C++.

  • Arrays
  • Dynamic Arrays
  • Two-Dimensional Arrays
  • Pointers
  • Memory Allocation

Uploaded on | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS1201: Programming Language 2 By:Nouf Aljaffan Edited by : Nouf Almunyif Dynamic Arrays

  2. Dynamic Array int * arrayptr; int arraysize; cout<<"enter the array size :" <<endl; cin >> arraysize; arrayptr = new int [arraysize];

  3. Two-Dimensional Arrays int board[4][5]; 1 2 3 4 2 3 4 5

  4. Dynamic Two-Dimensional Arrays (1) X int x ; P int *p ; X int x[4]

  5. Dynamic Two-Dimensional Arrays (1) There are various ways you can create dynamic dimensional arrays. One way is as follows. // declares an array of four pointers. int *p[4]; P

  6. Dynamic Two-Dimensional Arrays (1) Suppose that we want to draw a 4X6 bord How can we do that ? int *board[4]; You can now use these pointers to create the rows of board. Suppose that each row of board has six columns. Then, the following for loop creates the rows of board. for (int row = 0; row < 4; row++) board[row] = new int[6];

  7. int *board[4]; for (int row = 0; row < 4; row++) board[row] = new int[6]; Row = Board[0] = new int [6] Board[1] =new int [6] Board[2] =new int [6] Board[3] = new int [6] 0 1 2 3 4 board

  8. Note Note that the expression new int[6] creates an array of six components of type int and returns the base address of the array. The assignment statement then stores the returned address into board[row]. It follows that after the execution of the previous for loop, board is a two-dimensional array of four rows and six columns. However, the way board is declared, the number of rows is fixed. So in reality, board is not a true dynamic two-dimensional array.

  9. Dynamic Two-Dimensional Arrays (2) Second approach to declare 2D array is int **board; // declares board to be a pointer to a pointer. In other words, board and *board are pointers Suppose that you want board to be an array of 10 rows and 15 columns. To accomplish this, first we create an array of 10 pointers of type int and assign the address of that array to board. board = new int* [10]; Next, we create the columns of board. The following for loop accomplishes this. for (int row = 0; row < 10; row++) board[row] = new int[15];

  10. Example #include <iostream> using namespace std; void fill (int **p, int row ,int column); void print (int **p, int row ,int column); void main(){ int ** board; int R, C; cout<<"enter the array size :" <<endl; cout << "number of rows : " <<endl;cin >>R; cout<<"number of columns : " <<endl;cin >> C; cout <<" *************************" <<endl; board = new int*[R]; for ( int i=0;i<R;i++) board[i]= new int[C]; fill (board,R,C); print(board ,R,C); }

  11. Example void fill (int **p, int row ,int column) { for ( int i=0;i<row;i++) { cout <<" enter "<<column<<" numbers to fill row number " << i<<endl; for (int j=0;j<column ;j++) cin>>p[i][j]; cout<<endl;} }

  12. Example void print(int **p, int row ,int column) { cout << " the board is : "<<endl; for (int i=0;i<row;i++) { for (int j=0;j<column ;j++) cout << p[i][j]<< " "; cout<< endl; } }

Related


More Related Content