ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.

Slides:



Advertisements
Similar presentations
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Types and Arithmetic Operators
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Data Types, Expressions and Functions (part I)
CC112 Structured Programming Lecture 4 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Numeric Types, Expressions, and Output ROBERT REAVES.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CSC Programming I Lecture 5 August 30, 2002.
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
Doing math In java.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
$100 $200 $300 $400 $100 $200 $300 $400 $300 $200 $100 Writing variable equations Find variables in addition equations Find variables in subtraction.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Arithmetic Expressions
Variables, Operators, and Expressions
Arithmetic Expressions
CSE 220 – C Programming Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Completing the Problem-Solving Process
INSPIRING CREATIVE AND INNOVATIVE MINDS
BIL 104E Introduction to Scientific and Engineering Computing
TMF1414 Introduction to Programming
Relational Operations
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Arithmetic Operators in C
Arithmetic Expressions in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operators in C
C Operators, Operands, Expressions & Statements
Two Step Equation.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Elementary Programming (C++)
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Two step equation Brackets
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

ICS 103 Lab 2-Arithmetic Expressions

Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic expressions Learn how to use arithmetic expressions Learn how to use math functions Learn how to use math functions

Arithmetic operators We need arithmetic operators to solve most programming problems, you will need to write arithmetic expressions that manipulate data types. basic arithmetic operators: (Binary) (Unary) (Binary) (Unary) addition (+) plus(+) subtraction (-) negation(-) multiplication (*) division (/) remainder (%)

Each operator has 2 operands. Each operator has 2 operands. Operands may be constants, variables, or other arithmetic expressions. Operands may be constants, variables, or other arithmetic expressions. If the operands are of different types, the one with weaker type will be raised to the level of the other type, then the operation will be performed. The order is char, int, long, float, double. If the operands are of different types, the one with weaker type will be raised to the level of the other type, then the operation will be performed. The order is char, int, long, float, double. Binary operators

Division The division between two integer numbers results in an integer result (as you can see 5/2 gives 2 not 2.5). The division between two integer numbers results in an integer result (as you can see 5/2 gives 2 not 2.5). The division by zero is undefined. Either you get a compilation warning or a run time error. The division by zero is undefined. Either you get a compilation warning or a run time error.

Remainder The remainder operator % is related to the division one. This operator will give the remainder produced by dividing the two numbers ( 5 % 2 = 1). Therefore, this operator is not used with double values. If one of the operand or both are double, you will get an error message from the compiler saying: The remainder operator % is related to the division one. This operator will give the remainder produced by dividing the two numbers ( 5 % 2 = 1). Therefore, this operator is not used with double values. If one of the operand or both are double, you will get an error message from the compiler saying: Illegal use of floating point

Arithmetic expressions contain a combination of the arithmetic operations including brackets: x = z – (a + b / 2) + w * -y To evaluate the expression, we need to follow the precedence rule which is as follows: 1. ( )expression within parentheses 2. +, -unary operators (plus and minus sign) 3. *, /, %multiplication, division and remainder are evaluated from left to right 4. +, -addition and subtraction are evaluated from left to right Arithmetic expressions

Mathematical Functions To do some advanced mathematical functions in C, there is a header file called that has different trigonometric and algebraic functions. Here are some frequently used functions: To do some advanced mathematical functions in C, there is a header file called that has different trigonometric and algebraic functions. Here are some frequently used functions: pow(x,y) x y pow(5,3) = 5 3 = 125 sqrt(x)(x > 0)sqrt(4) = = 2 log(x)ln x (x > 0)log(5) = log10(x) (x > 0) log10(5) = exp(x) e x exp(2) = sin(x)sin x(x in radian)sin(90) = cos(x)cos x (x in radian)cos(90) = tan(x)tan x (x in radian)tan(90) = asin(x)sin-1 x ( x in [-1,1])asin (0) = 0 acos(x)cos-1 x (x in [-1,1])acos(0) = atan(x)tan-1 x atan(0) = 0 Note that you have to include the header file math.h before you use any of these functions. Also, the return type of these functions is double. Note that you have to include the header file math.h before you use any of these functions. Also, the return type of these functions is double.

Example Write a program that finds the area of a triangle given the length of its sides: a, b, c. Write a program that finds the area of a triangle given the length of its sides: a, b, c. TEST CASES: a = 2, b = 3, c = 4 TEST CASES: a = 2, b = 3, c = 4 AREA= 2.90 AREA= 2.90

Lab Work

Write a program that reads the lengths of two sides of a triangle and the angle between them and calculates the length of the third side using the following formula: Write a program that reads the lengths of two sides of a triangle and the angle between them and calculates the length of the third side using the following formula: Write a program that reads the volume of a sphere and finds the radius of it. Write a program that reads the volume of a sphere and finds the radius of it. Write a program to solve the quadratic equation using quadratic formulas: Write a program to solve the quadratic equation using quadratic formulas: Your Program should prompt the user for the values of a, b and c.

Test cases Program 1: Program 1: Input\ Side1 = 10, Side2 = 5, a = 45 Output\ Side3 = 8.51 Program 2: Program 2: Input\ V= 100 Output\ r = 2.87 Program 3: Program 3: Input\ a = 5, b = 10, c = 3 Output\ x1 = -0.36, x2 = -1.63