Created
July 24, 2020 03:01
-
-
Save SproutSeeds/1e4812d7a90385bdb7b3f3d2e0dee24d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
using namespace std; | |
void fillArray(int arr[], int length){ | |
for(int i = 0; i < length; ++i){ | |
arr[i] = rand()%10; //0-9 | |
} | |
} | |
void printArray(int arr[], int length){ | |
for(int i = 0; i < length; ++i){ | |
cout << "arr[" << i << "] = " << arr[i] << endl; | |
} | |
cout << endl; | |
} | |
int main(){ | |
srand(time(NULL));//Seeding a random number generator | |
int size; | |
cout << "How big is your array?"; | |
cin >> size; | |
int *arr1 = new int[size]; | |
int *arr2; | |
char hello[] = "Hello World";//Arrays identifiers are similar to pointers | |
char *str = hello; //Shallow Copy. Use strcpy for a deep copy of c_strings | |
fillArray(arr1, size); | |
cout << "Arr1:" << endl; | |
printArray(arr1, size); | |
arr2 = arr1;//SHALLOW COPY OF ARR1 INTO ARR2 | |
cout << "Arr2:" << endl; | |
printArray(arr2, size); | |
fillArray(arr2, size);//CHANGE VALUES TO ARR2 | |
cout << "Arr1 after Arr2 gets new values" << endl; | |
printArray(arr1, size); //VALUES HAVE CHANGED IN ARR1 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment