C++ for Engineers and Scientists
C++ for Engineers and Scientists
4th Edition
ISBN: 9781133187844
Author: Bronson, Gary J.
Publisher: Course Technology Ptr
bartleby

Videos

Textbook Question
Book Icon
Chapter 3, Problem 1PP

(General math) a. Write a C++ program to calculate and display the value of the slope of the line connecting two points with the coordinates (3,7) and (8,12). Use the fact that the slope between two points with the coordinates ( x 1 , y 1 )   a n d   ( x 2 , y 2 )   i s   ( y 2     y 1 )   /   ( x 2     x 1 ) .

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)

Expert Solution
Check Mark
Program Plan Intro

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).

Program Description Answer

1

Explanation of Solution

Given information:

The slope between two points with the coordinates (x1,y1) and (x2,y2) is (y2y1)/(x2x1)

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 (y2y1)/(x2x1) .

Sample output: -

  C++ for Engineers and Scientists, Chapter 3, Problem 1PP , additional homework tip  1

(b)

Expert Solution
Check Mark
Program Plan Intro

To verify the result of the program is correct.

Explanation of Solution

Given information: formula to calculate the slope: - (y2y1)/(x2x1)

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.

  slope=( y 2 y 1)/( x 2 x 1)=5/5=1

Since, the slope of the line is 1.

Hence, verified

(c)

Expert Solution
Check Mark
Program Plan Intro

To modify the program of exercise by changing the value of coordinates point values.

Explanation of Solution

Given information:formula to calculate the slope: - (y2y1)/(x2x1)

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: -

  C++ for Engineers and Scientists, Chapter 3, Problem 1PP , additional homework tip  2

Explanation:

The slope of a line is calculated by same formula as used in the exercise 1.a(y2y1)/(x2x1)

In the above program only the value of the coordinates point has changed.

(d)

Expert Solution
Check Mark
Program Plan Intro

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: -

  C++ for Engineers and Scientists, Chapter 3, Problem 1PP , additional homework tip  3

(e)

Expert Solution
Check Mark
Program Plan Intro

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: -

  C++ for Engineers and Scientists, Chapter 3, Problem 1PP , additional homework tip  4

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
A cylinder of diameter 10 cm rotates concentrically inside another hollow cylinder of inner diameter 10.1 cm. Both cylinders are 20 cm long and stand with their axis vertical. The annular space is filled with oil. If a torque of 100 kg cm is required to rotate the inner cylinder at 100 rpm, determine the viscosity of oil. Ans. μ= 29.82poise
Make the following game user friendly with GUI, with some simple graphics The following code works as this: The objective of the player is to escape from this labyrinth. The player starts at the bottom left corner of the labyrinth. He has to get to the top right corner of the labyrinth as fast he can, avoiding a meeting with the evil dragon. The player can move only in four directions: left, right, up or down. There are several escape paths in all labyrinths. The player’s character should be able to moved with the well known WASD keyboard buttons. If the dragon gets to a neighboring field of the player, then the player dies. Because it is dark in the labyrinth, the player can see only the neighboring fields at a distance of 3 units.  Cell Class: public class Cell { private boolean isWall; public Cell(boolean isWall) { this.isWall = isWall; } public boolean isWall() { return isWall; } public void setWall(boolean isWall) { this.isWall = isWall; } @Override public String toString() {…
Please original work What are four of the goals of information lifecycle management think they are most important to data warehousing, Why do you feel this way, how dashboards can be used in the process, and provide a real life example for each. Please cite in text references and add weblinks

Chapter 3 Solutions

C++ for Engineers and Scientists

Ch. 3.1 - (Debug) Determine and correct the errors in the...Ch. 3.1 - Prob. 12ECh. 3.1 - Prob. 13ECh. 3.1 - (General math) The area of an ellipse (see Figure...Ch. 3.1 - Prob. 15ECh. 3.2 - Prob. 1ECh. 3.2 - Prob. 2ECh. 3.2 - (Practice) Write a C++ program that displays the...Ch. 3.2 - Prob. 4ECh. 3.2 - Prob. 5ECh. 3.2 - Prob. 6ECh. 3.2 - Prob. 7ECh. 3.2 - Prob. 8ECh. 3.2 - (Electrical eng.) The combined resistance of three...Ch. 3.2 - Prob. 10ECh. 3.2 - Prob. 11ECh. 3.2 - (Civil eng.) Write a C++ program to calculate and...Ch. 3.3 - Prob. 1ECh. 3.3 - Prob. 2ECh. 3.3 - (Practice) Write C++ statements for the following:...Ch. 3.3 - Prob. 4ECh. 3.3 - (General math) Write, compile, and run a C++...Ch. 3.3 - (General math) If a 20-foot ladder is placed on...Ch. 3.3 - (Physics) The maximum height reached by a ball...Ch. 3.3 - (Transportation) Road construction requires...Ch. 3.3 - Prob. 9ECh. 3.3 - Prob. 10ECh. 3.3 - Prob. 11ECh. 3.3 - Prob. 12ECh. 3.4 - Prob. 1ECh. 3.4 - (Practice) a. Write a C++ program that first...Ch. 3.4 - Prob. 3ECh. 3.4 - Prob. 4ECh. 3.4 - Prob. 5ECh. 3.4 - Prob. 6ECh. 3.4 - (General math) a. Write, compile, and run a C++...Ch. 3.4 - Prob. 8ECh. 3.4 - Prob. 9ECh. 3.4 - (Electrical eng.) For the series circuit shown in...Ch. 3.4 - Prob. 11ECh. 3.4 - Prob. 12ECh. 3.4 - Prob. 13ECh. 3.5 - Prob. 1ECh. 3.5 - Prob. 2ECh. 3.5 - Prob. 3ECh. 3.5 - Prob. 4ECh. 3.5 - Prob. 5ECh. 3.6 - Prob. 1ECh. 3.6 - (General math) The value of p can be approximated...Ch. 3.6 - Prob. 3ECh. 3.6 - (General math) The volume of oil stored in an...Ch. 3.6 - Prob. 5ECh. 3.6 - (General math) The perimeter, approximate surface...Ch. 3.6 - Prob. 7ECh. 3.6 - Prob. 8ECh. 3.6 - Prob. 9ECh. 3 - (General math) a. Write a C++ program to calculate...Ch. 3 - General math) a. Write a C++ program to calculate...Ch. 3 - (General math) Modify the program written for...Ch. 3 - (Biology) The number of bacteria, B, in a culture...Ch. 3 - Prob. 5PPCh. 3 - (Heat transfer) The formula developed in Exercise...Ch. 3 - Prob. 7PPCh. 3 - (Electrical eng.) a. The voltage gain of an...Ch. 3 - (Electrical eng.) a. Write, compile, and run a C++...Ch. 3 - (Electrical eng.) The amplification of electronic...Ch. 3 - (Acoustics) The loudness of a sound is measured in...Ch. 3 - (General math) a. A balance has the following...
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Boolean Algebra - Digital Logic and Logic Families - Industrial Electronics; Author: Ekeeda;https://www.youtube.com/watch?v=u7XnJos-_Hs;License: Standard YouTube License, CC-BY
Boolean Algebra 1 – The Laws of Boolean Algebra; Author: Computer Science;https://www.youtube.com/watch?v=EPJf4owqwdA;License: Standard Youtube License