transform a code to pointers

Hello can you please help me on how we can transform this code into pointers(put pointers in this code) im a beginner and im finding some difficulties cause its new for me...

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
#include<iostream> 
#include <ctime> 
#include <cstdlib>   
Using namespace std;     
 
const int N=5;               
void FillArray(int[N][N]); 
void PrintArray(int[N][N]);

int main() {                     

int arr[N][N];                  
srand(time(0));                   
FillArray(arr);
Cout<<"print array:"<<endl;                        
PrintArray(arr);}

void FillArray(int arr[N][N]) 
{ for(int r=0; r < N; r++) 
{ for(int c=0; c < N; c++) 
{ arr[c][r] = (rand() % 998) + 44; } } }                     
 
void PrintArray(int arr[N][N]) 
{ for(int r=0;r<N;r++) 
{ for(int c=0 ; c<N ;c++) 
{  cout << setw(6) << arr[c][r]; 
}                             
cout << '\n'; } 
}
Well, for one thing, you need to work on you're nesting; the code you've posted looks awful. Here's a guide I found using Google - hope it helps.

http://www.cprogramming.com/tutorial/style.html

Anyways, you're request
how we can transform this code into pointers(put pointers in this code)
is actually rather vague. I ASSUME you mean you want to convert the 2D arrays in ur code into double pointers. I'll do so, but to understand how it works and why arrays and pointers can be interchangeable, I suggest you read this: http://www.cplusplus.com/doc/tutorial/pointers/#arrays

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

const int N = 5;
void FillArray(int**);
void PrintArray(int**);

int main() {
    srand(time(0));
    int **arr = new int*[N];
    //allocate memory to the array
    for (unsigned i = 0; i < N; ++i) {
        arr[i] = new int[N];
    }
    FillArray(arr);

    cout << "print array:" << endl;
    PrintArray(arr);
    //EDIT: you need to free the memory!
    for (unsigned i = 0; i < N; ++i) {
        delete arr[i];
    }
    delete arr;
}

void FillArray(int **arr) {
    for(int r = 0; r < N; r++) {
        for(int c = 0; c < N; c++) {
            arr[c][r] = (rand() % 998) + 44;
        }
    }
}

void PrintArray(int **arr) {
    for(int r = 0;r<N;r++) {
        for(int c = 0 ; c<N ;c++) {
            cout << setw(6) << arr[c][r] << endl;
        }
    }
}
Last edited on
closed account (N36fSL3A)
You're not freeing the memory after in your example. At the end of main, put:
1
2
3
4
    for(unsigned i = 0; i < N; i++){
        delete arr[i];
    }
    delete arr;
Last edited on
Can we make it with one pointer? Because we didn't learn this (**) or this "new int".... Thank you for answering!!
Thanks, Lumpkin, I totally neglected that.

chry44 - using a single pointer would mean a 1D array, or in other words,

int *arr = new int[N]
is equivalent to:
int arr[N];

whereas
int **arr = new int*[N]
and subsequent for loop is equivalent to:
int arr[N][N]

Because you're using 2D arrays, we need also to use 2 stars; "A pointer to a pointer". Likewise, if we wanted a 3D array, we would use three stars(int ***) and so on.

The new int is necessary to dynamically allocate memory to the array. Note that when you handle memory in this way, it also becomes you're responsibility to deallocate the memory - as Lumpkin stated.
Can we make it with one pointer?
you can always turn multiple dimension into one. You just have to calculate the index:

a2d[x][y] -> a1d[(x * max_y) + y]

Since the computer nows just about one dimension this is what the compiler generates anyway
> how we can transform this code into pointers(put pointers in this code)
> Can we make it with one pointer?
> Because we didn't learn this (**) or this "new int"

Only one pointer is required; a pointer to the beginning (to the first element) of the array.
And a second parameter which gives the number of elements in the array.

Ignore this (**) and this "new int". They are completely irrelevant to the question that was asked.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip> // for setw
using namespace std;

const int N = 5 ; // compile-time constant

typedef int array_type[N] ; // 'array_type' is an array of N int or int[N]

// these functions recieve a pointer to the first element of an array
// the type of the element is an array of N int int[N]
// the number of elements is num_elems
void FillArray( array_type* first, int num_elems ) ;
void PrintArray( const array_type* arr, int num_elems ) ;

int main()
{
    std::cout << "----------------- an array ----------------\n\n" ;
    
    int arr[N][N]; // 'arr' is an array; every element of 'arr' is also an array
                   // it is an array of N arrays      
                   // sometimes referred to as a 'two-dimensional array'
    srand(time(0));

    array_type* first = arr ; // 'arr' is implicilly converted to a pointer
                              // 'first' points to the first element of 'arr'
                              // that element is an array of N int or int[N]

    // fill the array; pass pointer to first element and number of elememts
    FillArray( first, N ) ;

    // print the array; pass pointer to first element and number of elememts
    // 'arr' decays to a pointer to the first element
    // sizeof(arr) gives the size of 'arr' (bytes)
    // sizeof( arr[0] ) gives the size of the first element of 'arr'
    // sizeof(arr) / sizeof( arr[0] ) gives the number of elements
    PrintArray( arr, sizeof(arr) / sizeof( arr[0] ) ) ;

    {
        std::cout << "\n------------ another (bigger) array ----------------\n\n" ;
        const int M = 8 ;
        array_type another_array[M] ; // an array of M arrays 
        FillArray( another_array, M ) ;
        PrintArray( another_array, M ) ;
    }
}

void FillArray( array_type* first, int num_elems )
{
    for( int r=0; r < num_elems ; ++r )
    {
        array_type& row = first[r] ; // the element (row) at position r

        for( int c=0; c < N;  ++c )
        {
            int& e = row[c] ; // the integer at position (col) c
            e = (rand() % 998) + 44;
        }
    }
}

void PrintArray( const array_type* arr, int num_elems )
{
    for( int r=0; r < num_elems ; ++r )
    {
        for( int c=0; c < N;  ++c )
        {
            cout << setw(6) << arr[c][r];
        }
        cout << '\n';
    }
}

http://coliru.stacked-crooked.com/a/411cdeee705c4578
Thank you all so much! i really appreciate it!!
I have this part too(now i wrote it... This is the last part of the code....) do you have any idea how to put pointers?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

const int N=6;
int c,tmp,x;

void SortDiagonally (int[N][N]);
 
int main()
{
int arr[N][N];

srand(time(0));

Sortdiagonally(arr)

void SortDiagonally(int arr[N][N])
{
	int W [N*N];
	int k=0;
	int tmp1=0;
	int tmp2=0;
	int x=0;
	int n=0;

	//copy to array W
	for (int i= 0; i<N ; i++){
		for (int j = 0; j<N; j++)
		{
			W[k] = arr[i][j];
			k++;
		}
	}

	//sort array W
	bool swapped=true;
	while(swapped){
		swapped=false;
		for (int o=1; o< N*N; o++){
			if ( W[o-1] > W[o]) {
				tmp1 = W[o-1];
				W[o-1] = W[o];
				W[o]= tmp1;
				swapped=true;
			}
		}
	}

	//copy sorted data back to array1
	k=0;
	for (int col=0; col<N; col++)
		for (int row=0; row<=col; row++)
			arr[row][col-row] = W[k++];


	for(c=N-1;c>-N;c--)
	{tmp=N-abs(c)-1;
	x=tmp;
	while(x>=0){
		if(c>=0){
			cout<<arr[x][tmp-x]<<", ";
		}
		else{
			cout<<arr[M-(tmp-x)-1][(M-1)-x]<<", ";
		}
		--x;
	}
	cout<<"\n";
	}
}
Topic archived. No new replies allowed.