// arr4.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program illustrates how arrays can be passed as arguments to // functions. The program also shows one of the simple applications // that are used often with arrays; that is finding the smallest // value in an array. // Key Points: 1) Arrays as arguments. // 2) Notice that the size of the passed array is not indicated. // 3) Pass by reference. #include void FindSmallest( int a[], int searchrange ) { // This function accepts an array of integers as it argument. As a second argument, // the function takes an integer which indicates the search range // from the beginning of the array. For example, if that value is passed as // "7", then the array is searched from index 0 to index 7. The passed array // must have at least (searchrange + 1 ) elements. The function then // shows the smallest value in the array, within the passed range, and // indicates the index in which this min value was found. // If more than one element includes a minimum value, the first one is considered. int min = a[0]; // Assume that the first element includes the // smallest value, so index 0 is the index that // included the min value. int minindex = 0; for ( int i = 1; i <= searchrange; i++) { if (a[i] < min) { min = a[i]; // update min minindex = i; // update the index that includes the min value } } cout << " The minimum value within the array is: " << min << endl << " ,and it was found at index #: " << minindex << endl; } void IncrementElement( int a[], int range, int value ) { // This function accepts an array of integers as its argument, // along with a range and an integer value. The function will then increment // all elements within that range by the passed value. The array must have at // least (range + 1 ) elements. // Question: Will the change applied to the array be permanent? In other words, // is the array passed by value or by reference? for (int i = 0; i <= range; i++) a[i] += value; } int main() { const int size1 = 10, size2 = 6; int ar1[size1] = {2, 3, 12, 4, 2, 4, 2 , 21, 1, 7}; int ar2[size2] = {23, 19, 22, 90, 12, 10}; cout << "\n Here is ar1 after initialization: " << endl; for (int i = 0; i < size1; i++) cout << ar1[i] << "\t"; cout << "\n Here is ar2 after initialization: " << endl; for (i = 0; i < size2; i++) cout << ar2[i] << "\t"; cout << "\n Information of ar1 " << endl; cout << " ====================" << endl; FindSmallest( ar1, 9 ); // this will find the minimum value in the // first 10 elements, which is the entire array. cout << "\n Information of ar2 " << endl; cout << " ====================" << endl; FindSmallest( ar2, 3 ); // this will find the minimum value in the first 4 // elements only. cout << "\n Executing IncrementElement for ar1 " << endl; IncrementElement(ar1, 9, 20); // Increment all elements by 20 cout << "\n Here is ar1 after executing IncrementElement: " << endl; for ( i = 0; i < size1; i++) cout << ar1[i] << "\t"; cout << "\n Executing IncrementElement for ar2 " << endl; IncrementElement(ar2, 5, 100); // Increment all elements by 100 cout << "\n Here is ar2 after executing IncrementElement: " << endl; for ( i = 0; i < size2; i++) cout << ar2[i] << "\t"; return 0; } // If compiling the program in HP-UX, compile with +a1, since array initialization // is not implemented there by default. // The output of the program /* Here is ar1 after initialization: 2 3 12 4 2 4 2 21 1 7 Here is ar2 after initialization: 23 19 22 90 12 10 Information of ar1 ==================== The minimum value within the array is: 1 ,and it was found at index #: 8 Information of ar2 ==================== The minimum value within the array is: 19 ,and it was found at index #: 1 Executing IncrementElement for ar1 Here is ar1 after executing IncrementElement: 22 23 32 24 22 24 22 41 21 27 Executing IncrementElement for ar2 Here is ar2 after executing IncrementElement: 123 119 122 190 112 110 */