Please solve the mathematics problem using nested for loops in C++. Given 2 inte
ID: 3934910 • Letter: P
Question
Please solve the mathematics problem using nested for loops in C++.
Given 2 integers x and y, x [0, 100] and y [25, 75]. Find the values of x and y that maximize the expression xy - y 2 (y squared) .
Your program should display the solutions in this format, where the # symbols should be replaced by your solutions.
x=#, y=# will maximize the expression.
The max value of the expression is #.
Hint: nested for loops work in this manner:
for ( ; ; ) // if this loop will circle 7 times,
{ for ( ; ; ) // and if this loop will circle 3 times,
{
statements; // then the statements will run 7x3=21 times.
}
}
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int x,y;
int max=INT_MIN;
int max_x,max_y;
for(int x=0;x<=100;x++)
{
for (int y =25; y<=75; y++)
{
if((x*y-y*y)>max)
{
max=(x*y-y*y);
max_x=x;
max_y=y;
}
}
}
cout<<"x = "<<max_x<<endl;
cout<<"y = "<<max_y<<endl;
return 0;
}