R
You don't have the whole program code right.First, the size of the mass should have an entire type. Otherwise, the number of elements allocated for the mass and cycles used to fill it may not be consistent.For example, it is allowed that variable value n equals 1.5♪ Then there will be a mass of one element, as the fragmented portion of the number will be dropped.
However, in this cyclefor(i=0;i<n;i++)
*(a+i)=random(21)-10;
two terations for variable values i 0 and 1, both of which are less than 1.5. As a result, it will be applied to an index 1 element that does not exist. The programme is therefore inconclusive.The function is also not correct.The function is returned to a type object double which will be converted to type intbecause it's the type of reset function. The function would therefore be irrelevant if the returned object had a fragmentation.int maxs(double *, int );
^^^
When a function is called, it is not clear why the size of the body is reduced.maxs(a,n-1);
^^^^
The number of elements would therefore be misrepresented within the function.In these proposalsr1=maxs(a,n/2);
r2=maxs(a+n/2+1,n-1);
The idea is looking for the maximum elements of two floors of the body. However, the number of elements in the second half is not correctr2=maxs(a+n/2+1,n-1);
^^^^
This function also has undetermined behaviour.You can make it easy for yourself to make sure that you're doing this simple example.#include <iostream>
#include <algorithm>
int maxs (double *a,int n){
double r1,r2;
if(n==0)
return a[0];
r1=maxs(a,n/2);
r2=maxs(a+n/2+1,n-1);
return std:: max(r1,r2);
}
int main()
{
double a[] = { 1.1, 2.2, 3.3, 100.123 };
std::cout << maxs( a, 2 ) << std::endl;
}
In this programme, the function shall be started for a sub-base consisting of edments.
1.1, 2.2, 3.3, as the second parameter is 2. However, the function returns the number 100 (not 100.123) instead of the correct value 3.3.It would be appropriate to write a function in such a way that it would return the maximum element index. The function will then return the index of the equal length of the mass, that is, 0, to an empty body, thus distinguishing whether it is real or empty.That's what a function looks like.size_t max_element( const double *a, size_t n )
{
if ( n < 2 )
{
return 0;
}
else
{
size_t i = max_element( a, n / 2 );
size_t j = n / 2 + max_element( a + n / 2 , n - n / 2 );
return a[i] < a[j] ? j : i;
}
}
At first, she's checking how many elements in the body, i.e. less than two or no. If the cells in the mass are less than two, then either the mass consists of one element or empty, the index of this element shall be returned.Otherwise the mass is divided into two floors: the elements with the indices are smaller n / 2 and elements with indices, starting with n /2 and before n♪ And the function is recurring for each of the floors of the reference range. Then, on the basis of the re-established function of the maximum element indices for each half of the mass, these two maximum elements shall be compared and the largest index shall in turn be reset from function.This is a demonstration programme that gives rise to your original function and function proposed by me for sub-basement of three elements.#include <iostream>
#include <algorithm>
size_t max_element( const double *a, size_t n )
{
if ( n < 2 )
{
return 0;
}
else
{
size_t i = max_element( a, n / 2 );
size_t j = n / 2 + max_element( a + n / 2 , n - n / 2 );
return a[i] < a[j] ? j : i;
}
}
int maxs (double *a,int n){
double r1,r2;
if(n==0)
return a[0];
r1=maxs(a,n/2);
r2=maxs(a+n/2+1,n-1);
return std:: max(r1,r2);
}
int main()
{
double a[] = { 1.1, 2.2, 3.3, 100.123 };
std::cout << maxs( a, 2 ) << std::endl;
std::cout << a[max_element( a, 3 )] << std::endl;
}
Programme withdrawal100
3.3
As can be seen from the conclusion, your function returns a value that is not even part of sub-basic { 1.1, 2.2, 3.3}, while the second function returns the correct value. 3.3♪The function is simple. Suppose there is a set of four elements {a[0], a[1], a[2], a[3]}. A function is performed with the address of the first element of the mass and the number of elements.max_element( a, 4 )
In its body, the function causes itself for the sub-masses {a[0], a[1]} and {a[2], a[3]}, as follows: max_element( a, 2 ) <== max_element( a, n / 2 )
andmax_element( a + 2, 2 ) <== max_element( a + т . 2, т - n / 2 )
I mean, the maximum element of the whole body is searched as the highest value of the two floors of the body.a[0] a[1] a[2] a[3]
| | | |
max1 max2
max
Each recurring challenge to the function reverts the maximum element index to the sub-massium by dividing the reference mass into two halfs. These indices are listed as variables i and jSo, after a recurring challenge, there are two maximum cell indices for the first half of the mass and for the second half of the mass, respectively.size_t i = max_element( a, n / 2 );
size_t j = n / 2 + max_element( a + n / 2 , n - n / 2 );
Then compare these maximum elements of each of the sexes corresponding to the indices received and eventually reset from the function the index of that element of the two, which is the maximumreturn a[i] < a[j] ? j : i;