Description: This lab combines the knowledge acquired in Labs 3 and 4, in particular your understanding of pointers, in

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Description: This lab combines the knowledge acquired in Labs 3 and 4, in particular your understanding of pointers, in

Post by answerhappygod »

Description This Lab Combines The Knowledge Acquired In Labs 3 And 4 In Particular Your Understanding Of Pointers In 1
Description This Lab Combines The Knowledge Acquired In Labs 3 And 4 In Particular Your Understanding Of Pointers In 1 (112.84 KiB) Viewed 27 times
The following is the code to work with:
Description: This lab combines the knowledge acquired in Labs 3 and 4, in particular your understanding of pointers, in the creation of an RPN version of the Complex Calculator You will have completed this lab when you: Created a pointerized version of the Complex Calculator from Lab 3 Implemented the calculator with an RPN interface, according to the instructions provided Can demonstrate that your code runs correctly by reproducing the sample output shown at the end of this document, based on the input values indicated; Can answer questions about the operation of your code. Note: (1) This lab builds on code created in Lab 3. If you did not fully complete that lab, then contact the instructor for assistance completing that lab first, before starting on Lab 5. (2) You must load and display the test outputs, provided in the Sample section below, executed in the VSC console PRIOR to demonstrating your code to the lab professor. You should not need to enter any keystrokes for this part of the demonstration: EVERYTHING should be loaded and displayed when you step up to have your lab evaluated. Worth 5%%%
▶ Lab 5 Lab 5 The Complex Calculator (Part 2) Introduction In Part I of this lab (i.e. Lab 3) you built a short program to do complex number arithmetic, where each complex number has the form a + Bi where the first number (a) is the real portion and the second number, the one preceded by the character 'i', (i.e. B) is the imaginary value. Two complex numbers may be added, subtracted, multiplied or divided, with the result (usually) being another complex number. Addition and subtraction of complex numbers is intuitive: you simply add the real and imaginary components separately. Thus (a + Bi) + (x Yi) = (a+x) + (B+Y) i + For multiplication and division, the results are less obvious. For example, to multiply two complex numbers the result is: Bi) Yi) * (x = (ax-BY) + (aY+Bx) i (a + Note: as stated above, Lab 3 must work correctly and completely before commencing this lab. If you did not finish Lab 3, contact your instructor to find out how to complete that lab first, before you start Lab 5. There are three new features in this lab. The first is that you'll be using pointers to complex numbers in your function calls rather than passing complex numbers directly-more on this shortly. The second feature is that, rather than deal with two declared complex numbers, c0 and cl, our code should now be able to work with any number of complex numbers, which will be stored in an array that acts like a circular queue, exactly as we encountered in Labs 2 and 4. The third new feature is the introduction of an RPN interface (for Reverse Polish Notation) for the Complex Calculator. This mathematical notation was common feature of early hand- held calculators. It has the advantage of requiring fewer keystrokes to enter values, and occupying less memory, thus requiring fewer transistors to complete the same task as a conventional calculation-an important consideration on devices with only a few kilobytes of available memory. (See https://en.wikipedia.org/wiki/Reverse_Polish_not ation for details.) The essence of RPN is that the operation to be performed (+,-, *, /, %, ^, etc.) is performed after the values have been entered, rather than between the values. Thus to add '3+ 4 =' in RPN you'd enter 3 4 +. The operation, addition, falls at the end of the calculation; hence the '+' removes the need to enter an '='. The '+' at the end says, effectively, you've entered two values, now do this operation. Furthermore, as indicated above, each individual value is stored in a stack-which is really just an array with a pointer to the 'head' of the stack- and the math operation tells the code what to do with the last two values entered (i.e. pointed to). The result of a calculation is saved to the head of the stack, where it can be reused in the next calculation, again without using an '=' sign. For example 3 * 7 + 2 = could be entered as or, alternately, 2 3 7 + 37 * 2+ (As an additional feature, RPN calculators allow the result of calculations to be copied back onto
the stack, allowing them to be reloaded with a single keystroke a feature we'll see shortly using the Copy() menu item, which replaces Move().) In this implementation of an RPN calculator, we'll follow the same logic, but using complex numbers rather than real values in our calculations. And each complex number, as it is entered, will be loaded into a circular array-the same method as Lab 4-that acts as our stack. I. Refactor the menu and its related math functions. a. Make a copy of your completed Lab3.c code, and rename it Lab5.c. Load this 'new' lab into VSC. b. As you will have noticed, much of the Lab 3 code was (intentionally) ward, involving C. reading in a complex number and returning a second complex number that held the result. This complexity was due to the fact that, in C, most small values passed to functions are pass-by-reference, hence only copies are passed, and so any calculations performed cannot impact the original values directly. This led to some inelegant code, such as: c = complexConjugate (c); The first order of business will be to rewrite this code using pointers. So the above function can should now be complexConjugate(&c); so that any changes made to the complex number c will affect the original value in memory. Start by rewriting the forward declarations for the five math-related functions associated with the Lab 3 menu items so that each implements a pointer to a complex structure, rather than passing, and returning, the value directly. The four math functions corresponding to complex plus, minus, multiply and divide will still need to return a complex data type based on the two values input by the user and return a pointer to the Complex structure. But the unitary complexConjugate () method can at least return a void, avoiding the repetitious construction shown at left in section lb. } d. At this point it is advisable to test your code using the pointerized version of the functions you just created. If you've done everything correctly, the last five choices in the menu-the five complex math operations should work the same as before, so now's a good time to check that these still work by using the Lab 3 sample output to test your code. e. The Load, Display, and Move functions will all have their operations modified to suit the stack operations (to be added in Section II). In particular, the Move() function will be performing a Copy() function it will need to copy the current value into the stack-so now would be a good time to change the wording of this feature in the menu. Also, you won't be needing chooseComplexNumberTo() any longer, since all operations will be performed on the stack-and RPN picks its own stack values to operate on; we don't. So you can safely ignore this function, since you'll be bypassing its operation in the code. f. In Lab 3 we used the loadComplex () function to enter the values of c0 and c1. Complex loadComplex () { Complex cTemp; char sign; printf("\tEnter complex number as 'a + Bi' (e.g. 3.4 27.1i): "); scanf("%lf %c %lf", &cTemp. real, &sign, &cTemp.imag); if (sign=='') cTemp.imag = -cTemp.imag; fflush(stdin); return cTemp;
To streamline this operation somewhat, use the code in the box above to input your complex numbers as a single string. II. Code the RPN operation into a circular queue that functions like a stack. a. To fully understand how the stack functions, we need look at its internal operation in more detail. Assume that we use the loadComplex () function, presented above, to enter two complex values, 1 + 3i and 2 - 7.5i (in that order), onto the stack (which loads from the bottom upward). We'll assume our stack contains, at most, 4 items (and hence we'll need an array of complex values of SIZE 4 to hold the contents of the stack at any time). The purpose of the Display() function (formerly used to display co or c1, according to the user's choice) is now to display the contents of the entire stack (and only when prompted by the user, not automatically after each operation). So following entry of the above two complex numbers, we'd expect Display() to show: Index 3 2 1 0 0.00000000+ 0.000000001 0.00000000+ 0.000000001 1.00000000 + 3.000000001 2.00000000-7.500000001 (Note: the number of digits displayed is not relevant to this lab; functionality, not formatting, is the issue here). If we now select '5' from the menu to add the bottom-most two numbers, the stack then contains 0.99999999 + 0.99999999i 9.09999999 + 0.999999901 0.99999999 + 9.99999999i 3.99999999 - 4.59999999i Index 3 1 0 That is, the complex number at location 1 was added to the complex number at location 0; the stack appears to have been "pushed down" in the process, and a '0 + Oi' value filled in at the top. A similar effect is observed for other operations. If the stack is loaded as follows: 0.00000000 + 0.000000001 7.99999999 + 12.999999991 16.00000000 13.500000001 12.00000000- 9.50990990i Selecting '6' to subtract causes c[1]-c[0] will give the result: 0.00000000 + 0.000000001 0.00000000 + 0.90999999i 7.00000000+ 12.000000001 4.00000000 - 4.000000001 The selecting '8' from the menu causes c[1]/c[0], with the result Index 3 2 1 0 + 0.000000001 0.00000000 0.00000000 + 0.00000000 + 0.000000001 0.000000001 -0.62500000 + 2.37500000i Index 3 2 1 0 Index 3 2 1 0 b. As you should recognize from Lab 2, it is inefficient to move the contents of each element of an array from location index to index-1; a far smarter way to perform the same effect is to save a pointer to the apparent 'head' of the array, and load the result of each calculation 'up' one index value, back-filling the former 'head' with '0+ Oi'. So long as the Display() function uses a pointer to the nominal 'head' of the stack for its display, and uses the % trick to loop through the array correctly, there should be no problem providing the results shown above, but without the inefficiency of moving the whole stack each time. So, for example, using the code above, the actual indices of the array (the 'head' used by
Display() is indicated with a be: a. 0.00000000 + 0.000000001 7.99999999 + 12.99999999i 16.99999999 - 13.599999991 12.00000000 - 9.500000001 0.00000000 + 0.000000001 7.99999999 + 12.99999999i 4.00000000- 4.000000001 0.00000000 + 0.000000001 ) would Following subtraction of c[1] - c[0] this becomes Index 3 2 1 0 Index 3 2 1 0 And then after the division of c [1]/c[0], this would be 0.00000000 + 0.000000001 0.00000000 + 0.000000001 0.00000000 + 0.000000001 -0.62500000 + 2.375000001 Index 3 2 0.00000000 + 0.000000001 -0.62500000 + 2.37500000i 0.00000000 + 0.000000001 1 0.00000000 + 0.000000001 0 So Display() would loop through the array, using a pointer to the second element of the array as the head of the stack, and printing out: Index I 0 3 2 III. Demonstrate your code a. Test your program execution against the sample output provided below. Remember, as with Lab 4, pointers must be used throughout the program(aside from the declaration of the array to hold complex numbers.) With the sample output displayed in the VSC console, present the output and the code to your lab professor. Be prepared to answer questions about the execution of your code. RUN YOUR CODE AND ENTER THE FOLLOWING INPUT (SEE BELOW) BEFOREHAND SO THAT IT IS CORRECTLY DISPLAYED IN YOUR VSC CONSOLE WHEN YOU SUBMIT YOUR LAB FOR EVALUATION. THIS SAVES EVERYONE TIME. Sample Output: (inputs highlighted) Welcome to the complex number calculator Select one of the following: 1. Load complex numbers 2. Copy complex numbers 3. Display complex numbers 4. Conjugate complex numbers. 5. Add complex numbers 6. Subtract complex numbers 7. Multiply complex numbers. 8. Divide complex numbers 0. Exit Program 1 Enter complex number as 'a + Bi'... 1 + 0i Select one of the following: 1. Load complex numbers 2. Copy complex numbers. ...etc. 1 Enter complex number as 'a + Bi'... 0 + li Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 1
Enter complex number as 'a + Bi'... 7 + 121 Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 1 Enter complex number as 'a + Bi'.... 2 + 21 Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 2 Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 3 0.00000000+ 1.000000001 7.00000000+ 12.000000001 2.00000000 + 2.000000001 2.00000000 + 2.00000000i Select one of the following: 1. Load complex numbers. 2. Copy complex numbers // etc. 5 Select one of the following: 1. Load complex numbers. 2. Copy complex numbers. ...etc. 4 Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 3 0.00000000+ 0.000000001 0.00000000 + 1.00000000i 7.00000000+ 12.000000001 4.00000000- 4.000000001 Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 8 Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 3 0.00000000 + 0.000000001 0.00000000 + 0.000000001 0.00000000 + -0.62500000 + Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 0 Program exited 1.000000001 2.375000001 Marking Requirements Sample output was demonstrated, and performs the correct calculations Converted all required functions to use a pointer to a structure Implemented circular queue correctly to load and store the complex values entered by the user. This includes having a pointer to the 'head' of the array, and using it correctly as complex numbers are input to the queue. Complex output is formatted correctly, and in the order specified, consistent with output expected from an RPN calculator Total: Mark 6 4 12 3 25
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply