# How do I divide / multiply two variables?

• 02-26-2013
Cofex
How do I divide / multiply two variables?
Hello great programmers out there!
I am new to C programming and I am just wondering how to multiply / divide two different variables which the user type in as the promt is asking like this:

Code:

```void inmatning3 (double *a, double *b) {   printf("Mata in tv\217 stycken flyttal: "); /* asks you to type in 2 numbers */   scanf("%lf %lf", a, b); }```
When you've enterd the two numbers I need to eather multiply or divide the two variables "a" & "b"

Thanks for the help!
• 02-26-2013
GReaper
Do you want to give the option to the user to multiply or divide the numbers? If so, there are some ways to do so:
*) Give a list of options and let the user choose the desired one
*) Make it possible to type for example "1.2 / 3.4" ( By reading a number, a non-blank character, and a number again )
• 02-26-2013
Cofex
Quote:

Originally Posted by GReaper
Do you want to give the option to the user to multiply or divide the numbers? If so, there are some ways to do so:
*) Give a list of options and let the user choose the desired one
*) Make it possible to type for example "1.2 / 3.4" ( By reading a number, a non-blank character, and a number again )

Yeah, exaclty! But they won't be able to divide it straight away...
1) They first have to type in the two variables
2) Secound they will choose eather if they want to divide or multiply, like so:
Code:

```void PellerK(double *Produkt, double *Kvot){   printf("Produkt eller Kvot?\n"); /* Here it asks for eather to Multiply or Divide */   scanf("%lf %lf",Produkt,Kvot); if (PellerK == Produkt) {  /* Multiply */ printf("Produkten blir = %d\n"); } else {    /* Divide */ printf("Kvoten blir = %d\n"); }```
Here I am confused and don't know what to do, I am in need of how to do... I want to be able to multiply when I type in "Produkt" and when I type in "Kvot" or anything but "Produkt" to be able to divide...
• 02-27-2013
AndiPersti
Quote:

Originally Posted by Cofex
Yeah, exaclty! But they won't be able to divide it straight away...
1) They first have to type in the two variables
2) Secound they will choose eather if they want to divide or multiply, like so:

Try to convert this pseudocode into a program. The code below doesn't do anything similar to that.

Code:

`void PellerK(double *Produkt, double *Kvot){`
I guess your two parameters should be the numbers you want to multiply/divide. Then the variable names are misleading and the parameters should be called like "number1" and "number2". And I don't think you need them to be pointers but return the actual result from the calculation.

Code:

```  printf("Produkt eller Kvot?\n"); /* Here it asks for eather to Multiply or Divide */   scanf("%lf %lf",Produkt,Kvot);```
You ask the user whether s/he wants to multiply or divide and expect two doubles as input? If I see a question like that I would input "*" or "/" or perhaps "multiply" or "divide" but I would never try to input the actual numbers for the calculation.

Code:

```if (PellerK == Produkt) {  /* Multiply */ printf("Produkten blir = %d\n");```
"PellerK" is a function and in this expression it's a pointer to the function. So that's completely wrong.
And if you use "%d" you also need to provide the value to print.

You need to think about the whole process step by step and write each step down. Then you need to translate this pseudocode into valid C code.
If you don't have a clear plan for your program you won't be able to finish it.

Code:

```1) Ask user for first number 2) Read input from user and store value in variable "number1" 3) Ask user for second number ...```
and only after you have written down every single step you turn on the computer and write the C code for it.

Bye, Andreas
• 02-27-2013
Cofex
Wow, thanks Andreas for the great respond!

Quote:

Originally Posted by AndiPersti
Try to convert this pseudocode into a program. The code below doesn't do anything similar to that.

Code:

`void PellerK(double *Produkt, double *Kvot){`
I guess your two parameters should be the numbers you want to multiply/divide. Then the variable names are misleading and the parameters should be called like "number1" and "number2". And I don't think you need them to be pointers but return the actual result from the calculation.

Produkt means multiply & Kvot means divide. I have allready passed the part when I am going to enter the two numbers.
Code:

```void inmatning(double *, double *); int main(int argc, char *argv[]) {   double nummer1 = 1, nummer2 = 2;   inmatning(&nummer1,&nummer2);   printf("%f %f\n",nummer1,nummer2); void inmatning(double *nummer1, double *nummer2) {   printf("Mata in tv\217 stycken flyttal: ");   scanf("%lf %lf", nummer1, nummer2); }```
Do you got Skype by any chance ? - I would be really greatfull if I could talk & ask questions to a good C Programmer...
• 02-27-2013
AndiPersti
Quote:

Originally Posted by Cofex
Produkt means multiply & Kvot means divide. I have allready passed the part when I am going to enter the two numbers.

Ok, but that doesn't change the fact that you shouldn't name the parameters like that. Think about what you want to pass to the function: two numbers, right?

Do you have already a plan how you want to continue, i.e. the pseudocode you want to implement? Don't write any line of real code as long as you don't have a plan.

Quote:

Do you got Skype by any chance ? - I would be really greatfull if I could talk & ask questions to a good C Programmer...
No, I don't have Skype. Ask here and you will probably get an answer (either from me or someone else).

Bye, Andreas