How to multiply values in array?

Hello everyone!

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.
1
2
3
4
5
6
7
for (int i = min; i < max; i++)
{
    if (arr[i] > min && arr[i] < max)
    {
        product = product * arr[i];
    }
}


You meant this I hope:

1
2
3
4
5
6
7
for (int i = 0; i < n; i++)
{
    if (arr[i] > min && arr[i] < max)
    {
        product = product * arr[i];
    }
}
It's not sufficient to keep track of the min and max values, you also need to keep track of their index.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iterator>
#include <numeric>
#include <utility>

int main()
{
	int a[] = { 8, 3, 4, 1, 5, };

	std::pair<int *, int *> p = std::minmax_element( std::begin( a ), std::end( a ) );

	if ( std::distance( std::begin( a ), p.second ) < std::distance( std::begin( a ), p.first ) )
	{
		std::swap( p.first, p.second );
	}

	if ( p.first != p.second && ++p.first != p.second )
	{
		int product = std::accumulate( p.first, p.second, 1, std::multiplies<int>() );
		std::cout << "The product is equal to " << product << std::endl;
	}
}
Last edited on
Thank's everyone. Vlad, thank you, but I am at the very beggining of C++ and could not understand what needs to be done from your code.

Well, I tried to keep track on their indexes, but still nothing.. maybe additional help could be provided. This is how far I have got.



float max = arr[0];
float min = arr[0];
int minIndex = 0, maxIndex = 0;
float temp, temp2;

for (int i = 0; i < n; i++)
{

if (arr[i] < min)
{
min = arr[i];
minIndex = i;
}

if (arr[i] > max)
{
max = arr[i];
maxIndex = i;
}

if(max < min)
{
temp = max;
max = min;
min = temp;
}
}
cout << minIndex << endl << maxIndex << endl;


float product = 1.00;

for (int i = minIndex + 1; i < maxIndex; i++)
{
product = product * arr[i];
}

cout << "Product of values between maximum and minimum values: " << product << endl;
Last edited on
Could you use [code][/code] tags?

And what output are you getting? (compared to what you are expecting.)
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
unsigned int min_element( float a[], unsigned int size )
{
   unsigned int min = 0;

   for ( unsigned int 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.
Last edited on
Thanks for help everyone! Came out with this such resolution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
	float max = arr[0];
	float min = arr[0];
	int minIndex = 0, maxIndex = 0;
	float temp, temp2;

	for (int i = 0; i < n; i++)
	{
	
		if (arr[i] < min)
		{
			min = arr[i];
			minIndex = i;			
		}

		if (arr[i] > max)
		{
			max = arr[i];
			maxIndex = i;
		}
	}

	float product = 1.00;
	if (maxIndex > minIndex)
	{
		for (int i = minIndex + 1; i < maxIndex; i++)
		{
			product = product * arr[i];
                }	
         }
    if (maxIndex < minIndex)
   {
		for (int i = maxIndex + 1; i < minIndex; i++)
		{
			product = product * arr[i];
                }	
    }
    if ((maxIndex + 1 == minIndex) || (maxIndex == minIndex + 1))
    {
        product =0;                                                
    }	
	cout << "Product of values between maximum and minimum values: " << product << endl;
Last edited on
Topic archived. No new replies allowed.