(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
- 1. Complete the routing table for R2 as per the table shown below when implementing RIP routing Protocol? (14 marks) 195.2.4.0 130.10.0.0 195.2.4.1 m1 130.10.0.2 mo R2 R3 130.10.0.1 195.2.5.1 195.2.5.0 195.2.5.2 195.2.6.1 195.2.6.0 m2 130.11.0.0 130.11.0.2 205.5.5.0 205.5.5.1 R4 130.11.0.1 205.5.6.1 205.5.6.0arrow_forwardAnalyze the charts and introduce each charts by describing each. Identify the patterns in the given data. And determine how are the data points are related. Refer to the raw data (table):arrow_forward3A) Generate a hash table for the following values: 11, 9, 6, 28, 19, 46, 34, 14. Assume the table size is 9 and the primary hash function is h(k) = k % 9. i) Hash table using quadratic probing ii) Hash table with a secondary hash function of h2(k) = 7- (k%7) 3B) Demonstrate with a suitable example, any three possible ways to remove the keys and yet maintaining the properties of a B-Tree. 3C) Differentiate between Greedy and Dynamic Programming.arrow_forward
- What are the charts (with their title name) that could be use to illustrate the data? Please give picture examples.arrow_forwardA design for a synchronous divide-by-six Gray counter isrequired which meets the following specification.The system has 2 inputs, PAUSE and SKIP:• While PAUSE and SKIP are not asserted (logic 0), thecounter continually loops through the Gray coded binarysequence {0002, 0012, 0112, 0102, 1102, 1112}.• If PAUSE is asserted (logic 1) when the counter is onnumber 0102, it stays here until it becomes unasserted (atwhich point it continues counting as before).• While SKIP is asserted (logic 1), the counter misses outodd numbers, i.e. it loops through the sequence {0002,0112, 1102}.The system has 4 outputs, BIT3, BIT2, BIT1, and WAITING:• BIT3, BIT2, and BIT1 are unconditional outputsrepresenting the current number, where BIT3 is the mostsignificant-bit and BIT1 is the least-significant-bit.• An active-high conditional output WAITING should beasserted (logic 1) whenever the counter is paused at 0102.(a) Draw an ASM chart for a synchronous system to providethe functionality described above.(b)…arrow_forwardS A B D FL I C J E G H T K L Figure 1: Search tree 1. Uninformed search algorithms (6 points) Based on the search tree in Figure 1, provide the trace to find a path from the start node S to a goal node T for the following three uninformed search algorithms. When a node has multiple successors, use the left-to-right convention. a. Depth first search (2 points) b. Breadth first search (2 points) c. Iterative deepening search (2 points)arrow_forward
- We want to get an idea of how many tickets we have and what our issues are. Print the ticket ID number, ticket description, ticket priority, ticket status, and, if the information is available, employee first name assigned to it for our records. Include all tickets regardless of whether they have been assigned to an employee or not. Sort it alphabetically by ticket status, and then numerically by ticket ID, with the lower ticket IDs on top.arrow_forwardFigure 1 shows an ASM chart representing the operation of a controller. Stateassignments for each state are indicated in square brackets for [Q1, Q0].Using the ASM design technique:(a) Produce a State Transition Table from the ASM Chart in Figure 1.(b) Extract minimised Boolean expressions from your state transition tablefor Q1, Q0, DISPATCH and REJECT. Show all your working.(c) Implement your design using AND/OR/NOT logic gates and risingedgetriggered D-type Flip Flops. Your answer should include a circuitschematic.arrow_forwardA controller is required for a home security alarm, providing the followingfunctionality. The alarm does nothing while it is disarmed (‘switched off’). It canbe armed (‘switched on’) by entering a PIN on the keypad. Whenever thealarm is armed, it can be disarmed by entering the PIN on the keypad.If motion is detected while the alarm is armed, the siren should sound AND asingle SMS message sent to the police to notify them. Further motion shouldnot result in more messages being sent. If the siren is sounding, it can only bedisarmed by entering the PIN on the keypad. Once the alarm is disarmed, asingle SMS should be sent to the police to notify them.Two (active-high) input signals are provided to the controller:MOTION: Asserted while motion is detected inside the home.PIN: Asserted for a single clock cycle whenever the PIN has beencorrectly entered on the keypad.The controller must provide two (active-high) outputs:SIREN: The siren sounds while this output is asserted.POLICE: One SMS…arrow_forward
- 4G+ Vo) % 1.1. LTE1 : Q B NIS شوز طبي ۱:۱۷ کا A X حاز هذا على إعجاب Mohamed Bashar. MEDICAL SHOE شوز طبي ممول . اقوى عرض بالعراق بلاش سعر القطعة ١٥ الف سعر القطعتين ٢٥ الف سعر 3 قطع ٣٥ الف القياسات : 40-41-42-43-44- افحص وكدر ثم ادفع خدمة التوصيل 5 الف لكافة محافظات العراق ופרסם BNI SH ופרסם DON JU WORLD DON JU MORISO DON JU إرسال رسالة III Messenger التواصل مع شوز طبي تعليق باسم اواب حمیدarrow_forwardA manipulator is identified by the following table of parameters and variables:a. Obtain the transformation matrices between adjacent coordinate frames and calculate the global transformation matrix.arrow_forwardWhich tool takes the 2 provided input datasets and produces the following output dataset? Input 1: Record First Last Output: 1 Enzo Cordova Record 2 Maggie Freelund Input 2: Record Frist Last MI ? First 1 Enzo Last MI Cordova [Null] 2 Maggie Freelund [Null] 3 Jason Wayans T. 4 Ruby Landry [Null] 1 Jason Wayans T. 5 Devonn Unger [Null] 2 Ruby Landry [Null] 6 Bradley Freelund [Null] 3 Devonn Unger [Null] 4 Bradley Freelund [Null] OA. Append Fields O B. Union OC. Join OD. Find Replace Clear selectionarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning