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
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Problem Solving with C++ (10th Edition)
Management Information Systems: Managing The Digital Firm (16th Edition)
SURVEY OF OPERATING SYSTEMS
Starting Out With Visual Basic (8th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
- 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_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_forwarduse c++.arrow_forward
- C++ programarrow_forwardProblem 6 The factorial n! of a positive number (integer) is defined by n! = n · (n – 1) · (n – 2 ) ·.….. 3. 2·1, where 0! = 1. Write a user-defined function that calculates the factorial n! of a number. For function name and arguments, use y=fact(x), where the input argument x is the number whose factorial is to be calculated and the output argument y is the value x!. The function displays an error message if a negative or non-integer number is entered when the function is called. Do not use MATLAB built-in function factorial. Use fact with the following numbers: (а) 9! (b) 8.5! (c) 0! (d) -5! *Note: Name the file B000000_P6_fact.marrow_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_forward
- Problem 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_forwardc++ 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_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