#include // We will plot a linear inequality using characters. // // Given the inequality // // a b // y <= --- x + ---, // d d // // we will plot all integer points (x, y) in the // square // // -10 <= x <= 10 // -10 <= y <= 10. // // At each point: // // - print '*' if the inequality is strict, // - print 'o' if there is equality, // - otherwise, print a space. // // We will also draw the coordinate axes: // // - print '+' at the origin, // - print '|' on the y-axis, // - print '-' on the x-axis. // // The axes take precedence over the inequality. // Thus, if a point on the line also lies on an // axis, the axis character is printed instead. // // First, query the user for integer values of // 'a', 'b', and 'd'. We require d > 0. If the // user enters d <= 0, simply return 0. // // The variable 'x' represents the abscissa, while // 'y' represents the ordinate. See: // // https://en.wikipedia.org/wiki/Abscissa_and_ordinate // // Thus, for each integer point (x, y), print: // // If x == 0 and y == 0, print '+'. // // Otherwise, if x == 0, print '|'. // // Otherwise, if y == 0, print '-'. // // Otherwise, determine whether // // a b // y < --- x + ---, // d d // // in which case print '*'; whether // // a b // y == --- x + ---, // d d // // in which case print 'o'; or otherwise // print a space. // // ******************* WARNING ******************* // // Because we are using integer arithmetic, we // CANNOT correctly test the inequality using // // y < (a/d)*x + b/d. // // Integer division would calculate 'a/d' and // 'b/d' separately and discard any fractional // parts. // // Instead, because d > 0, we can multiply both // sides by 'd' without changing the direction // of the inequality: // // d*y < a*x + b. // // Similarly, equality can be tested using // // d*y == a*x + b. // // Question: // // What happens if you instead test // // y <= (a/d)*x + b/d // // using integer arithmetic? Try several values // for 'a', 'b', and 'd' where the divisions are // not exact. Can you explain the results? // // ******************* WARNING ******************* // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // We want something that looks like the following: // // | // | // | // | // | * // | *** // | o**** // | ****** // | ******** // |o********* // ----------+---------- // **|********** // o***|********** // *****|********** // *******|********** // o********|********** // **********|********** // **********|********** // **********|********** // **********|********** // **********|********** // // This is the plot for // // 3 2 // y <= --- x + ---. // 5 5 // // Each line of output corresponds to one value // of 'y'. // // On the first row, y = 10 while 'x' takes every // integer value from -10 through 10. // // On the next row, y = 9 while 'x' again takes // every integer value from -10 through 10. // // This continues until the final row, where // y = -10. // // Why must 'y' decrease rather than increase as // we print successive rows? // The outer loop should therefore be: // // for ( int y{ 10 }; y >= -10; --y ) { // // Print one complete row. // } // // Notice that the largest value of 'y' appears // at the top of the plot, just as it does on a // conventional Cartesian coordinate system. // For every value of 'y', the inner loop allows // 'x' to increase from -10 through 10: // // for ( int y{ 10 }; y >= -10; --y ) { // for ( int x{ -10 }; x <= 10; ++x ) { // // Decide whether to print // // '+', '|', '-', '*', 'o', or ' '. // } // // // What must be printed after all // // 21 points in one row are complete? // } // Here is one solution: /* int main() { int a{}; std::cout << "Enter 'a' in y = (a/d)x + b/d: "; std::cin >> a; int b{}; std::cout << "Enter 'b' in y = (a/d)x + b/d: "; std::cin >> b; int d{}; std::cout << "Enter a positive value for 'd': "; std::cin >> d; if ( d <= 0 ) { return 0; } for ( int y{ 10 }; y >= -10; --y ) { for ( int x{ -10 }; x <= 10; ++x ) { if ( (x == 0) && (y == 0) ) { std::cout << "+"; } else if ( x == 0 ) { std::cout << "|"; } else if ( y == 0 ) { std::cout << "-"; } else if ( d*y < a*x + b ) { std::cout << "*"; } else if ( d*y == a*x + b ) { std::cout << "o"; } else { std::cout << " "; } } std::cout << std::endl; } return 0; } */