Thread: How do I divide / multiply two variables?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    Question 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!
    Last edited by Cofex; 02-26-2013 at 02:19 PM. Reason: Editing text for more info

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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 )
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by GReaper View Post
    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...

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Cofex View Post
    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.

    For example you could start with a list like
    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

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Wow, thanks Andreas for the great respond!

    Quote Originally Posted by AndiPersti View Post
    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...

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Cofex View Post
    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.

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Multiply and Divide operators
    By Conti in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2012, 04:44 AM
  2. Replies: 15
    Last Post: 01-24-2008, 09:40 PM
  3. Addition using only multiply and divide
    By abachler in forum Contests Board
    Replies: 26
    Last Post: 09-19-2007, 12:40 AM
  4. multiply by 7
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-22-2006, 02:19 PM
  5. How to multiply and Divide Huge int numbers
    By Dewayne in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 08:41 PM

Tags for this Thread