C Math Problem
Results 1 to 10 of 10

Thread: C Math Problem

  1. #1
    Banned
    Join Date
    Feb 2003
    Posts
    35

    Question C Math Problem

    Hello guys,
    I need some help on solving this math problem in c.

    result=3+-(4)/2

    result variable = 3 plus minus (4) divided by 2.
    thanks,


    PUNJABIAN263

  2. #2
    Senior Member
    Join Date
    Feb 2003
    Location
    Memphis, TN
    Posts
    3,747
    Ok, We have x=3+-4/2

    Since you have to follow the order of operations, you have to divide the -4 by the positive 2. so x=3+-2

    Now we have to add. We have a positive three and a negative 2 so two taken from a positive 3 leaves you with a positive 1 so the answer is x=1

    x=3+-4/2
    x=3+-2
    x=1
    =

  3. #3
    Senior Member
    Join Date
    May 2002
    Posts
    344
    err... well this is what it would look like in C i guess:

    Code:
    #include <iostream.h>
    
    int main (void)
    {
        int result;
        result=0;
        
        result=3+(-4)/2;
    }
    watch out for orders of operation and then output the answer!

    EDIT: damn someone got there before me...cheyenne i think he wanted us to write a C program for him that did that...umm how do you output something again in C? damn i hate programming...
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  4. #4
    Senior Member
    Join Date
    Jun 2003
    Posts
    236
    printf("%d",result);

    this should work, in the shortest code possible

    #include<stdio.h>
    void main()
    {
    printf("%d",(3+(-4)/2));
    }

    for learing sake this is more 'programitically correct'

    #include<stdio.h>
    int main(int argc,char ** argv)
    {

    printf("%d",(3+(-4)/2));
    }

    oops..hit worng key before i was finished

    #include<stdio.h>
    int main(int argc,char ** argv)
    {
    int result;
    result = 3+(-4)/2;
    printf("%d\n",result);
    return 0;
    }
    That which does not kill me makes me stronger -- Friedrich Nietzche

  5. #5
    Senior Member
    Join Date
    May 2003
    Posts
    472
    well solutions are down there...but punjabian263 what are u actually trying to achieve...and are u facing any errors in it??
    guru@linux:~> who I grep -i blonde I talk; cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;

  6. #6
    Senior Member
    Join Date
    Jul 2003
    Posts
    634
    Yea remember Bodmas when doing maths. Both on computer and by hand

    B do anything in brackets first
    o powers of
    d division
    m Mulitplication
    a addition
    s subtraction

    if you run though that list when you solve a problem its makes it work,

    i2c

  7. #7
    Junior Member
    Join Date
    Sep 2002
    Posts
    14
    Actually, it's BEDMAS.

    Brackets
    Exponents
    Division
    Multiplication
    Addition
    Subtraction

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    344
    well actually, you are all wrong, it is PEMDAS:

    P-Parenthesis
    E-Exponents
    M-Multiplication
    D-Division
    A-Addition
    S-Subtraction

    This just proves that there are multipule different ways how to remember the order of operations...anyways just thought i might share how my seventh grade teacher thought them to us
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  9. #9
    Senior Member
    Join Date
    Jul 2003
    Posts
    634
    heheh there must be load of different ways to do it,

    Last year i did first year degree level maths and it worked for me, and its worked for me for the past 8 or 9 yrs

    looks like theres a british, american and canadian way!

    i2c

  10. #10
    Banned
    Join Date
    Feb 2003
    Posts
    35

    Thanks to all

    Hello all guys,
    Thanks for helping me.
    my problem is solved. i have got the right answer.

    THANKS,

    punjabian263

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •