/* This program demonstrates * 1. Reading and printing array values * 2. Working with arrays and functions. * 2.a: Linear Search * 2.b: Left Array Shift * a.c: Right Array Shift * * Arrays can be passed into function like regular variables. * However, arrays always behave like they have been passed 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 size); int linearSearch(double arr[], int size, double key); void leftArrayShift(double arr[], int size); int main() { double arr[10]; // declare the array const int NUM = 10; //accept array values from the user cout<<"\nEnter values for the array: "; for(int i=0; i<10; i++) { cin>>arr[i]; } /* calling the cubeElements function. Note that when we call the * function, we only mention the name of the array. */ cubeElements(arr, NUM); /* You will notice that changing the values of array elements in the * cubeElements function has resulted in squaring the values of the * array in the main function as well. This is because arrays always pass * by address (which resembles pass by reference), even without us * explicitly saying anything */ cout<<"After function call: "; for(int i=0; i>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"<