SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Self-test Exercises
Question:26 | ISBN:9780321531346 | Edition: 7

Question

Consider the quadratic expression x2 − 4x + 3 Describing where this quadratic is negative involves describing a set of numbers that are simultaneously greater than the smaller root (+1) and less than the larger root (+3). Write a C++ Boolean expression that is true when the value of this quadratic is negative.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
#include <cmath>
using namespace std;

//Program starts a main method
int main() 
{
    // Declare the coefficients of the quadratic expression
    int a = 1; // coefficient of x^2
    int b = -4; // coefficient of x
    int c = 3; // constant term
    
    // Calculate the discriminant to determine the nature of the roots
    int discriminant = b * b - 4 * a * c;
    
    // Check if the discriminant is greater than zero (real and distinct roots)
    if (discriminant > 0) {
        // Calculate the roots of the quadratic equation
        float root1 = (-b + sqrt(discriminant)) / (2 * a);
        float root2 = (-b - sqrt(discriminant)) / (2 * a);
        
        // Check if the expression is negative in the interval (root1, root2)
        if (root1 < root2) {
            // Check if a number falls within the range (root1, root2)
            float numberToCheck = 2; // Replace with the desired number
            
            // Check if the number falls within the range
            if (numberToCheck > root1 && numberToCheck < root2) {
                cout << "The quadratic expression is negative when x is between " 
                     << root1 << " and " << root2 << endl;
                return true;
            }
        }
    }
    
    cout << "The quadratic expression is not negative." << endl;
    return false;
}

 

OUTPUT OF THE PROGRAM CODE:

The quadratic expression is not negative.

 

0 0

Discussions

Post the discussion to improve the above solution.