Computational Science I - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Computational Science I

Description:

We are able to perform integer based arithmetic with the int variable type: ... The compiler complains that it has no idea what the cos function is or what the ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 25
Provided by: timwar1
Category:

less

Transcript and Presenter's Notes

Title: Computational Science I


1
Computational Science I
  • Lecture 5
  • CAAM 420, Fall 2004
  • Instructor Tim Warburton

2
Recall Integer Arithmetic
  • We are able to perform integer based arithmetic
    with the int variable type

3
Running The Arithmetic
  • As before, we create a program, compile it, and
    run it

4
float
  • We can also create floating point variables which
    have approximately eight significant figures.
  • We create these

5
float cont
  • By asking for at least 15 decimal places to be
    printed out we see that in fact those 19 decimal
    places
  • were truncated at around the 8th or 9th decimal
    place (the rest is garbage)

6
float arithmetic
  • Again we can perform arithmetic with floats and
    can even use ints too, although the results may
    take a little effort to predict.

7
float cont
  • Running
  • Explanations ?

8
double
  • When we need more precise answers (i.e. more than
    8 decimal places) we can use double type
    variables which can store up to about 16 decimal
    places.

9
double cont
  • When we run this we see
  • i.e. we see some differences because dadb is not
    the same as fafb . This is because da!fa and
    db!fb
  • ! from now on will stand for not equal

10
Code Control In C
  • Suppose I wish to sum up the numbers between 1
    and 10.
  • I could write code like

11
mysum
  • So I run the code and it works

12
Class Exercise F
  • Write a piece of code which sums up the numbers
    between 1 and a million without using the well
    known formula.

13
for loop
  • C has a built in loop syntax.
  • We can rewrite the mysum program as

14
for loop
  • The syntax of the for loop is
  • for( (1) (2) (3) ) body
  • In words
  • initialize some variables (usually counters) to
    be used in the loop. This is done before the loop
    is entered.
  • Completion test to see if the body of the for
    loop should be entered. If the test is true then
    the body of the loop is executed otherwise the
    for for loop is finished.
  • the increment part of the for statement is
    executed after the body of the loop is executed
  • we go back to (2)

15
Step by Step
  • We can go through this for loop step by step

16
More Advanced Loop Tests
  • The loop completion test can be quite complicated
  • Here I compare iloop with iNterms so that I can
    add all the numbers between 1 and iNterms where
    iNterms is defined before the loop.

17
Class Exercise G
  • Write a routine to add all the odd numbers
    between 1 and 100
  • Change the condition test to use a variable
    instead of 100.
  • Compare your result with a math formula for this
    task (design it yourself).

18
while loops
  • We can also construct less rigid loops.
  • A looser stucture is
  • while(completion test is true)
  • body

19
The Sum Example Using while
  • We initialize the iloop variable outside the
    while loop and increment it inside the while loop
    to achieve the same effect as the for loop.

20
Using break
  • We sometimes would like to break out of a for or
    while loop before the conditional test is made.

21
Some Notes on mybreak.c
  • Here I used a function call to cos() and referred
    to M_PI
  • Believe it or not this is not part of the default
    C language.
  • So at the top of the file I included math.h AND
    when I compiled I linked in libm.a because this
    includes the cos function

22
What happens if we dont take care of cos()
  • Suppose I do not include math.h
  • The compiler complains that it has no idea what
    the cos function is or what the variable M_PI is
    and stops without completing the compile.

23
cont
  • Suppose I forget to link in (i.e. include lm on
    the compile line) but do remember to include
    math.h
  • The compiler tells us it cant find the cos
    function definition.
  • Thats why we have to tell the compiler to
    include libm.a by adding -lm

24
For Next Time
  • Read Chapter 3 of K R to reinforce todays
    class.
  • Next time we will discuss memory pointers, memory
    allocation and structures.
Write a Comment
User Comments (0)
About PowerShow.com