Create a C++ program where you will use nested loops to solve a system of linear
ID: 3862106 • Letter: C
Question
Create a C++ program where you will use nested loops to solve a system of linear inequalities, given limits on the domains of x and y. Your program should use this system of inequalities: 2x-y < 4 and x + 3y > 1 Given these inequalities, your program should take four values as input: xLow, xHigh (both integers) - Limits on the domain of x yLow, yHigh (both integers) - Limits on the domain of y Your program should use nested for loops to extend the x value from xLow to xHigh and to extend y from yLow to yHigh, in increments of one unit for each variable. For each pair (x, y) check the system of inequalities for a valid solution; if the values of x and y result in a “true” value for both inequalities, the (x, y) pair satisfies the system. If the (x, y) pair satisfies the system, output the pair to the screen. Test Case #1: Enter your x limits (low high): 1 5 Enter your y limits (low high): 2 7 (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (4, 5) (4, 6) (4, 7) (5, 7) Test Case #2: Enter your x limits (low high): 3 4 Enter your y limits (low high): 4 5 (3, 4) (3, 5) (4, 5) Test Case #3: Change your inequalities to be y < cos(x) and sin(x) > y. Enter your x limits (low high): 0 4 Enter your y limits (low high): 0 4 (1, 0) Test Case #4: Use the inequalities from Test Case #3. Enter your x limits (low high): 1 5 Enter your y limits (low high): 1 5 There are no solutions.
Explanation / Answer
#include<iostream>
using namespace ::std;
int main() {
int xlow, xhigh, ylow, yhigh, i, j, flag = 0;
cout<<"Enter your x limits (low high): ";
cin>>xlow>>xhigh;
cout<<"Enter your y limits (low high): ";
cin>>ylow>>yhigh;
for (i = xlow; i<=xhigh; i++)
for (j = ylow; j<=yhigh; j++)
if ((2*i-j < 4) && (i + 3*j > 1)){
cout<<"("<<i<<","<<j<<") ";
flag = 1;
}
if (flag == 0)
cout<<"There are no solutions.";
cout<<endl;
return 0;
}
for 3 & 4
#include<iostream>
#include<math.h>
using namespace ::std;
int main() {
int xlow, xhigh, ylow, yhigh, i, j, flag = 0;
cout<<"Enter your x limits (low high): ";
cin>>xlow>>xhigh;
cout<<"Enter your y limits (low high): ";
cin>>ylow>>yhigh;
for (i = xlow; i<=xhigh; i++)
for (j = ylow; j<=yhigh; j++)
if ((j < cos(i)) && (sin(i) > j)){
cout<<"("<<i<<","<<j<<") ";
flag = 1;
}
if (flag == 0)
cout<<"There are no solutions.";
cout<<endl;
return 0;
}