Problem:
Write a function to return the result of all elements in the array being divided by the first element, including the first element.
Analysis:
Pay attention to two points: 1. Determine whether the input is valid; 2. Determine whether the divisor is 0; 3. Remove from the back (really cool)
Code implementation:
1/* div_array.cc 2*2014/09/03 create 3 * write a function to return the result of all elements in the array being divided by the first element, including the first element, you also need to remove yourself 4 */5 # include <iostream> 6 using namespace STD; 7 8 void div_array (int * parray, int size) {9 // judge whether the input is valid 10 if (parray = NULL | size = 0) 11 return; 12 // determine whether the divisor is 013 if (parray [0] = 0) 14 return; 15 // divide from the back to the front by the first element 16 for (INT I = size-1; I> = 0; I --) 17 parray [I]/= parray [0]; 18} 19 20 int main () {21 int array [5] = {3, 12, 7, 5, 1}; 22 div_array (array, sizeof (array) /sizeof (array [0]); 23 for (INT I = 0; I <sizeof (array)/sizeof (array [0]); I ++) 24 cout <array [I] <"; 25 return 0; 26}
Output:
$ ./a.exe1 4 2 1 0
Beauty of programming-write a function that returns the result of division of all elements in the array by the first element