(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
- The following is code for a disc golf program written in C++: // player.h #ifndef PLAYER_H #define PLAYER_H #include <string> #include <iostream> class Player { private: std::string courses[20]; // Array of course names int scores[20]; // Array of scores int gameCount; // Number of games played public: Player(); // Constructor void CheckGame(int playerId, const std::string& courseName, int gameScore); void ReportPlayer(int playerId) const; }; #endif // PLAYER_H // player.cpp #include "player.h" #include <iomanip> Player::Player() : gameCount(0) {} void Player::CheckGame(int playerId, const std::string& courseName, int gameScore) { for (int i = 0; i < gameCount; ++i) { if (courses[i] == courseName) { // If course has been played, then check for minimum score if (gameScore < scores[i]) { scores[i] = gameScore; // Update to new minimum…arrow_forwardIn this assignment, you will implement a multi-threaded program (using C/C++) that will check for Prime Numbers and Palindrome Numbers in a range of numbers. Palindrome numbers are numbers that their decimal representation can be read from left to right and from right to left (e.g. 12321, 5995, 1234321). The program will create T worker threads to check for prime and palindrome numbers in the given range (T will be passed to the program with the Linux command line). Each of the threads works on a part of the numbers within the range. Your program should have some global shared variables: • numOfPrimes: which will track the total number of prime numbers found by all threads. numOfPalindroms: which will track the total number of palindrome numbers found by all threads. numOfPalindromic Primes: which will count the numbers that are BOTH prime and palindrome found by all threads. TotalNums: which will count all the processed numbers in the range. In addition, you need to have arrays…arrow_forwardHow do you distinguish between hardware and a software problem? Discuss theprocedure for troubleshooting any hardware or software problem. give one reference with your answer.arrow_forward
- You are asked to explain what a computer virus is and if it can affect computer’shardware or software. How do you protect your computer against virus? give one reference with your answer.arrow_forwardDistributed Systems: Consistency Models fer to page 45 for problems on data consistency. structions: Compare different consistency models (e.g., strong, eventual, causal) for distributed databases. Evaluate the trade-offs between availability and consistency in a given use case. Propose the most appropriate model for the scenario and explain your reasoning. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440AZF/view?usp=sharing]arrow_forwardOperating Systems: Deadlock Detection fer to page 25 for problems on deadlock concepts. structions: • Given a system resource allocation graph, determine if a deadlock exists. If a deadlock exists, identify the processes and resources involved. Suggest strategies to prevent or resolve the deadlock and explain their trade-offs. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440 AZF/view?usp=sharing]arrow_forward
- Artificial Intelligence: Heuristic Evaluation fer to page 55 for problems on Al search algorithms. tructions: Given a search problem, propose and evaluate a heuristic function. Compare its performance to other heuristics based on search cost and solution quality. Justify why the chosen heuristic is admissible and/or consistent. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440 AZF/view?usp=sharing]arrow_forwardRefer to page 75 for graph-related problems. Instructions: • Implement a greedy graph coloring algorithm for the given graph. • Demonstrate the steps to assign colors while minimizing the chromatic number. • Analyze the time complexity and limitations of the approach. Link [https://drive.google.com/file/d/1wKSrun-GlxirS3IZ9qoHazb9tC440 AZF/view?usp=sharing]arrow_forwardRefer to page 150 for problems on socket programming. Instructions: • Develop a client-server application using sockets to exchange messages. • Implement both TCP and UDP communication and highlight their differences. • Test the program under different network conditions and analyze results. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qo Hazb9tC440AZF/view?usp=sharing]arrow_forward
- Refer to page 80 for problems on white-box testing. Instructions: • Perform control flow testing for the given program, drawing the control flow graph (CFG). • Design test cases to achieve statement, branch, and path coverage. • Justify the adequacy of your test cases using the CFG. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS3IZ9qo Hazb9tC440 AZF/view?usp=sharing]arrow_forwardRefer to page 10 for problems on parsing. Instructions: • Design a top-down parser for the given grammar (e.g., recursive descent or LL(1)). • Compute the FIRST and FOLLOW sets and construct the parsing table if applicable. • Parse a sample input string and explain the derivation step-by-step. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440 AZF/view?usp=sharing]arrow_forwardRefer to page 20 for problems related to finite automata. Instructions: • Design a deterministic finite automaton (DFA) or nondeterministic finite automaton (NFA) for the given language. • Minimize the DFA and show all steps, including state merging. • Verify that the automaton accepts the correct language by testing with sample strings. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qo Hazb9tC440AZF/view?usp=sharing]arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr