Practice Problem 2.32 (solution page 153)
You are assigned the task of writing code for a function tsub_ok, with arguments x and y, that will return 1 if computing x-y does not cause overflow. Having just written the code for Problem 2.30, you write the following:
/* Determine whether arguments can be subtracted without overflow */
/* WARNING: This code is buggy. */
int tsub_ok(int x, int y) {
return tadd_ok(x, -y)
}
For what values of x and y will this function give incorrect results? Writing a
correct version of this function.is left as an exercise (Problem 2.74).
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Additional Engineering Textbook Solutions
Starting Out with C++: Early Objects (9th Edition)
Software Engineering (10th Edition)
Absolute Java (6th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- splir + 1).) 7. (Numerical) Heron's formula for the area, A, of a triangle with sides of length a, b, and cie A = vIsts - aXs - b(s - c)] where (a +b+c) Write, test, and execute a function that accepts the values of a, b, and c as parameters from a calling function, and then calculates the values of s and [s(s - a)(s - b)(s - c)]. If this quantity is positive, the function calculates A. If the quantity is negative, a, b, and c do not form a triangle, and the function should set A = -1. The value of A should be returned by the function.arrow_forwardProblem 2 (5 pts) Write a Python program that plays the game Ro-Sham-Bo (a.k.a., Rock, Paper, Scissors) for a player and a computer opponent. The player selects one of the options listed in a console prompt, then the computer randomly selects an option as well. The winner of the game is determined as follows: Rock beats Scissors - Scissors beats Paper - Paper beats Rock The same selection results in a tie. Your program must do the following: Prompt the player to enter a selection in the console. Provide instructions to user on what is a valid input. For example, you can have the integers 1, 2, and 3 represent the selection, or use the words rock, paper, scissors, etc. Check that the user entered valid input. If the entry is invalid, the program needs to inform the user of the invalid input and can end, or re-prompt the user for their entry. Generate a random selection for the computer's choice. You can use a random integer and follow the same rules as was stated to the user. - Display…arrow_forward(a) Assume that five generation units with third order cost function (F, (R) = A; P+ B;P+C; P; + D;) are in the circuit. Write a computer program using any abitrary programming longuage (MATLAB, C++, C#, Python,.) to calculate economic load dipatch (ELD) using first order gradient method. Note that all parameters and variables should be defined inside the program (at tirst lines) such that units' characteristics and demand can be changed easily. Neglect grid losses. jusing dynamic programming (DP) methed.arrow_forward
- C++ programarrow_forward(a) Assume that five generation units with third order cost function (F: (P) = A: P²³ + B; P; ² + C; P; +D;) are in the circuit. Write a computer program using any arbitrary programming language (MATLAB, C++, C#, Python,...) to calculate economic. load dispatch (ELD) using first order gradient method. Note that all parameters and variables should be defined inside the program (at first lines) such that units' characteristics and demand can be changed easily. Neglect grid losses.arrow_forward(Wattan Corporation) is an Internet service provider that charges customers a flat rate of $7.99 for up to 10 hours of connection time. Additional hours or partial hours are charged at $1.99 each.Write a function charges that computes the total charge for a customer based on the number of hours of connection time used in a month. The function should also calculate the average cost per hour of the time used (rounded to the nearest 0.01), so use two output parameters to send back these results.You should write a second functionround_money that takes a real number as an input argument and returns as the function value the number rounded to two decimal places. Write a main function that takes data from an input file usage.txt and produces an output file charges.txt. The data file format is as follows:Line 1: current month and year as two integersOther lines: customer number (a five-digit number) and number of hours usedHere is a sample data file and the corresponding output file:Data file…arrow_forward
- (Wattan Corporation) is an Internet service provider that charges customers a flat rate of $7.99 for up to 10 hours of connection time. Additional hours or partial hours are charged at $1.99 each. Write a function charges that computes the total charge for a customer based on the number of hours of connection time used in a month. The function should also calculate the average cost per hour of the time used (rounded to the nearest 0.01), so use two output parameters to send back these results. You should write a second function round_money that takes a real number as an input argument and returns as the function value the number rounded to two decimal places. Write a main function that takes data from an input file usage.txt and produces an output file charges.txt. The data file format is as follows: Line 1: current month and year as two integers Other lines: customer number (a five-digit number) and number of hours used Here is a sample data file and the corresponding output file:…arrow_forwardPlease write a C++ coding with modularity using functions. 15. (Numerical) a. Euclid’s method for finding the greatest common divisor (GCD) of two positive integers consists of the following steps:Step 1: Divide the larger number by the smaller and retain the remainder.Step 2: Divide the smaller number by the remainder, again retaining the remainder.Step 3: Continue dividing the previous remainder by the current remainder until the remainder is zero, at which point the last non-zero remainder is the GCD. For example, if the two positive integers are 84 and 49, you have the following: Step 1: 84/49 yields a remainder of 35.Step 2: 49/35 yields a remainder of 14.Step 3: 35/14 yields a remainder of 7.Step 3: 14/7 yields a remainder of 0. Therefore, the last non-zero remainder, which is 7, is the GCD of 84 and 49.Using Euclid’s algorithm, replace the stub function written for Exercise 14 with an actual function that determines and returns the GCD of its two integer arguments. thank you…arrow_forwardProblem 4 (Searching a list of Student objects)Write a function called find_by_ID(students, id) that takes a list of student objects(students) and an int (id) as parameters, and returns the student object in the list withthe given id. If there is no student with the given id, it returns “NA”.See the sample output below. Looking for a student with ID = 123: Student: Alice, ID# 123 found Looking for a student with ID = 999: Student: Cathy, ID# 567 foundLooking for a student with ID = 555: NA foundarrow_forward
- c++ language, please solve question a and barrow_forward7. [10] (Display Pattern) Write a C++ function called pattern that displays a pattern of stars and numbers for a given number of columns (passed as a parameter). Maximize your use of repetition (with nested for statements) and minimize the number of cout statements. Your function should then display a pattern of the appropriate size. Write a test program (main) that generates a random number in the range of 1 to 20 (inclusive) to specify the number of columns in the pattern. Use pattern to display the pattern with this random number as the number of columns. Here are two samples: With 3 as the input: With 5 as the input: 1 1 *2 *2 **3 **3 *2 ***4 1 ****5 ***4 **3 *2 1arrow_forwardProblem 7. [ 1 point ] A function named cube_volume has already been defined. It consumes a float value and returns a float value. Call the function cube_volume with an argument of 2.5 and assign the result to a variable named cube.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education