• Arithmetic Operators

    What are arithmetic operators?

    Why are arithmetic operators important?

    • you can now manipulate and process data
    • instead of reading it in and redisplaying it

    How do are arithmetic operators important used?

    • place them between 2 values, like an algebraic expression
    • 1 + 1
  • Adder

    adder.c:

    Things to note:

    • printf can take a variable sum or an expression first_number + second_number
  • Precedence

    What is precedence?

    Why is precedence important?

    • it will reduce the chances of miscommunication between you and the computer
    • it's best to make your code clear, but when things get ambiguous, the order of precedence are the rules that the computer will follow, hence someone reading your code can still "figure out" your code

    An example of precendence

    precedence.c:

    output of precedence.c:

    1 + 2 * 2 = 5 
    (1 + 2) * 2 = 6 
    1 + (2 * 2) = 5

    Things to note:

    • (expr)has higher precedence than other operators (+-*/%)
    • having (expr) makes the intent of the equation easier to understand
  • Associativity

    What is associativity?

    Why is associativity important?

    • it will reduce the chances of miscommunication between you and the computer
    • it's best to make your code clear, but when things get ambiguous, the associativity are the rules that the computer will follow, hence someone reading your code can still "figure out" your code

    An example of associativity

    associativity.c:

    output of associativity.c:

    4 - 3 - 2 = -1
    (4 - 3) - 2 = -1
    4 - (3 - 2) = 3

    Things to note:

    • (expr)has higher precedence than other operators (+-*/)
    • having (expr) makes the intent of the equation easier to understand
  • Dividing

    An example of dividing

    divide.c:

    Things to note:

    • avoid dividing by 0
    • be aware of the data types
  • Write out the modified quadratic equation in code

    write a program that takes in 3 numbers ($a$ & $b$ & $c$) and calculates $z={-b+(b^2-4ac)^1}/{2a}$

      Test inputs and outputs:
    • a = 1; b = 2; c= 3; z = -5;
    • a = 2; b = 6; c= 3; z = 1.5;
  • Assignment

    Homework Problem 11.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_11_1.c
      • example:
      • zhaol_11_1.c

    Write a program that takes in 2 numbers ($x$ & $y$) and calculates $z=-x+x^2-{(4y*2y)^2}$

    • Test Case 1:
      Enter x: [user enters 2]
      Enter y: [user enters 2]
      −x+x^2−(4y*2y)^2 = -1022.000000
      
    • Test Case 2:
      Enter x: [user enters 4]
      Enter y: [user enters 6]
      −x+x^2−(4y*2y)^2 = -82932.000000
      

    Homework Problem 11.2, at the end of this problem you should submit to Laulima:

    • a file named UHusername_11_2.c
      • example:
      • zhaol_11_2.c

    Write a program that takes in a dividend and a divisor and returns their quotient and remainder:

    • Test Case 1:
      Please enter the dividend: 
      [user enters 9]
      Please enter the divisor: 
      [user enters 4]
      The quotient is: 2
      The remainder is: 1 
      
    • Test Case 2:
      Please enter the dividend: 
      [user enters 20]
      Please enter the divisor: 
      [user enters 6]
      The quotient is: 3
      The remainder is: 2 
      
      Notes:
    • do not worry about decimal numbers for this assignment
    • if you get stuck, you can read up on the mod operator, %