(General math) a. Write a C++
b. How do you know the result your program produced is correct?
c. After verifying the output your program produces, modify it to determine the slope of the line connecting the points (2,10) and (12,6).
d. What do you think will happen if you use the points (2,3) and (2,4), which results in a division by zero? How do you think this situation can be handled?
e. If your program doesn’t already do so, change its output to this:
The value of the slope is xxx.xx
The xxx.xx denotes placing the calculated value in a field wide enough for three places to the left of the decimal point and two places to the right of it.
(a)
Program plan: -
Variables
used in the following program are given below: -
- line_slope: -To store the slope of the line
- a1, a2: - To store the coordinates point values of x1 and x2.
- b1, b2: - To store the coordinate point values of y1 and y2.
Formula used: - (b2 − b1)/(a2 − a1)
Program description: -The purpose of the C++ program is to determine the slope of the line connecting two points with the coordinates (3, 7) and (8, 12).
1
Explanation of Solution
Given information:
The slope between two points with the coordinates
Program:
//header file #include <iostream> //using the namespace usingnamespacestd; intmain() { //declaring the variables floatline_slope, a1 =3, a2 =8, b1 =7, b2 =12; //calculating the slope of the line connecting two points line_slope=(b2 - b1)/(a2 - a1); //displaying the slope of the line_slope cout<<"Slope of a line is: "<<line_slope<<endl; //return statement return0; }
Explanation:
The above code is used to calculate the slope of a line connecting the two points.
Firstly, declaring the variables of float data type. The variable line_slope will store the slope of the line, a1, a2 and b1, b2 is used to store the coordinates point values of x1, x2 and y1, y2.
The slope of a line is calculated by the given formula
Sample output: -
(b)
To verify the result of the program is correct.
Explanation of Solution
Given information: formula to calculate the slope: -
Slope of the line is 1
Explanation:
Now, calculating the slope of the line by manual calculating to verify the result of the above program.
Since, the slope of the line is 1.
Hence, verified
(c)
To modify the program of exercise by changing the value of coordinates point values.
Explanation of Solution
Given information:formula to calculate the slope: -
a1 = 2, a2 = 12 and b1 = 10, b2 = 6
Program:
//header file #include <iostream> //using the namespace usingnamespacestd; intmain() { //declaring the variables floatline_slope, a1 =2, a2 =12, b1 =10, b2 =6; //calculating the slope of the line connecting two points line_slope=(b2 - b1)/(a2 - a1); //displaying the slope of the line_slope cout<<"Slope of a line is: "<<line_slope<<endl; //return statement return0; }
Sample output: -
Explanation:
The slope of a line is calculated by same formula as used in the exercise 1.a
In the above program only the value of the coordinates point has changed.
(d)
To determine the situation while using the points (2, 3) and (2, 4) and the denominator part becomes zero.
Explanation of Solution
Given information:New coordinates point values: - (2, 3) and (2, 4)
Explanation:
While calculating the slope of a line where the coordinates point values are (2, 3) and (2, 4)thanthe values of x1, and x2 are 2 same and the values of y1, and y2 are 3 and 4,here the denominator value is zero as the expression (x2-x1) is equal to zero.
In this case, the value of the slope will be infinite. This situation can be handled by using a conditional statement given below: -
//header file #include <iostream> //using the namespace usingnamespacestd; intmain() { //declaring the variables floatline_slope, a1 =2, a2 =2, b1 =3, b2 =4; //calculating the slope of the line connecting two points line_slope=(b2 - b1)/(a2 - a1); //displaying the slope of the line_slope cout<<"Slope of a line is: "<<line_slope<<endl; //to check when the denominator is equal to zero if((a2 - a1 ==0)) { //message cout<<"The slope of the line is infinite."<<endl; } //return statement return0; }
Sample output: -
(e)
To make changes in the above program so that the output is produced in the given format: xxx.xx
cout<<"|"<<setw(5)<<setiosflags(ios::fixed)<<setprecision(2)<<5.267<<"|";
Explanation of Solution
Given information:The format of the output is xxx.xx
Program:
#include <iostream> #include <iomanip> //using the namespace //for using the standard I/O usingnamespacestd; intmain() { //declaring the variables //as per the requirement floatline_slope, a1 =12, a2 =7, b1 =8, b2 =3; //calculating the slope //by usinf the given formula line_slope=(b2 - b1)/(a2 - a1); //displaying the slope and settingthe format of output as xx.xxx //the width is set to 6 and the number of digits after decimal is //set //to 2 cout<<"The value of the slope is "<<setw(6)<<setiosflags(ios::fixed )<<setprecision(2)<<line_slope<<endl; //return statement return0; }
Sample output: -
Want to see more full solutions like this?
Chapter 3 Solutions
C++ for Engineers and Scientists
- Need help, has to be done with <stdio.h> and <math.h>arrow_forward(Software quality engineering)Derive the test cases using equivalent partitioning.arrow_forward(Attach Python file only) Using Python, define the function f, and then call it to calculate the value of f(5) given: f(0)%3D8 f9 =2 f(n-1)+14 where n is Natural number A- BI T. 8 13 |! II 123 !!arrow_forward
- (Growth of function) Express the following in the terms of Θ notation: (n + a)^ b for any real constants a and b, where b > 0. Please show your work by giving the constants c1, c2, and n0.arrow_forward(Python) Define a function calc_pyramid_volume() with parameters base_length, base_width, and pyramid_height, that returns the volume of a pyramid with a rectangular base. calc_pyramid_volume() calls the given calc_base_area() function in the calculation.Relevant geometry equations:Volume = base area x height x 1/3(Watch out for integer division).Sample output with inputs: 4.5 2.1 3.0Volume for 4.5, 2.1, 3.0 is: 9.45arrow_forward(c program only) 2. Jack `N Poyby CodeChum Admin Jack `N Poy is a very common game since our childhood days. Just thinking about it makes me reminisce how I crushed my cousin in this game until he cried ? I want to feel the glory again of being very good at something. Could you help me recreate the game? It's really simple. In Jack `N Poy, there are two players who both select either one of the following options: RockPaperScissors The winner is selected depending on the following rules: Rock beats ScissorsScissors beats PaperPaper beats RockIf both players chose the same option, then it's a tie Instructions: In the code editor, you are provided with an enum called option which contains three possible named values:ROCK - 'r'PAPER - 'p'SCISSORS - 's'Your task is to ask two users for there chosen options. And then based on their options, determine the winner.Input 1. Option selected by Player 1 2. Option selected by Player 2 Output If Player 1 wins, print the message "Player 1…arrow_forward
- Please use C PROGRAM by making the code. (Please see attached photo)arrow_forwardP5. ( Boolean Algebra Circuit. (1) Transform the following Boolean equation in SOP form to POS form: Y = F(A, B, C, D) = ĀB + CD (2) Expand the following Boolean equation into a sum of minterms, where each minterm should have the three input variables in their original or complement forms. Y = F(A, B, C) = AC + AB (3) Simplify the following Boolean equations using Boolean theorems. For each step in the minimization process, show which theorem or axiom or method or definition is used to get there. Y = ABC + B + AC + B (4) Transform the following Boolean equation to an equation that only has 2-input NAND gate(s) and/or NOT gate(s). You are not required to draw a schematic. Y = A + B + Carrow_forward(For a and b) Equation: a. f(x) = [()*+10]. 12 (3)], for x = 21,2,.. 2 b. x Use the inverse transform method to generate variates from the given equation. With the given equation, derive mathematically and numerically the mean and variance. PROVIDE CODE COMMENTS OR FURTHER EXPLANATION IF NECESSARY.arrow_forward
- (Computer Graphics)Using the Cohen-Sutherland line clipping algorithm, clip the line PQ with coordinates P (-5, 3) and Q (15, 9) against a clipping rectangular window with coordinates (0, 0) and (10, 10) as lower left and upper right corner respectively. Also, find intersection points if any.arrow_forwarduse python codearrow_forward(a) {w|w contains a 1 after a 0 and before another 0} (b) {w |w does not contain the substring 00} (c) {w |w contains an odd number of 1s or exactly one 0} 2. Use the algorithm you learned in class to convert the following NFA to an equivalent regular expression. a, A Poarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr