Title: Mathematical Operations
1Mathematical Operations
2Average
- Accept three user input values, prompting the
user to enter each individual value, compute the
average value of the three input values and
display. - Pseudo code
- Prompt user to enter value 1
- Read in value 1
- Prompt user to enter value 2
- Read in value 2
- Prompt user to enter value 3
- Read in value 3
- Compute the average value of the entered data
- Display the average value
3Average
alternative code (can group on one line) double
value1,value2, value3, averageValue
Coding
- / header /
- include ltiostream.hgt
- void main(void)
-
- double value1
- double value2
- double value3
- double averageValue
- cout ltlt "Input value 1 "
- cin gtgt value1
- cout ltlt endl ltlt "Input value 2 "
- cin gtgt value2
- cout ltlt endl ltlt "Input value 3 "
- cin gtgt value3
strings are enclosed in double quotes
user input
end of line (carriage return)
end of line (carriage return)
4Average
Program output
- Input value 1 -1
- Input value 2 32
- Input value 3 68.3
- The average value is 33.1
5Assignment Statements
Stepping through the code
- / header /
- include ltiostream.hgt
- void main(void)
-
- double value1
- double value2
- double value3
- double averageValue
- cout ltlt "Input value 1 "
- cin gtgt value1
- cout ltlt endl ltlt "Input value 2 "
- cin gtgt value2
- cout ltlt endl ltlt "Input value 3 "
- cin gtgt value3
value1 ? value2 ? value3
? averageValue ?
value1 -1 value2 ? value3
? averageValue ?
value1 -1 value2 32 value3
? averageValue ?
6Assignment Statements
Stepping through the code
- / header /
- include ltiostream.hgt
- void main(void)
-
- double value1
- double value2
- double value3
- double averageValue
- cout ltlt "Input value 1 "
- cin gtgt value1
- cout ltlt endl ltlt "Input value 2 "
- cin gtgt value2
- cout ltlt endl ltlt "Input value 3 "
- cin gtgt value3
value1 -1 value2 32 value3
68.3 averageValue ?
value1 -1 value2 32 value3
68.3 averageValue 33.1
7cin
- Two code fragments that have the same effect
double a, b, c cout ltlt Enter the sides of a
triangle cin gtgt a cin gtgt b cin gtgt c
double a, b, c cout ltlt Enter the sides of a
triangle cin gtgt a gtgt b gtgt c
Three ways to enter data values with the same
effect
Enter the sides of a triangle 17.3 12.9 9.1
Each of these three approaches is a stream of
input data containing the same three numbers
separated by blanks, tabs, and/or newline
characters (all of which are ignored when
extracting the numerical values from the input
stream
Enter the sides of a triangle 17.3 12.9 9.1
Enter the sides of a triangle 17.3 12.9 9.1
8Operators and Expressions
9Arithmetic Expressions
- Operators
- Addition
- Subtraction
- Multiplication
- Division /
- Modulus
- Operands
- Variables
- Constants
- Functions with output arguments (e.g. sin(3),
pow(x,3), sqrt(y))
10Order of Operation
- Parentheses evaluate expressions enclosed by
parentheses first - Precedence evaluate operators according to their
precedence/priority (e.g. multiplication and
division before addition or subtraction) - Associativity if there is a sequence of two or
more operators of the same precedence evaluate - Unary operators from right to left (right
associativity) - Binary operators from left to right (left
associativity) - Examples
- a b c/3
- (a b c)/3
- a/-b (c 2 b)(-n 1)
11Type Conversion
- The output of an arithmetic expression will have
a type equal to the input type. - Floating point example 9.0/2.0 produces
4.5 -11.0/4.0 produces -2.75 - Integer example 9/2 produces 4 -11/4 produces
-2 - 92 produces 1 -114 produces -3
- If there are a mixture of input types operands
are automatically converted/promoted to the
higher type prior to the application of the
operator. - 1.0 1/2 produces 1.0 (not 1.5)
high long double double float unsigned
long int long int unsigned int int low shor
t int
division produces 0, which is then converted to
0.0 prior to the addition
12Explicit Type Conversion (aka Type Casting)
- We can explicitly convert a value from one type
to another. - Syntax
- lttype to cast togt (ltvariable/expressiongt)
- or
- (lttype to cast togt) ltvariable/expressiongt
- Note that type casting simply performs a
conversion prior to the variable/expression being
used. It does not change the content of the
variable. - Example
- 1.0 (double)1/2 produces 1.5
13Explicit Type Conversion (aka Type Casting)
- int totalScore 23
- int numStudents 4
- double average
- average (double) totalScore/(double)
numStudents - or
- average double (totalScore)/double
(numStudents) - or
- average double (totalScore)/numStudents
- All these computations of average are equivalent
and produce a value of 5.75. Note, as shown in
the last line that only a single casting is
required as the second casting would be done
automatically.
14Explicit Type Conversion (aka Type Casting)
- Type casting can also be performed from a higher
data type to a lower data type. For example, we
could type cast a double to an int, which has the
effect of truncation. - double a 17.9
- double b 22.1
- cout ltlt a ltlt " rounds to " ltlt (int) a ltlt " and
ltlt - b ltlt " rounds to " ltlt int (b)
- Produces
- 17.9 rounds to 17 and 22.1 rounds to 22
15Assignment Shortcuts
- Short form Long form equivalent
- counter counter counter 1
- counter counter counter 1
- index-- index index 1
- --index index index 1
- index 2 index index 2
- index / counter index index/counter
- index 5 index index 5 index
- index index 1
- index 5 --index index index - 1
- index 5 index
- --index 5 index index - 1
- index 5 index
- Although these short forms produce more compact
code ensure that the readability of your code
remains intact!
16Boolean (True/False)
- In C false is related to the value zero and
true is related to any other value other than
zero. When a boolean expression is evaluated in
C, a true value will be represented by a value
of one. Borland C provides a boolean variable
type called bool and the constants true and
false. - Note Some compilers, including Borland C
version 4.2 or earlier do not have the bool
variable type, and the constants true and false
do not exist. In these cases you can define them
yourself using the line - typedef enum false 0, true 1 bool
- after your include directives.
17Relational and Logical Operators
- Relational operators Action
- gt Greater than
- gt Greater than or equal to
- lt Less than
- lt Less than or equal to
- Equal to
- ! Not equal to
- Logical operators Action
- AND
- OR
- ! NOT
18Precedence
- Relational and logical operators have lower
precedence than arithmetic operators (e.g. , ,
). - The relative precedence of the relational and
logical operators are - Highest !
- gt gt lt lt
- !
-
- Lowest
19Examples
- Expression Evaluates to
- 9 gt 5 true
- 9 lt 5 false
- 13 4 true
- ((57) gt 0) ((-17) lt 0) true
- 57 gt 0 -17 lt 0 true
- true !0 1 true
- true !(0 1) false
- 9 true true
20Problem 1 Area and volume of a sphere
Write a program that accepts a user input for the
sphere, and computes and displays the surface
area and volume of a sphere. Use float data types
for your variables radius, area, and volume. See
sample output below. What happens when you change
your variables to type int. Note that you will
have to use the directive include ltmath.hgt to
have access if you want to use the function
pow(x,y), You should also define the constant
M_PI, using the code const double M_PI 2.0
asin(1.0) After your include statements (note
Borland defines M_PI in ltmath.hgt so doesnt
require this code)
- Enter the radius of the sphere 15.5
- The area of the sphere is 3019.07 and the volume
is 15598.5
21Problem 2 Long equation
Write a program that accepts three user inputs x,
y, and z. The program should then compute and
display the result for the following
formula See sample outputs below. Use data
type float for your variables x, y, z, and
f. Change your variables x, y, and z to data type
int. Does the value of f change? Use type casting
so that f is evaluated correctly. Try to use as
few type casts as possible (it can be done with
only three type casts for sure)
- Enter the values for x, y, and z 3 5 7
- f(x,y,z) 3.47142
Enter the values for x, y, and z 5 9 3 f(x,y,z)
24.067