I have randomly generated array of floats and I need to multiply the values between min and max value of the array, for example, if the array is:
1 3 4 8 5 then product should be 12 (3 * 4) since these values are between 1 and 8.
float max = arr[0];
float min = arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
cout << max << endl << min << endl;
float product = 1;
for (int i = min; i < max; i++)
{
if (arr[i] > min && arr[i] < max)
{
product = product * arr[i];
}
}
When writen like this, it is multiplying all the value that are bigger than min and smaller than max, but I need to multiply only those between min and max. I am bit stuck, could anyone help out? Many thanks in advance.
You should write two functions. The first one will find the index of the minimal element of the array and the second one will find the maximum element of the array.
For example
1 2 3 4 5 6 7 8 9 10 11
unsignedint min_element( float a[], unsignedint size )
{
unsignedint min = 0;
for ( unsignedint i = 0; i < size; i++ )
{
if ( a[i] < a[mini] ) min = i;
}
return ( min );
}
The same way you should write function max_element. Then you should determine whether the minimum or maximum element has the smalest index and calculate the product.