how to write this program in function.i am puzzled.

create a dynamic array of size n*n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 #include<iostream>
#include<conio.h>
using namespace std;
void dynamicarray(int arrayy[3][5],int n){

	 int **arrayy= new int*[n];

    for (int i=0; i < n; ++i){
		int **arrayy=new int*[8];
	
for(int j=0;j<8;++j)
	arrayy[i][j]=new int [8][1];//declaring an array like a chessboard

for(int x=0;x<8;x++){

for(int y=0;y<8;y++)
arrayy[x][y]=x*y;//set each element to value
}

        cout<<"array indicates;\n";
	for (int x=0;x<8; x++){
	for(int y=0;y<8;y++)
	  // we can use mda[x][y] now

    // now we must clean up
    for (int i=0; i < n; ++i){
		for(int j=0;j<n;++j)
		{
        delete[] arrayy[i];

    delete[] arrayy;
	getch();
		}}}}}


int main()
{
    int n=0;
	int x,y;


    cout << "Specify n: ";
    cin >> n;
	cout<<"["<<x<<"]["<<y<<"]="<<dynamicarrayy[x][y]<<" ";
	cout<<"\n";
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
      // first allocate the rows dynamically
      int** dynamicArray = new int *[rows];

     // then allocate the columns dynamically.
     for( int i = 0 ; i < rows; i++)
           dynamicArray[i] = new int[cols];

    
     // delete them properly
         for( int i = 0 ; i < rows; i++)
           delete[] dynamicArray[i];

           delete[] dynamicArray;


Topic archived. No new replies allowed.