Skip to content

Array implementation by 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
//****************************************************************
// We introduce the array implementations by using pointers
//****************************************************************
#include <cstdlib>
#include <iostream>
 
using namespace std;
void printElements(int **&haha,int size1,int size2);
int main(int argc, char *argv[])
{
    //We initalize the arrays' size
    const int size1 = 4,size2 =3;
    // We define a pointer which is ptr
    int *ptr;
    // We initialize out pointer by size of 10
    ptr = new int[10];
    // To show that let'S print the array first element
    ptr[0] = 3;
    cout<<"The first element of the array ptr is "<<ptr[0]<<endl;
    int **pointer;// We implement a matrix
    //Initalization of the columns
    pointer = new int*[size1];
    //Initialization of the rows
    for(int i=0;i<size1;i++)
        pointer[i] = new int[size2];
    //We inialize all the elements to 0
    for(int i=0;i<size1;i++)
        for(int j=0;j<size2;j++)
            pointer[i][j] = 0;
    //We call the method printElements
    printElements(pointer,size1,size2);
    system("PAUSE");
    return EXIT_SUCCESS;
}
void printElements(int **&haha,int size1,int size2){
     for(int i=0;i<size1;i++){
         for(int j=0;j<size2;j++){
             cout<<haha[i][j]; 
         }
         cout<<endl; 
     }
}

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *