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...
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
chry44 - using a single pointer would mean a 1D array, or in other words,
int *arr = newint[N]
is equivalent to: int arr[N];
whereas int **arr = newint*[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 newint 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.
> 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.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip> // for setw
usingnamespace std;
constint N = 5 ; // compile-time constant
typedefint 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" ;
constint 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';
}
}
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?