/* This program demonstrates * 1. Reading and printing array values * 2. Copying arrays - deep copy required * 3. Working with array elements as L-values and R-values * 4. Working with arrays and functions. * * 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 linearSearch(double arr[], int size, double key); void leftArrayShift(double arr[], int size); 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"; } double sum =0; /* We can perform all the operations we've seen so far using array * elements as variables * For example, to get the sum of all array elements * iterate through the array, add each value to the sum */ for(int i=0; i<10; i++) { sum = sum + arr[i]; } // Replace array elements with the rounded down square roots of new numbers. for(int i=0; i<10; i++) { arr[i] = static_cast ( sqrt ( abs (arr[i] ) ) ); } cout<<"Elements replaced by rounded down square roots: "; for(int i=0; i<10; i++) { cout<>searchVal; //call the function, store the returned value in "found" foundPos = linearSearch(arr, NUM, searchVal); if( foundPos == -1) cout<<"The element is not in the array"<