
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
![## Problem 1: Write a Program in a Language of Your Choice
### Description:
Write a program in a language of your choice (preferably Python, or R) using the following algorithm.
### Algorithm:
```plaintext
procedure add(a, b: positive integers)
{the binary expansions of a and b are (aₙ₋₁aₙ₋₂...a₁a₀)₂ and (bₙ₋₁bₙ₋₂...b₁b₀)₂, respectively}
c := 0
for j := 0 to n - 1
d := [(aⱼ + bⱼ + c) / 2]
sⱼ := aⱼ + bⱼ + c - 2d
c := d
sₙ := c
return (s₀, s₁, ..., sₙ) {the binary expansion of the sum is (sₙsₙ₋₁...s₀)₂}
```
### Explanation:
- The procedure `add(a, b)` takes two positive integers, `a` and `b`, as input.
- These integers are represented in binary as sequences of bits.
- The binary expansions of `a` and `b` are given as (aₙ₋₁aₙ₋₂...a₁a₀)₂ and (bₙ₋₁bₙ₋₂...b₁b₀)₂ respectively.
- Initialize a carry value `c` to 0.
- Loop over each bit position `j` from 0 to n-1:
- Calculate the value `d` as the integer division of the sum of the corresponding bits from `a`, `b`, and the carry `c`.
- Assign the result of the bit-wise sum minus double `d` to `sⱼ`.
- Update the carry `c` with the value of `d`.
- After the loop, assign the final carry value `c` to `sₙ`.
- Return the binary expansion of the sum as a sequence of bits (s₀, s₁, ..., sₙ).
### Figure 1: Adding Two Base 2 Numbers
(This may refer to a visual representation like a flowchart or diagram showing the steps taken](https://content.bartleby.com/qna-images/question/e872339c-719c-4a81-bc7c-37653a99cb41/b3ebeade-7b8e-402d-918d-b96c1132d502/j29kxo6_thumbnail.jpeg)
Transcribed Image Text:## Problem 1: Write a Program in a Language of Your Choice
### Description:
Write a program in a language of your choice (preferably Python, or R) using the following algorithm.
### Algorithm:
```plaintext
procedure add(a, b: positive integers)
{the binary expansions of a and b are (aₙ₋₁aₙ₋₂...a₁a₀)₂ and (bₙ₋₁bₙ₋₂...b₁b₀)₂, respectively}
c := 0
for j := 0 to n - 1
d := [(aⱼ + bⱼ + c) / 2]
sⱼ := aⱼ + bⱼ + c - 2d
c := d
sₙ := c
return (s₀, s₁, ..., sₙ) {the binary expansion of the sum is (sₙsₙ₋₁...s₀)₂}
```
### Explanation:
- The procedure `add(a, b)` takes two positive integers, `a` and `b`, as input.
- These integers are represented in binary as sequences of bits.
- The binary expansions of `a` and `b` are given as (aₙ₋₁aₙ₋₂...a₁a₀)₂ and (bₙ₋₁bₙ₋₂...b₁b₀)₂ respectively.
- Initialize a carry value `c` to 0.
- Loop over each bit position `j` from 0 to n-1:
- Calculate the value `d` as the integer division of the sum of the corresponding bits from `a`, `b`, and the carry `c`.
- Assign the result of the bit-wise sum minus double `d` to `sⱼ`.
- Update the carry `c` with the value of `d`.
- After the loop, assign the final carry value `c` to `sₙ`.
- Return the binary expansion of the sum as a sequence of bits (s₀, s₁, ..., sₙ).
### Figure 1: Adding Two Base 2 Numbers
(This may refer to a visual representation like a flowchart or diagram showing the steps taken
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 4 steps with 2 images

Knowledge Booster
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
- (Data processing) Your professor has asked you to write a C++ program that determines grades at the end of the semester. For each student, identified by an integer number between 1 and 60, four exam grades must be kept, and two final grade averages must be computed. The first grade average is simply the average of all four grades. The second grade average is computed by weighting the four grades as follows: The first grade gets a weight of 0.2, the second grade gets a weight of 0.3, the third grade gets a weight of 0.3, and the fourth grade gets a weight of 0.2. That is, the final grade is computed as follows: 0.2grade1+0.3grade2+0.3grade3+0.2grade4 Using this information, construct a 60-by-7 two-dimensional array, in which the first column is used for the student number, the next four columns for the grades, and the last two columns for the computed final grades. The program’s output should be a display of the data in the completed array. For testing purposes, the professor has provided the following data:arrow_forwardWhen you perform arithmetic operations with operands of different types, such as adding an int and a float, ____________. C# chooses a unifying type for the result you must choose a unifying type for the result you must provide a cast you receive an error messagearrow_forward15. Write a program to implement and test the algorithm that you designed for Exercise 15 of Chapter 1. (You may assume that the value of . In your program, declare a named constant PI to store this value.)arrow_forward
- (Statistical) In many statistical analysis programs, data values considerably outside the range of the majority of values are simply dropped from consideration. Using this information, write a C++ program that accepts up to 10 floating-point values from a user and determines and displays the average and standard deviation of the input values. All values more than four standard deviations away from the computed average are to be displayed and dropped from any further calculation, and a new average and standard deviation should be computed and displayed.arrow_forward(Numerical) Write and test a function that returns the position of the largest and smallest values in an array of double-precision numbers.arrow_forward(Computation) Among other applications, Pascal’s triangle (see Figure 7.22) provides a means of determining the number of possible combinations of n things taken r at a time. For example, the number of possible combinations of five people (n = 5) taken two at a time (r=2)is10. Each row of the triangle begins and ends with 1. Every other element in a row is the sum of the element directly above it with the element to the left of the one above it. That is, element[n][r]=element[n1][r]+element[n1][r1] Using this information, write and test a C++ program to create the first 11 rows of a twodimensional array representing Pascal’s triangle. For any given value of n less than 11 and r less than or equal to n, the program should display the correct element. Use your program to determine in how many ways a committee of 8 can be selected from a group of 10 peoplearrow_forward
- (Conversion) a. Write a C++ program to convert meters to feet. The program should request the starting meter value, the number of conversions to be made, and the increment between metric values. The display should have appropriate headings and list the meters and the corresponding feet value. If the number of iterations is greater than 10, have your program substitute a default increment of 10. Use the relationship that 1 meter = 3.281 feet. b. Run the program written in Exercise 6a on a computer. Verify that your program begins at the correct starting meter value and contains the exact number of conversions specified in your input data. c. Modify the program written in Exercise 6a to request the starting meter value, the ending meter value, and the increment. Instead of the condition checking for a fixed count, the condition checks for the ending meter value. If the number of iterations is greater than 20, have your program substitute a default increment of (ending value - starting value) / 19.arrow_forward(Thermodynamics) a. Design, write, compile, and run a program that determines the work,W, performed by a piston engine providing a force of 1000 N over a distance of 15 centimeters. The following formula is used to determine the work performed: W=Fd F is the force provided by the piston in Newtons. d is the distance the piston moves in meters. b. Manually check the values computed by your program. After verifying that your program is working correctly, modify it to determine the work performed by six pistons, each providing a force of 1500 N over a distance of 20 centimeters.arrow_forward(Numerical) Using the srand() and rand() C++ library functions, fill an array of 1000 floating-point numbers with random numbers that have been scaled to the range 1 to 100. Then determine and display the number of random numbers having values between 1 and 50 and the number having values greater than 50. What do you expect the output counts to be?arrow_forward
- (Electrical eng.) a. An engineer has constructed a two-dimensional array of real numbers with three rows and five columns. This array currently contains test voltages of an amplifier. Write a C++ program that interactively inputs 15 array values, and then determines the total number of voltages in these ranges: less than 60, greater than or equal to 60 and less than 70, greater than or equal to 70 and less than 80, greater than or equal to 80 and less than 90, and greater than or equal to 90. b. Entering 15 voltages each time the program written for Exercise 7a runs is cumbersome. What method could be used for initializing the array during the testing phase? c. How might the program you wrote for Exercise 7a be modified to include the case of no voltage being present? That is, what voltage could be used to indicate an invalid voltage, and how would your program have to be modified to exclude counting such a voltage?arrow_forward(Data processing) Years that are evenly divisible by 400 or are evenly divisible by 4 but not by 100 are leap years. For example, because 1600 is evenly divisible by 400, 1600 was a leap year. Similarly, because 1988 is evenly divisible by 4 but not by 100, it was also a leap year. Using this information, write a C++ program that accepts the year as user input, determines whether the year is a leap year, and displays a message telling the user whether the entered year is or is not a leap year.arrow_forward(Numerical) a. Write a C++ program that accepts an integer argument and determines whether the passed integer is even or odd. (Hint: Use the % operator.) b. Enter, compile, and run the program written for Exercise 8a.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage

C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning

C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr

Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,

Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning

EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage