Simple Calculator

This is a discussion on Simple Calculator within the C Programming forums, part of the General Programming Boards category; create a complex number calculator, your calculator should be able to compute the following: addition of 2 numbers subtraction of ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    16

    Simple Calculator

    create a complex number calculator, your calculator should be able to compute the following:
    addition of 2 numbers
    subtraction of 2 numbers
    multiplication of 2 numbers
    division of 2 numbers
    computation of the conjugate of a complex number z

    Sample run:
    Press a for addition, b for subtraction, c for multiplication, d for division, e for conjugate, or any other key to exit: a
    enter a complex number: 25
    Enter a complex number: 18
    (2+5i)+(1+8i)=(3+13i)

    To represent a complex number, use an array of two elements. Eg float z|2|; here z|0| corresponds to the real part and z|1| corresponds to the imaginary part of the complex number. Your program should include the following functions:
    1) void read_complex(float *z)
    2) void print_complex(float *z)
    3) void add_complex(float *z, float *z2, float *z3) //z3 z1|z2
    4) void sub_complex(float *z1, float *z2, float *z3)//z3 z1-z2
    5) void mul_complex(float *z1, float *z2, float *z3)//z3 z1*z2
    6) void div_complex(float *z1, float *z2, float *z3)//z3 z1/z2
    7) void conj_complex(float *z) //z=conj(z)

    I don't knkow what the * means before the z, or what I'm supposed to do with it. My professor doesn't exactly teach, yet expects us to do this.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Code:
    while( professor == useless ) {
       replace( professor );
    }
    He wants you to do this but hasn't taught you about pointers yet? Yeesh.

    the "float *z" part in your function declarations say that the function takes a pointer to a float as its argument. A pointer is the address of a something in memory. So you've got a float called "z", it's stored some address in memory, and the function expects you to tell it that address. You would normally call the function this way:
    Code:
    void_conjugate( &x );
    where the &x means, give us the address of x. A pointer to x is passed to the function.

    Now, you're storing your complex numbers as two-element arrays. An array is a sequence of objects in consecutive addresses in memory. So z[1] is stored directly after z[0] in memory. Referring to just z without brackets gives you the address of the array, which is the address of its first element- so "z" and "&z[0]" are the same thing.

    You can therefore call conj_complex this way: "conj_complex( z );"

    I hope this helps. Let me know if it's still obscure.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    all of that went over my head =\

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Okay, let's go for a simple example. Suppose your computer's memory contains only ten memory locations. Now do the following:
    Code:
    int x = 50;
    Your variable "x" will be stored somewhere in memory as follows:

    Code:
    Address: 1   2   3   4   5   6   7   8   9   10
    Value:   ?   ?   50  ?   ?   ?   ?   ?   ?   ?
    This means the computer has stored the 50 at memory address 3.

    If you now do
    Code:
    int *a = &x;
    This declares a variable of type "pointer to int" whose value is the address of the variable "x". Its value is 3, because that is where "x" is stored in memory.


    Now write a new program as follows:
    Code:
    int array[4] = {0,10,20,30};
    Your memory might look like this
    Code:
    Address: 1   2   3   4   5   6   7   8   9   10
    Value:   ?   ?   ?   ?   ?   ?   0   10  20  30
    So the following are true:
    &array[0] is equal to 7
    &array[1] is equal to 8
    &array[2] is equal to 9
    &array[3] is equal to 10
    array is equal to 7, because it is the same thing as &array[0].

    And all of these 5 things are pointers that can be passed to functions that expect an "int *" as an argument.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    ok, so i create a pointer for each complex number in the function, then work through the pointers, and then backwards to get integers so I can display the answers?

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You won't need to create new pointers- remember that the name of the array already is a pointer. So if I wanted to write a function that takes a complex number "a+bi" and changes it to "b+ai" I would do this

    Code:
    void swap_complex( int *q ) {
       int temp = q[0];
       q[0] = q[1];
       q[1] = temp;
       return;
    }
    and call it like this:
    Code:
    int main( void ) {
       int z[2] = {1,5};
       swap_complex( z );
       return 0;
    }
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    I still don't really understand the whole concept of complex numbers, I think I get it with pointers and such, it's just a value that can be called upon, right?

    I'm just going to try and write enough to get some credit, maybe I'll look for a tutor on my campus that can explain in person so I have more of a basis on what's going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple calculator
    By symcy in forum C Programming
    Replies: 2
    Last Post: 02-05-2011, 12:04 PM
  2. simple calculator
    By symcy in forum C Programming
    Replies: 0
    Last Post: 02-05-2011, 11:21 AM
  3. simple calculator
    By yacek in forum C Programming
    Replies: 10
    Last Post: 10-07-2010, 06:24 AM
  4. Simple calculator
    By princeyeni in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2010, 04:54 AM
  5. Help About a simple calculator pls!!
    By aaron_88 in forum C Programming
    Replies: 7
    Last Post: 01-15-2008, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21