/* This program demonstrates * 1. Reading and printing array values * 2. Address calculation for array elements * 3. Copying arrays - deep copy required * 4. Working with array elements as L-values and R-values * 5. Working with arrays and functions. * * * An array is a collection of variables that satisfies the following properties: * 1. All the variables in the array are of the same type. * 2. All the variables share the same name and are accessed by an index. * 3. The elements (individual variables) are ordered and start at index 0. * 4. The elements are in continuous memory locations. * * Array address Calculation * Since array elements are stored in contiguous memory locations, we can calculate * the address of any particular array element. We would need (a) the starting address * which is the address of array element 0. (b) the size of each element in bytes, and * (c) the index for the element we want the address for. * * Address = Starting_Address + size_of_one_element * index * * Arrays can be passed into function like regular variables. * However, arrays always pass by reference. So any changes made to the array * in the function will be preserved across the function call and will be refected * in main, or any other calling function. */ #include #include using namespace std; /* Declare the functions here * when we pass an array into the function, we don't have to mention the size * the array. Mentioning it won't result in an error, but it is not necessary. * However, we have to specify it is an array, by using the [] * * Function names are self-descriptive here. They are described while being defined. */ void cubeElements(double arr[], int numElements); int main() { double arr[10]; // declare the array //accept array values from the user cout<<"\nEnter values for the array: "; for(int i=0; i<10; i++) { cin>>arr[i]; } cout<<"Printing array values: "; for(int i=0; i< 10; i++) { cout<< arr[i]<<"\t"; } /* If we exceed the index, it will either print junk, or result in a segfault * That depends on the index, and whether that memory location is accessible. */ /* cout<<"Element at index -200: "<< array[-200]< ( sqrt ( abs (arr[i] ) ) ); } cout<<"Elements replaced by rounded down square roots: "; for(int i=0; i<10; i++) { cout<