Previously we looked at some string handling functions. Now we look at arithmetic with numbers of type double.

Similar documents
Biggar High School Mathematics Department. National 5 Learning Intentions & Success Criteria: Assessing My Progress

Pythagoras Theorem. Page I can identify and label right-angled triangles explain Pythagoras Theorem calculate the hypotenuse

Trigonometric Functions and Triangles

WORK SCHEDULE: MATHEMATICS 2007

ALGEBRA 2/TRIGONOMETRY

Find the length of the arc on a circle of radius r intercepted by a central angle θ. Round to two decimal places.

Chapter 8 Geometry We will discuss following concepts in this chapter.

6.1 Basic Right Triangle Trigonometry

Trigonometry Hard Problems

COMPETENCY TEST SAMPLE TEST. A scientific, non-graphing calculator is required for this test. C = pd or. A = pr 2. A = 1 2 bh

MCA Formula Review Packet

SOLVING TRIGONOMETRIC EQUATIONS

SAT Subject Math Level 2 Facts & Formulas

TRIGONOMETRY Compound & Double angle formulae

Trigonometry LESSON ONE - Degrees and Radians Lesson Notes

Postulate 17 The area of a square is the square of the length of a. Postulate 18 If two figures are congruent, then they have the same.

Revision Notes Adult Numeracy Level 2

Parallel and Perpendicular. We show a small box in one of the angles to show that the lines are perpendicular.

Solutions to Exercises, Section 5.1

D.3. Angles and Degree Measure. Review of Trigonometric Functions

General Physics 1. Class Goals

Extra Credit Assignment Lesson plan. The following assignment is optional and can be completed to receive up to 5 points on a previously taken exam.

MATHS LEVEL DESCRIPTORS

Give an expression that generates all angles coterminal with the given angle. Let n represent any integer. 9) 179

How do you compare numbers? On a number line, larger numbers are to the right and smaller numbers are to the left.

ModuMath Basic Math Basic Math Naming Whole Numbers Basic Math The Number Line Basic Math Addition of Whole Numbers, Part I

Section 7.1 Solving Right Triangles

National 5 Mathematics Course Assessment Specification (C747 75)

Geometry Notes PERIMETER AND AREA

Trigonometry Review Workshop 1

Mathematics Placement Examination (MPE)

Pythagorean Theorem: 9. x 2 2

Additional Topics in Math

Higher Education Math Placement

The Deadly Sins of Algebra

Tennessee Mathematics Standards Implementation. Grade Six Mathematics. Standard 1 Mathematical Processes

Geometry Notes RIGHT TRIANGLE TRIGONOMETRY

Core Maths C2. Revision Notes

Calculating Area, Perimeter and Volume

Introduction and Mathematical Concepts

Functions and their Graphs

Number Sense and Operations

(1.) The air speed of an airplane is 380 km/hr at a bearing of. Find the ground speed of the airplane as well as its

Math 0980 Chapter Objectives. Chapter 1: Introduction to Algebra: The Integers.

CAMI Education linked to CAPS: Mathematics

FOREWORD. Executive Secretary

WEDNESDAY, 4 MAY AM AM. Date of birth Day Month Year Scottish candidate number

WEDNESDAY, 2 MAY AM AM. Date of birth Day Month Year Scottish candidate number

2312 test 2 Fall 2010 Form B

Mathematics programmes of study: key stage 4. National curriculum in England

With the Tan function, you can calculate the angle of a triangle with one corner of 90 degrees, when the smallest sides of the triangle are given:

Florida Math Correlation of the ALEKS course Florida Math 0018 to the Florida Mathematics Competencies - Lower

4.3 & 4.8 Right Triangle Trigonometry. Anatomy of Right Triangles

The GED math test gives you a page of math formulas that

Trigonometry Review with the Unit Circle: All the trig. you ll ever need to know in Calculus

Graphing Trigonometric Skills

Week 13 Trigonometric Form of Complex Numbers

2. Complete the table to identify the effect tripling the radius of a cylinder s base has on its volume. Cylinder Height (cm) h

Algebra and Geometry Review (61 topics, no due date)

MEMORANDUM. All students taking the CLC Math Placement Exam PLACEMENT INTO CALCULUS AND ANALYTIC GEOMETRY I, MTH 145:

Trigonometric Functions: The Unit Circle

of surface, , , of triangle, 548 Associative Property of addition, 12, 331 of multiplication, 18, 433

Expression. Variable Equation Polynomial Monomial Add. Area. Volume Surface Space Length Width. Probability. Chance Random Likely Possibility Odds

13. Write the decimal approximation of 9,000,001 9,000,000, rounded to three significant

Key Topics What will ALL students learn? What will the most able students learn?

Mathematics (Project Maths Phase 1)

1 TRIGONOMETRY. 1.0 Introduction. 1.1 Sum and product formulae. Objectives

Algebra 1 End-of-Course Assessment Sample Questions Regular Print Paper-Based Accommodation

CSU Fresno Problem Solving Session. Geometry, 17 March 2012

5.3 SOLVING TRIGONOMETRIC EQUATIONS. Copyright Cengage Learning. All rights reserved.

MATHCOUNTS TOOLBOX Facts, Formulas and Tricks

Circumference of a Circle

Math Placement Test Study Guide. 2. The test consists entirely of multiple choice questions, each with five choices.

C Programming Language

Evaluating trigonometric functions

Section 1: How will you be tested? This section will give you information about the different types of examination papers that are available.

Mathematics. GCSE subject content and assessment objectives

Page. Trigonometry Sine Law and Cosine Law. push

NIFA NATIONAL SAFECON

Perimeter, Area, and Volume

One advantage of this algebraic approach is that we can write down

Chapter 5: Trigonometric Functions of Angles

x(x + 5) x 2 25 (x + 5)(x 5) = x 6(x 4) x ( x 4) + 3

Measurement Length, Area and Volume

43 Perimeter and Area

ANALYTICAL METHODS FOR ENGINEERS

ab = c a If the coefficients a,b and c are real then either α and β are real or α and β are complex conjugates

how to use dual base log log slide rules

Angles and Quadrants. Angle Relationships and Degree Measurement. Chapter 7: Trigonometry

Math Placement Test Practice Problems

Friday, January 29, :15 a.m. to 12:15 p.m., only

Area of a triangle: The area of a triangle can be found with the following formula: in

Unit 1 - Radian and Degree Measure Classwork

TUESDAY, 6 MAY 9.00 AM 9.45 AM. 2 Full credit will be given only where the solution contains appropriate working.

Algebra Academic Content Standards Grade Eight and Grade Nine Ohio. Grade Eight. Number, Number Sense and Operations Standard

Year 9 set 1 Mathematics notes, to accompany the 9H book.

2nd Semester Geometry Final Exam Review

The Primary Trigonometric Ratios Word Problems

Transcription:

1 Programming with C Terry Marris November 2010 4 The Double Number Type Previously we looked at some string handling functions. Now we look at arithmetic with numbers of type double. 4.1 Arithmetic Operators and Precedence Numbers of type double are the familiar numbers with a decimal point. We can perform the usual arithmetic operations: add +, subtract -, multiply * and divide / on them. The familiar rules of precedence apply: brackets first, then multiplication and division, then addition and subtraction. For example, in working out the Celsius equivalent of a given temperature in degrees Fahrenheit celcius = 5 * (Fahrenheit - 32) / 9 the brackets are worked out first, then the multiplication, then the division. 4.2 Constants The essential property of a constant is that its value remains unchanged during program execution. const is an example of a type qualifier. It specifies that the object it refers to cannot be modified during a program run. const is used to define π in the program shown below. The program calculates the area and circumference of a circle from a given radius. /* circle.c: circumference and area of a circle */ double radius, circumference, area; printf("radius of circle? "); radius = atof(string); circumference = 2 * pi * radius; area = pi * radius * radius; printf("circumference is %0.1f, area is %0.1f\n", circumference, area);

2 Since radius, circumference and area are all variables of type double, we can declare them all on the same line. double radius, circumference, area; Advantages of using constants include: maintenance: if the value of a constant is to be changed by a programmer, the change is made in one just one place in the program. If a constant was not used you would have to search through the entire program looking for every occurrence of the value to be changed. readability: a descriptive name e.g. pi, is easier to understand than some arbitrary value e.g. 3.14159265. 4.3 Power and Square Root The next program uses the familiar theorem of Pythagoras to calculate the length of the third side of a right-angled triangle. a h h 2 = a 2 + b 2 b h = a 2 + b 2 The C library math.h contains the functions pow() and sqrt() that raises a number to a given power, and finds the square root of a number respectively. pow(a, 2) will raise a to the power of 2. sqrt(x) will give the square root of X. Of course, X should not be negative. sqrt() and pow() are used in the program shown below.

3 /* pythagoras.c: finds the hypotenuse of a right-angled triangle */ #include <math.h> double a, b, h; printf("length of side A? "); a = atof(string); printf("length of side B? "); b = atof(string); h = sqrt(pow(a, 2) + pow(b, 2)); printf("hypotenuse is %0.1f\n", h); 4.4 The Trigonometric Functions The trigonometric functions, defined in math.h, include tan(), sin() and cos(). These trig functions require angles to be provided in radians. This is no problem provided we remember that and radians = degrees * π 180 degrees = radians * 180 π We use the sin() function in the next program. The area of a triangle is a = ½ab sin(θ) where θ is the angle included by sides a and b.

4 /* areatriangle.c: calculates the area of a triangle */ #include <math.h> double a, b, degrees, radians, area; printf("length of side A? "); a = atof(string); printf("length of side B? "); b = atof(string); printf("included angle in degrees? "); degrees = atof(string); radians = degrees * pi / 180; area = 0.5 * a * b * sin(radians); printf("area is %0.2f\n", area); Since the included angle is 90 we have a right-ang led triangle. The area of a triangle is ½ base x perpendicular height = ½ x 3 x 4 = 6 which is the same as previously calculated using the ½ a b sin(θ) formula. It is often useful to check the result of a calculation by using a different, independent method.

5 4.5 The Inverse Trigonometric Functions The inverse trig functions include acos(), asin() and atan(). You would use the inverse trig functions to determine the angle involved. For example... A cyclist comes across a near vertical hill with a gradient of 1 in 4. This means that for every four metres travelled, the road rises (or falls) one metre. 4 1 α sin α = 1 4 sin α = 0.25 α 14 (from tables) We take a look at the program. /* gradtodeg.c: converts a gradient to its equivalent in degrees */ #include <math.h> double h, d, ratio, radians, degrees; printf("height risen? "); h = atof(string); printf("distance travelled? "); d = atof(string); ratio = h / d; /* FAILS if d = 0 */ radians = asin(ratio); degrees = radians * 180 / pi; printf("the elevation in degrees is %0.1f\n", degrees);

6 The ratio is defined as height risen divided by distance travelled. If the distance travelled is zero then the ratio is undefined since you cannot divide by zero. We see how to deal with this kind of problem in a later chapter. There are some restrictions on the ratios. If you are using asin(ratio) or acos(ratio), the ratio must be between 0.0 and 1.0 inclusive. If you are using atan(ratio), the ratio must be between 0.0 and about 573.0. Exercise 4.1 1. A bicycle's gear ratio is traditionally calculated from gearratio = wheeldiameter x chainwheel rearsprocket A ratio of 100 would help you speed down hills. A ratio of 30 will help you toil up them. Typically, a wheel size is 27 inches, the number of teeth on a chainwheel (which is attached to the cranks, which are themselves attached to the pedals) is between 52 and 32, and the number of teeth on a rear sprocket between 13 and 28. Write and test a program that will input wheel size, and the number of teeth on both a chainwheel and a sprocket, and which outputs the gear ratio correct to one decimal place. 2. Write and test a program that converts kilometres to miles. You might like to use the fact that 1 km = 0.621 miles. 3. Hero's formula for finding the area of a triangle is A = s(s - a)(s - b)(s - c) where s = a + b + c 2 Write and test a program that inputs the lengths of three sides of a triangle and outputs its area. 4. The volume of a sphere is v = 4πr 3 where r is its radius. Write and test a program 3 that inputs the radius of a sphere and outputs its volume. 5. Surveying. It is useful to know the height of tall buildings since they are a hazard to low flying aircraft. At 500m from the base, the angle of elevation to the top of a building is 15. What is the height of the buildin g? h 15 500 m

7 6. Navigation. An airplane pilot. currently flying at point A, is seeking to reach point B, which is 40 miles east, and 30 miles north from A. What bearing should the pilot be flying on? 40 B θ 30 A We have seen how to perform arithmetic with numbers of type double, and how to use some of the functions provided in math.h. Next we see examine the numeric data type integer. Bibliography Kernighan B and Ritchie D The C Programming Language Prentice Hall 1988 Mark Williams Company ANSI C A Lexical Guide Prentice Hall 1988 Godfrey C and Siddons A Four-Figure Tables Cambridge University Press 1962