//To compile: g++ [source_code.cpp] -o [exec_file] // replace [soure_code.cpp] with the filename, // replace [exec_file] with the executable file name // i.e., g++ c++_basics.cpp -o run //Note: if the output redirect is not given, i.e., the "-o [exec_file]" part is missing // then the default executable name is a.out //To run the program, simply run the executable: ./[exec_file] // i.e., ./a.out (if your executable file is a.out) #include //input/output stream library #include #include using namespace std; void populate_array(int* arr, int size){ //initialize / populate the array for (int i = 0; i < size; ++i) { arr[i] = rand() % 10; //0-9 } } void print_array(int* arr, int size){ //print out the array elements for (int i = 0; i < size; ++i) { cout << arr[i] << " "; } cout << endl; } int* create_dynarray1(int size){ int* temp = new int [size]; return temp; } void create_dynarray2(int** temp, int size){ *temp = new int [size]; } void create_dynarray3(int*& temp, int size){ temp = new int [size]; } //main function: entry point of the program int main() { srand(time(NULL)); //stack vs. heap int x = 100; cout << "addr of x: " << &x << endl; int *p = new int; cout << "p: " << p << endl; *p = 50; cout << *p << endl; delete p; p = nullptr; //0x0 int size = 10; //usually comes from a user input //1D dyn array int *arr; arr = create_dynarray1(size); //an dynarray of 10 ints // create_dynarray2(&arr, size); // create_dynarray3(arr, size); populate_array(arr, size); print_array(arr, size); delete [] arr; arr = nullptr; return 0; }