Test Question 1: Write a function and return the result of division of all elements in an array by the first element.

Source: Internet
Author: User

Test Question 1: Write a function and return the result of division of all elements in an array by the first element.

Many may think of the following:

Void divaarry (int * parray, int size)

{

For (INT I = size-1; I> = 0; I --)

{

Parray [I]/= parray [0];

}

}

Question 1: Can I write the loop right now?

Question 2: Check whether the divisor is zero

Obviously, question 1 is not acceptable. If you are writing:

For (INT I = 0; I <size; I ++)

{

Parray [I]/= parray [0];

}

In this way, when I = 0 is, the first element of the array is changed to 1, and the divisor will always be 1 instead of a number of the original array, which does not meet the requirements.

After modification:

Void divarray (int * parray, int size)

{

For (INT I = size-1; I> = 0; I --)

{

If (parray [0] = 0)

Cout <"error" <Endl;

Else

Parray [I]/= parray [0];

 

}

}

This can be found in writing, but there is a problem, when the size is large enough, the efficiency is wrong, because every time you have to judge whether the first element of the array is zero, this is a waste of time, you can place it outside the loop,

The result is compiled into the following code:

Void divarray (int * parray, int size)

{

If (parray [0] = 0) cout <"error" <Endl;

Else

For (INT I = size-1; I> = 0; I --)

{

Parray [I]/= parray [0];

}

}

 

 

 

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.