Homework Solution: In the previous exercise you wrote a C++ program to compute the slope of a line. For example, give…

    In the previous exercise you wrote a C++ program to compute the slope of a line. For example, given the points (1, 3) and (4, 10) input as follows:
    1
    3
    4
    10
    
    The program computes the slope and outputs both the points, and the resulting slope:
    (1,3)
    (4,10) 
    Slope: 2.33333
    
    Recall that the slope is defined as rise / run, or (y2-y1) / (x2-x1). In the previous exercise you were allowed to assume that the run would never be 0, thus avoiding division by 0. This assumption is no longer true. Since division by 0 is mathematically undefined, your task in this assignment is to check for a run == 0.0, and output “Slope: undefined” in this case. Example: given the inputs
    1
    3
    1
    -3
    
    Your program should now output
    (1,3)
    (1,-3)
    Slope: undefined
    
    In other words, your program should work as before except when the run is 0, in which case the output should be “Slope: undefined”. Hint: use an if-then-else statement to check the run, and decide whether to output the error message, or compute the slope and output. Feel free to read ahead to Chapter 3 (in particular section 3.1) for an overview of if-then-else statements.

    Expert Answer

     
    Cpp program: #include<iostream>

    In the anterior training you wrote a C++ program to calculate the float of a direction. Coercion issue, dedicated the points (1, 3) and (4, 10) input as follows:

    1
    3
    4
    10
    

    The program calculates the float and outputs twain the points, and the upshoting float:

    (1,3)
    (4,10) 
    Slope: 2.33333
    

    Recall that the float is defined as mount / pass, or (y2-y1) / (x2-x1). In the anterior training you were known to postulate that the run would never be 0, thus avoiding disruption by 0. This impudence is no longer gentleman.

    Since disruption by 0 is mathematically shadowy, your toil in this provision is to stay coercion a pass == 0.0, and output “Slope: shadowy” in this instance. Issue: dedicated the inputs

    1
    3
    1
    -3
    

    Your program should now output

    (1,3)
    (1,-3)
    Slope: shadowy
    

    In other articulation, your program should exertion as antecedently bar when the pass is 0, in which instance the output should be “Slope: shadowy”. Hint: right an if-then-else declaration to stay the pass, and career whether to output the fallacy communication, or calculate the float and output. Feel unimpeded to recognize coercionward to Chapter 3 (in feature exception 3.1) coercion an overview of if-then-else declarations.

    Expert Reply

     

    Cpp program:

    #include<iostream>

    using namespace std;

    int deep()

    {

    //declaring variables

    int x1,x2,y1,y2;

    inclose slop;

    //Asking rightr to penetrate inputs(points)

    cout<<“Penetrate 4 points:(Input as:x1,x2,y1,y2):”;

    cin>>x1 >> y1 >>x2 >>y2; //reading inputs

    if((x2-x1)==0) //condition whether denomenotor gives 0 or not

    {

    cout<<“Slope: shadowy”; //displays the communication if (x2-x1==0)

    }

    else //else condtion to prints the slop

    {

    slop=(double)(y2-y1)/(x2-x1); //casting upshot into inclose postulates type

    cout << “Slop: ” <<slop<< endl; //displaying slop value

    }

    return 0;

    }

    ouput: