Not Yet Answered # Complex Numbers

Discussion Starter jcr1 kc0arf 53 Discussion Starter jcr1 Discussion Starter jcr1 Narue 5,707 Narue 5,707 Discussion Starter jcr1 frrossk 2 Discussion Starter jcr1 jwenting 1,594 Discussion Starter jcr1 jwenting 1,594 Discussion Starter jcr1 alc6379 105 OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...

-1

This is what i have at the moment.#include <stdio.h>

void main()

{

int a_real,a_imaginary;

int b_real,b_imaginary;

printf ("a real = ");

scanf ("%d",&a_real);

printf ("a imaginary = ");

scanf ("%d",&a_imaginary);

printf ("b real = ");

scanf ("%d",&b_real);

printf ("b imaginary = ");

scanf ("%d",&b_imaginary);

}

0

Hello,

It might help to know what type of calculator program you are looking for. You might want to structure it so that the user has to input a function...

```
Welcome to the Calculator Program.
Would you like to Add, Subtract, Divide, or Multiply (A,S,D,M)?
You chose to _
Please enter in your 1st Number: __
Please enter in your 2nd Number: __
Your answer is: ____
Would you like to calculate another?
```

Now, keep these things in mind:

* DO NOT ALLOW YOUR COMPUTER TO DIVIDE BY ZERO. That could crash out the computer, and give the user a nasty message. Put a check in there to ensure that b <> 0

* Be careful with the imaginary numbers. Review how they work.

* If you have to add trig functions, then make sure they follow the rules too.

* How are you going to handle exponents?

Some enhancement ideas:

* Implement a memory function that can store a value to be compared with later.

* Implement a small array, say 25 numbers, that you can average together or sort out (kinda like an adding machine thing).

Christian

0

cheers, i do need the user to put in a function either +,-,/,*.To calculate the complex number e.g. (1+j2) + (-6-j6) and it has to output the results in cartesian co-ordinates.

0

What is wrong with the if statement, according to my notes its right but it keeps telling me ! expression syntax.

```
#include <stdio.h>
void main()
{
int a_real,a_imaginary;
int b_real,b_imaginary;
int complex_number;
int enter_operator;
printf ("a real = ");
scanf ("%d",&a_real);
printf ("a imaginary = ");
scanf ("%d",&a_imaginary);
printf ("b real = ");
scanf ("%d",&b_real);
printf ("b imaginary = ");
scanf ("%d",&b_imaginary);
printf ("Enter operator = ");
scanf ("%d",&enter_operator);
if (enter_operator = +);
complex_number = (a_real+b_real)+j(a_imaginary+b_imaginary);
if (enter_operator = -)
complex_number = (a_real-b_real)+j(a_imaginary-b_imaginary);
}
```

*Edited 3 Years Ago by Dani*: Formatting fixed

0

>according to my notes its right

And your notes have to be correct? Nope, sorry.

>void main()

int main ( void )

>int enter_operator;

char enter_operator;

>scanf ("%d",&enter_operator);

scanf ("%c",&enter_operator);

>if (enter_operator = +);

if (enter_operator == '+')

>if (enter_operator = -)

else if (enter_operator == '-')

>}

return 0;

}

Clear?

0

>That,s no good either

Then you're doing it wrong.

>it's given me more errors

Please start giving us workable information instead of complaining about your mythical non-working code. Your idiocy isn't entertaining anymore. More specifically, post the errors you get, and the code that causes them.

-1

```
int main (void)
{
int a_real,a_imaginary,j;
int b_real,b_imaginary;
int complex_number;
char enter_operator;
printf ("a real = ");
scanf ("%d",&a_real);
printf ("a imaginary = ");
scanf ("%d",&a_imaginary);
printf ("b real = ");
scanf ("%d",&b_real);
printf ("b imaginary = ");
scanf ("%d",&b_imaginary);
printf ("Enter operator = ");
scanf ("%c",&enter_operator);
if (enter_operator == '+') complex_number = (a_real+b_real)+j(a_imaginary+b_imaginary);
printf ("\ncomplex number is %d\n",complex_number);
else if (enter_operator == '-');
complex_number = (a_real-b_real)+j(a_imaginary-b_imaginary);
printf ("\ncomplex number is %d\n",complex_number);
else if (enter_operator == '/');
complex_number = ((a_real*b_real+a_imaginary*b_imaginary)/(b_real*b_real)+(b_imaginary*b_imaginary))+j((a_imaginary*b_real-a_real*b_imaginary)/(b_real*b_real+b_imaginary*b_imaginary));
printf ("\ncomplex number is %d\n",complex_number);
else if (enter_operator == '*');
complex_number = ((a_real*b_real)-(a_imaginary*b_imaginary))+j((a_real*b_imaginary)+(a_real*b_real));
printf ("\ncomplex number is %d\n",complex_number);
return 0;
}
```

errors

call of notification

misplaced else

code has no effect

*Edited 3 Years Ago by happygeek*: fixed formatting

0

I assume #include <stdio.h>

```
int main (void)
{
int a_real,a_imaginary,j;
int b_real,b_imaginary;
int complex_number;
char enter_operator;
printf ("a real = ");
scanf ("%d",&a_real);
printf ("a imaginary = ");
scanf ("%d",&a_imaginary);
printf ("b real = ");
scanf ("%d",&b_real);
printf ("b imaginary = ");
scanf ("%d",&b_imaginary);
printf ("Enter operator = ");
scanf ("%c",&enter_operator);
if (enter_operator == '+')
{
complex_number = (a_real+b_real)+j(a_imaginary+b_imaginary);
printf ("\ncomplex number is %d\n",complex_number);
}
else if (enter_operator == '-')
{
complex_number = (a_real-b_real)+j(a_imaginary-b_imaginary);
printf ("\ncomplex number is %d\n",complex_number);
}
else if (enter_operator == '/')
{
complex_number = ((a_real*b_real+a_imaginary*b_imaginary)/(b_real*b_real)+(b_imaginary*b_imaginary))+j((a_imaginary*b_real-a_real*b_imaginary)/(b_real*b_real+b_imaginary*b_imaginary));
printf ("\ncomplex number is %d\n",complex_number);
}
else if (enter_operator == '*')
{
complex_number = ((a_real*b_real)-(a_imaginary*b_imaginary))+j((a_real*b_imaginary)+(a_real*b_real));
printf ("\ncomplex number is %d\n",complex_number);
}
return 0;
}
```

First, if in an **if** statement you have more than one single instruction, you have to put these instructions between { and }

Second, **else if (expr)** does not ever end with a **;**

0

Whats up with this program it lets me put the numbers in but dose not output the answer from the calculations?

```
#include <stdio.h>
int main (void)
{
int a_real,a_imaginary;
int b_real,b_imaginary;
int complex_number;
char j;
char enter_operator;
printf ("a real = ");
scanf ("%d",&a_real);
printf ("a imaginary = ");
scanf ("%d",&a_imaginary);
printf ("b real = ");
scanf ("%d",&b_real);
printf ("b imaginary = ");
scanf ("%d",&b_imaginary);
printf ("Enter operator = ");
scanf ("%c",&enter_operator);
if (enter_operator == '+')
{
complex_number += (a_real+b_real)+(a_imaginary+b_imaginary);
printf ("\ncomplex number is %c\n",complex_number);
}
else if (enter_operator == '-')
{
complex_number -= (a_real-b_real)+(a_imaginary-b_imaginary);
printf ("\ncomplex number is %c\n",complex_number);
}
else if (enter_operator == '/')
{
complex_number /= ((a_real*b_real+a_imaginary*b_imaginary)/(b_real*b_real)+(b_imaginary*b_imaginary))+((a_imaginary*b_real-a_real*b_imaginary)/(b_real*b_real+b_imaginary*b_imaginary));
printf ("\ncomplex number is %c\n",complex_number);
}
else if (enter_operator == '*')
{
complex_number *= ((a_real*b_real)-(a_imaginary*b_imaginary))+((a_real*b_imaginary)+(a_real*b_real));
printf ("\ncomplex number is %c\n",complex_number);
}
return 0;
}
```

0

don't start a new thread when dealing with your old problem...

http://www.daniweb.com/techtalkforums/thread13701.html

and use code tags and hopefully indentation to make your code somewhat readable.

Have you tested what the value of your "enter_operator" actually is after the input?

Should it even go into one of your if-statements?

0

sorry am in a rush with this program,whats code tags and how do i check what the input is ?

0

http://www.daniweb.com/techtalkforums/announcement.php?f=8&announcementid=3

ever heard of debuggers (or debug statements?).

0

```
#include <stdio.h>
int main (void)
{
int a_real,a_imaginary;
int b_real,b_imaginary;
int complex_number;
char j;
char enter_operator;
printf ("a real = ");
scanf ("%d",&a_real);
printf ("a imaginary = ");
scanf ("%d",&a_imaginary);
printf ("b real = ");
scanf ("%d",&b_real);
printf ("b imaginary = ");
scanf ("%d",&b_imaginary);
printf ("Enter operator = ");
scanf ("%c",&enter_operator);
if (enter_operator == '+')
{
complex_number += (a_real+b_real)+(a_imaginary+b_imaginary);
printf ("\ncomplex number is %c\n",complex_number);
}
else if (enter_operator == '-')
{
complex_number -= (a_real-b_real)+(a_imaginary-b_imaginary);
printf ("\ncomplex number is %c\n",complex_number);
}
else if (enter_operator == '/')
{
complex_number /= ((a_real*b_real+a_imaginary*b_imaginary)/(b_real*b_real)+(b_imaginary*b_imaginary))+((a_imaginary*b_real-a_real*b_imaginary)/(b_real*b_real+b_imaginary*b_imaginary));
printf ("\ncomplex number is %c\n",complex_number);
}
else if (enter_operator == '*')
{
complex_number *= ((a_real*b_real)-(a_imaginary*b_imaginary))+((a_real*b_imaginary)+(a_real*b_real));
printf ("\ncomplex number is %c\n",complex_number);
}
return 0;
}
```

0

don't start a new thread when dealing with your old problem...

http://www.daniweb.com/techtalkforums/thread13701.html

and use code tags and hopefully indentation to make your code somewhat readable.

Have you tested what the value of your "enter_operator" actually is after the input?

Should it even go into one of your if-statements?

Went ahead and merged these two threads together. Let's make sure that we keep topics all inclusive in one thread.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I don’t want at this stage work on a big separate project as I've already got plenty ...