#include // Query the user for integer coefficients 'a', // 'b', and 'c', and an integer constant 'd'. // // Find all single-digit integer solutions to // // ax + by + cz = d. // // By a single-digit integer, we mean any integer // from -9 through 9, inclusive. Thus, consider // every possible ordered triple (x, y, z) for // which // // -9 <= x <= 9, // -9 <= y <= 9, // -9 <= z <= 9. // // For each solution, print the ordered triple // (x, y, z). // // If no solutions are found, print // // No solutions found. // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // This will require three nested for loops, // one for each variable: // // for ( int x{ ... }; ...; ++x ) { // for ( int y{ ... }; ...; ++y ) { // for ( int z{ ... }; ...; ++z ) { // // Does (x, y, z) satisfy // // the equation? // } // } // } // // You will also need to remember whether at // least one solution has been found. Why might // a variable of type 'bool' be useful? // Here is one solution: /* int main() { std::cout << "We are considering the linear equation " << "ax + by + cz = d." << std::endl; int a{}; std::cout << "Enter the coefficient 'a': "; std::cin >> a; int b{}; std::cout << "Enter the coefficient 'b': "; std::cin >> b; int c{}; std::cout << "Enter the coefficient 'c': "; std::cin >> c; int d{}; std::cout << "Enter the constant 'd': "; std::cin >> d; bool found{ false }; for ( int x{ -9 }; x <= 9; ++x ) { for ( int y{ -9 }; y <= 9; ++y ) { for ( int z{ -9 }; z <= 9; ++z ) { if ( a*x + b*y + c*z == d ) { found = true; std::cout << "(x, y, z) = (" << x << ", " << y << ", " << z << ")" << std::endl; } } } } if ( !found ) { std::cout << "No solutions found." << std::endl; } return 0; } */ // Interesting exercise: // // Instead of simply printing the ordered triple, // suppose we want to print the substitution into // the equation itself. // // A direct approach may produce awkward output // such as // // 1*-1 + -2*0 + 3*-1 = -4 // 1*0 + -2*-4 + 3*-4 = -4 // 1*0 + -2*2 + 3*0 = -4 // 1*1 + -2*-8 + 3*-7 = -4 // // We would prefer output such as // // 1*(-1) - 2*0 + 3*(-1) = -4 // 1*0 - 2*(-4) + 3*(-4) = -4 // 1*0 - 2*2 + 3*0 = -4 // 1*1 - 2*(-8) + 3*(-7) = -4 // // Can you use conditional statements to format // the signs and negative values appropriately? /* std::cout << a << "*"; if ( x < 0 ) { std::cout << "(" << x << ")"; } else { std::cout << x; } if ( b < 0 ) { std::cout << " - " << -b << "*"; } else { std::cout << " + " << b << "*"; } if ( y < 0 ) { std::cout << "(" << y << ")"; } else { std::cout << y; } if ( c < 0 ) { std::cout << " - " << -c << "*"; } else { std::cout << " + " << c << "*"; } if ( z < 0 ) { std::cout << "(" << z << ")"; } else { std::cout << z; } std::cout << " = " << d << std::endl; */