NOTE: if you copy the code that i have provide and paste with out adding the three features I will report you because I

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

NOTE: if you copy the code that i have provide and paste with out adding the three features I will report you because I

Post by answerhappygod »

NOTE: if you copy the code that i have provide and paste without adding the three features I will report you because I posted 3times and didn't get the solution.
There are three new features in this lab:
I have provide the SORUCE CODE ....
Note If You Copy The Code That I Have Provide And Paste With Out Adding The Three Features I Will Report You Because I 1
Note If You Copy The Code That I Have Provide And Paste With Out Adding The Three Features I Will Report You Because I 1 (71.1 KiB) Viewed 123 times
/* Revision 1.1. loadComplex() changed, to return Complexstructure */#include <stdlib.h>#include <stdio.h>
typedef struct{double real;double imag;} Complex;
#define Exit 0#define Load 1#define Display 2#define Move 3#define Conjugate 4#define Add 5#define Subtract 6#define Multiply 7#define Divide 8
#define PROMPT_USER_TO(menuItem) printf("%d. %s complexnumbers\n", menuItem, #menuItem);
// Forward declarations// TODO: add other forward declarations, as requiredint displayMenu();void executeMenuItem(int menuItem);Complex loadComplex();int chooseComplexNumberTo(char *);// etc...void display(Complex c);Complex complexConjugate(Complex c);Complex add(Complex a, Complex b);Complex multiply(Complex c1, Complex c0);Complex divide(Complex c1, Complex c0);Complex subtract(Complex c1, Complex c0);
// DO NOT CHANGE THIS FUNCTIONint main(void){int choice = Exit;printf("Welcome to the complex number calculator\n\n");do{choice = displayMenu();executeMenuItem(choice);} while (choice != Exit);
printf("Program exited\n");exit(EXIT_SUCCESS);}
// display menu options, prompt the user to enter a choice, andreturn the value selected// DO NOT CHANGE THIS FUNCTIONint displayMenu(){int input = Exit;printf("Select one of the following:\n\n");PROMPT_USER_TO(Load)PROMPT_USER_TO(Display)PROMPT_USER_TO(Move)PROMPT_USER_TO(Conjugate)PROMPT_USER_TO(Add)PROMPT_USER_TO(Subtract)PROMPT_USER_TO(Multiply)PROMPT_USER_TO(Divide)printf("%d. Exit Program\n", Exit);scanf("%d", &input);return input;}
// DO NOT CHANGE THIS FUNCTIONvoid executeMenuItem(int menuItem){Complex c = {0, 0}; // c is used for temporary storage of Complexnumbers; it is not used in calculationsstatic Complex c0 = {0, 0}, c1 = {0, 0}, cResult = {0, 0}; // Usethese values for all operations below// Note that cResult is never used in calculations directly, onlyto store the result of calculations
switch (menuItem){
case Exit:break; // do not edit this; 'Exit' is handled in the 'while'statement
case Load: // load c0 or c1 with real and imaginary valuesif (chooseComplexNumberTo("load"))c1 = loadComplex();elsec0 = loadComplex();break;
/* TODO: write the following methods to move, display,conjugate, add, etc. theComplex number stored in c0 and c1, as indicated. The methodchooseComplexNumberTo()has already been written for you (below); it should not bealtered.*/case Display: // displays the result of a computationc = chooseComplexNumberTo("display") ? c1 : c0;display(c);break;
case Move: // move the cResult to c1 or c0// use the value indicated by chooseComplexNumberTo("set equal tothe result") to// determine whether cResult is copied to c1 or c0if (chooseComplexNumberTo("Move"))c1 = cResult;elsec0 = cResult;
break;
case Conjugate: // converts c to c*, i.e. real + imag *i ->real - imag * i// use chooseComplexNumberTo("conjugate") and then conjugate c1 orc0 accordinglyif (chooseComplexNumberTo("Conjugate")){ // store the conjugated result back in c, i.e. c -> c*c1 = complexConjugate(c1);display(c1);}else{c0 = complexConjugate(c0);display(c0);}break;
case Add: // add c0 + c1 and store to cResultcResult = add(c1, c0);display(cResult);break;
case Subtract: // subtract c1 - c0 and store to cResultcResult = subtract(c1, c0);display(cResult);break;
case Multiply: // etc....multiply c1 * c0cResult = multiply(c1, c0);display(cResult);break;case Divide: // etc....divide c1/c0 using (c1 * c0*)/(c0 *c0*)// use an assert() statement to check for division by 0+0i
cResult = divide(c1, c0);display(cResult);break;default:printf("Bad menu value entered; ");}printf("\n");}
// DO NOT CHANGE THIS FUNCTIONComplex loadComplex(){Complex cTemp;printf("\tEnter the real component : ");scanf("%lf", &cTemp.real);
printf("\tEnter the imaginary component : ");scanf("%lf", &cTemp.imag);printf("\n");return cTemp;}
// DO NOT CHANGE THIS FUNCTIONint chooseComplexNumberTo(char *operation){int choice = 0;printf("\nWhich complex number do you wish to %s? (enter 0 or 1):", operation);scanf("%d", &choice);return choice;}
// TODO: add all additional funtions here, as specified above inthe executeMenuItem() method.void display(Complex c){double r = c.real;double i = c.imag;int sign = i < 0 ? -1;/*if (i < 0){sign = -1;}else{sign = 1;}*/printf(" %.3lf %c %.3lfi", r, sign == -1 ? '-' : '+', sign *i);}
Complex complexConjugate(Complex c){c.imag = -1 * c.imag;return c;}Complex add(Complex a, Complex b){Complex cr = {0, 0};cr.real = a.real + b.real;cr.imag = a.imag + b.imag;return cr;}Complex subtract(Complex a, Complex b){Complex cr = {0, 0};cr.real = a.real - b.real;cr.imag = a.imag - b.imag;return cr;}Complex multiply(Complex c1, Complex c0){Complex cr = {0, 0};double a = c0.real, x = c1.real;double B = c0.imag, Y = c1.imag;cr.real = (a * x) - (B * Y);cr.imag = (a * Y) + (x * B);return cr;}Complex divide(Complex c1, Complex c0){Complex cr = {0, 0};Complex c0_conj = complexConjugate(c0);Complex numerator = multiply(c1, c0_conj);Complex denominator = multiply(c0, c0_conj);cr.real = numerator.real / denominator.real;cr.imag = numerator.imag / denominator.real;if (c0.real == 0 && c0.imag == 0){printf("division by 0 not possible");break;}return cr;}
▶Lab 5 The Complex Calculator (Part 2) Due: during your lab period between July II and July 15, 2022. 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 2.5% of your total mark
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: (a + Bi) + Yi) = (ax-BY) + (aY+Bx) i 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 a 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, 237 + 37 * 2+ (As an additional feature, RPN calculators allow the result of calculations to be copied back onto ▸ Page 1
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. 1. 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) awkward, involving 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. c. 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, > Page 2 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 ce and c1. Complex loadComplex () { Complex cTemp; char sign; printf("\tEnter complex number as 'a + Bi' (e.g. 3.4 27.11): "); scanf("%lf %c %1f", &cTemp.real, &sign, &cTemp.imag); if (sign=='') cTemp.imag fflush(stdin); return cTemp; = -cTemp.imag;
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+ 31 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 doubles of SIZE 4 to hold the contents of the stack at any time). The purpose of the Display() function (formerly used to display ce 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: 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). Index 3 2 1 0 If we now select '5' from the menu to add the bottom-most two numbers, the stack then contains 0.00000000+ 0.000000001 0.00000000+ 0.000000001 0.00000000+ 0.000000001 3.00000000 4.500000001 - Index 3 NH G 2 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.00000000+ 12.000000001 16.00000000 13.500000001 12.00000000- 9.500000001 0 0.00000000 + 0.000000001 0.00000000 + 0.000000001 7.09999999 + 12.999999991 4.00000000- 4.000000001 Index 3 Selecting '6' to subtract causes c[1]-c[0] will give the result: The selecting '8' from the menu causes c[1]/c[0], with the result 0.00000000 + 0.000000001 0.00000000 + 0.000000001 0.00000000 + 0.000000001 -0.62500000 + 2.375000001 2 1 Index 3 2 1 0 Index 3 218 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 Page 3
Display() is Indicated with a be: 0.00000000 + 0.000000001 7.00000000+ 12.000000001 16.00000000 13.500000001 12.00000000 9.500000001 - 7.00000000+ 12.000000001 4.00000000 4.000000001 0.00000000 + 0.000000001 ) would Following subtraction of c[1]c[0] this becomes Index 3 2 Index 0.00000000 + 0.000000001 3 2 1 0 0.00000000 + 0.000000001 0.00000000 + 0.000000001 0.00000000 + 0.000000001 -0.62500000 + 2.375000001 1 ▸ Page 4 10 And then after the division of c[1]/C[0], this would be 0 0.00000000 + 0.000000001 -0.62500000 + 2.375000001 0.00000000 + 0.000000001 0.00000000 + 0.000000001 0 1 Index 3 2 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.) a. 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 + Oi 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
Lab 5 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 + 2i 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.00000000₫ 2.00000000 + 2.000000001 2.00000000 + 2.000000001 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.000000001 7.00000000+ 12.000000001 4.00000000- 4.00000000i 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 + 1.000000001 -0.62500000 + 2.375000001 Select one of the following: 1. Load complex numbers 2. Copy complex numbers ...etc. 0 Program exited 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 ▸ Page 5
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply