Answer to Question #265889 in C++ for zxc

Question #265889

Given an integer array of size n write a function that finds the maximum product of two integers in it.


1
Expert's answer
2021-11-14T09:21:12-0500
using namespace std;
void GetMaxProduct(int arr[], int n)
{
    int MaxP = INT_MIN;
    int ii, jj,i,j;


    if (n < 2)	return;
 
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (MaxP < arr[i] * arr[j])
            {
                MaxP = arr[i] * arr[j];
                ii = i;
				jj = j;
            }
        }
    }
    cout<<"\n\tMax. Product Pair: ("<<arr[ii]<<", "<<arr[jj]<<")";
}
 
int main()
{
    int l,Num[] = { -20, -3, 4, 5, -2 };
	l = sizeof(Num) / sizeof(Num[0]);
    GetMaxProduct(Num, l);
    return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS