how to define global array in c 2b 2b in a scope

Solutions on MaxInterview for how to define global array in c 2b 2b in a scope by the best coders in the world

showing results for - "how to define global array in c 2b 2b in a scope"
Niko
25 Jun 2016
1class C {
2 int [] x;
3
4 void method A(int size)
5 {
6   x = new int[size];   // Allocate the array
7   for(int i = 0; i < size; i++)
8      x[i] = i;         // Initialise the elements (otherwise they contain random data)
9   B();
10   delete [] x;         // Don't forget to delete it when you have finished
11                        // Note strange syntax - deleting an array needs the []
12 }
13
14 void method B()
15 {
16   int n;
17   cin >> n;
18   cout << x[n];
19   // Be warned, if the user inputs a number < 0 or >= size, 
20   // you will get undefined behaviour!
21  }
22}