Beauty of programming-write a function that returns the result of division of all elements in the array by the first element

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.