Thread: simple arithmetic calculations

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    37

    simple arithmetic calculations

    I just wanted to double check some calculations..
    Code:
    5 + 3	                              8
    5.6 + 8	                             13.6
    5.3 – 8.7	                     -3.4
    10 / 3	                               3
    17 * 2	                               3.4
    10 / 3.0	                       3.0
    10.0 / 3	                       3.0
    10.0 / 3.0	                      3.0
    My answers are on the right...Is this what the program would return?
    Last edited by viciousv322; 11-16-2005 at 06:47 PM. Reason: for clarity

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    No.

    Why don't you create a program to do it, and see for yourself?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    i keep getting different results... would it matter if the variables were defined as int or double, etc? The problem does not specify??

    i got 8, 13, -3, 3 for the first 4...using int... is that correct?

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Post your code. Is the code you are running giving expected results or unexpected results? If unexpected, post what result you found was unexpected, and what code produced it.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i guess u are returing int instead of float/double from the fucntion.

    you function prototype should look soemthing like this. lets say u function ane us calc
    Code:
    double calc(double);
    ssharish2005

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i guess u are returing int instead of float/double from the fucntion.

    you function prototype should look something like this. lets say u function ane us calc
    Code:
    double calc(double);
    ssharish2005

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    #define FOO(expr,guess) "%s = %g (%.1f)\n", #expr, expr, guess
    #define BAR(expr,guess) "%s = %d (%d)\n", #expr, expr, guess
    #define BAZ(expr,guess) "%s = %d (%.1f)\n", #expr, expr, guess
    
    int main(void)
    {
       printf(BAR(5 + 3, 8));
       printf(FOO(5.6 + 8, 13.6));
       printf(FOO(5.3 - 8.7, -3.4));
       printf(BAR(10 / 3, 3));
       printf(BAZ(17 * 2, 3.4));
       printf(FOO(10 / 3.0, 3.0));
       printf(FOO(10.0 / 3, 3.0));
       printf(FOO(10.0 / 3.0, 3.0));
       return 0;
    }
    
    /* my output
    5 + 3 = 8 (8)
    5.6 + 8 = 13.6 (13.6)
    5.3 - 8.7 = -3.4 (-3.4)
    10 / 3 = 3 (3)
    17 * 2 = 34 (3.4)
    10 / 3.0 = 3.33333 (3.0)
    10.0 / 3 = 3.33333 (3.0)
    10.0 / 3.0 = 3.33333 (3.0)
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    10/3 is...? No, it's not 3.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Dweia
    10/3 is...? No, it's not 3.
    It is 3 if you are doing integer math

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM