1. Engineering
  2. Computer Science
  3. please do as instructed in the prompt below using the...

Question: please do as instructed in the prompt below using the...

Question details
Please do as instructed in the prompt below using the text pages below that. This is C++. Thank you
The goal of assignment 2 is to reinforce implementation of ADT concepts in C++. Specifically, the assignment is to do problem 2.15 on pages 94 of the text. Your code must implement the style guidelines of the author (preconditions, postconditions, etc.). Your solution needs to have 3 separate files. Please name the files as following: rational.h Class definition or header file rational.cpp -Implementation file that contains all implemented functions of the rational class rational test.cpp-test file to test the rational clas. Create at least 2 objects of the rational class and test ALL functions defined in the class. Make sure your output has proper labels for inputs as well as output. If you are working on your local machines, please make sure that your final code can be compiled and executed on the cloudland server.
Test if final value ofcontrol variable has counter not been 10 true cout << counter << endl Body of loop (this may be many statements) Increment the control variable false Fig. 2.19 Flowcharting a typical for repetition structure 2.15 Examples Using the for Structure The following examples show methods of varying the control variable in a for structure In each case, we write the appropriate for header. Note the change in the relational oper- ator for loops that decrement the control variable. a) Vary the control variable from 1 to 100 in increments of 1. for ( int i- 1; i <= 100; i++ ) b) Vary the control variable from 100 to 1 in increments of -1 (decrements of 1) for int 100 i 1, i- Common Programming Error 2.17 Not using the proper relational operator in the loop-continuation condition of a loop that a logic error that will yield incorrect results when the program runs Vary the control variable from 7 to 77 in steps of 7 d counts downwards (such as incorrectly using ї <-1 in a loop counting down to l ) is usually c) for (int í-7;i<-77; i +-7) Vary the control variable from 20 to 2 in steps of -2 for ( int í . 20, í>. 2; í--2) d) e) Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14 17, 20 for int -21 20,3 )
92 CONTROL STRUCTURES CHAPTER 2 Vary the control variable over the following sequence of values: 99, 88,77, 66 55, 44, 33, 22, 11, 0 1) for ( int -99 05 -11 The next two examples provide simple applications of the Eor structure. The program of Fig. 2.20 uses the for structure to sum all the even integers from 2 to rightmost portion of the f 00 or header by using the comma operator as follows: / initialization Note that the body of the for structure in Fig. 2.20 could actually be merged into the for int number2 number 100: umumber, number II continuation condition 2) /1 total and increment The initialization sum- O could also be merged into the initialization section of the for. Good Programming Practice 2.23 Although statements preceding a for and statements in the body of a for can often be merged into the for header, avoid doing so becouse it can make the program more difficalt to read Good Programming Practice 2.24 Limit the size of control structure headers to a single line if possible The next example computes compound interest using the for structure. Consider the following problem statement i1 Fig. 2.20: figo2 20.epp 2 I/ Summation with for #include <iostream.h> 5 int main) int sum-0 for int number 2; number 100: number+2) 10 sumnumber: 12 Cout <Sum is<< sum << endl 13 14 return 0 15 Sum is 2550 Fig. 2.20 Summation with for
CHAPTER 2 CONTROL STRUCTURES 93 A pernon imes s1o0o.o n a saving count yekling S perceninmerest Assuming thr all interest is left on deposit in the account, calenlate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts: aspl+r) where p is the original amount invested (i.e., the principal) r is the annual interest rate, n is the number of years, and a is the amount on deposit at the end of the nth year This problem involves a loop that performs the indicated calculation for each of the 10 years the money remains on deposit. The solution is shown in Fig. 2.21 The for structure executes the body of the loop 10 times, varying a control variable from 1 to 10 in increments of 1. C++ does not include an exponentiation operator, so we use the standard library function pow for this purpose. The function pow(x, y) calcu- lates the value of x raised to the yth power. Function pow takes two arguments of type double and returns a double value. The type double is a floating-point type much like float, but a variable of type double can store a value of much greater magnitude with greater precision than float. Constants (like 1000.0 and.05 in Fig. 2.21) are treated as being type double by C++. 1 I Fig. 2.21: fig02 21.cpp 2 I/ Calculating compound interest 3 #inelude <iostream.h 4 #include domanip.h> 5 #include <math.h» 7 int main() double amount, /I anount on deposit 10 principal -1000.0. 11 starting principal rate- .05 11 interest rate 12 3 cout << Year <setwl 21 14 15 16 for int year - 1, year 10; year+ <Amount on deposit << endl: 17 18 amount principal po 1.0 rate, year) cout << setw(4 year << setiosflagst ios::fixed I ios::showpoint ) 20 21 <<setw 21 << setprecision 2) << amount << endl: 23 24 return 0 25 Fig. 221 Calculating compound interest with for (oar 1 of 2),
CHAPTEN ONTROL STRUCT RES Amount on deposit 1050.00 1102.50 1157.62 1215.51 1276.28 1340.10 1407.10 1477-46 1551.33 1628.89 Year 10 Fig. 2.21 Calculating compound Interest with for (part 2 of 2). This program would not compile without the inclusion of math.h. Function pow requires two double arguments. Note that year is an integer. The math.h file includes information that tells the compiler to convert the value of year to a temporary double representation before calling the function. This information is contained in pows function prototype. Function prototypes are explained in Chapter 3. We provide a summary of the pow function and other math library functions in Chapter 3. Common Programming Error 2.18 Forgetting to include the math.h file in a program that uses math library functions is asym lax error Notice that we declared the variables amount, principal, and rate to be of type double. We have done this for simplicity because we are dealing with fractional parts of dollars and we need a type that allows decimal points in its values. Unfortunately, this can cause trouble. Here is a simple explanation of what can go wrong when using float or double to represent dollar amounts (assuming printing is done with setpreci- sion (2)): Two float dollar amounts stored in the machine could be 14.234 (which prints as 14.23) and 18.673 (which prints as 18.67). When these amounts are added, they produce the internal sum 32.907 which prints as 32.91. Thus your printout could appear as 14.23 +18.67 32.91 but a person adding the individual numbers as printed would expect the sum 32.90! You have been warned! Good Programming Practice 2.25 Do not use variables of type float or double to perform monetary calculations. The i precision of floating-point numbers can cause errors that will result in incorrect moneor values. In the exercises, we explore the use of integers to perform monetary calculations Note: C++ class libraries are becoming available for properly perfonming monetary col lations.
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution