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];
}
}