Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Seite - 47 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 47 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Bild der Seite - 47 -

Bild der Seite - 47 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 47 -

2.7 Exercises 47 Exercise2.7:While loopwitherrors Assume some program has been written for the task of adding all integers i D 1;2;:: :;10: some_number = 0 i = 1 while i < 11 some_number += 1 print some_number a) Identify the errors in the programby just reading the code and simulating the programbyhand. b) Writeanewversionof theprogramwitherrorscorrected.Runthisprogramand confirmthat it gives thecorrectoutput. Filename:while_loop_errors.py. Exercise2.8:Areaofrectangleversuscircle Consider one circle and one rectangle. The circle has a radius r D 10:6. The rectangle has sidesa andb, but onlya is known fromtheoutset. LetaD 1:3 and write a program that uses a while loop to find the largest possible integer b that givesarectangleareasmaller than,butascloseaspossible to, theareaof thecircle. Run theprogramandconfirmthat it gives the right answer (which isbD271). Filename:area_rectangle_vs_circle.py. Exercise2.9: Findcrossingpointsof twographs Consider two functionsf.x/Dx andg.x/Dx2on the interval Œ 4;4 . Write a program that, by trial and error, finds approximately forwhich values ofx the two graphs cross, i.e., f.x/ D g.x/. Do this by consideringN equally distributedpointsontheinterval,ateachpointcheckingwhether jf.x/ g.x/j< , where is some small number. LetN and be user input to the programand let the result be printed to screen. Run your programwithN D 400 and D 0:01. Explain the output from the program. Finally, try also other values ofN , keeping thevalueof fixed. Explainyourobservations. Filename:crossing_2_graphs.py. Exercise2.10: Sortarraywithnumbers The import statement from random import * will give access to a function uniform that may be used to draw (pseudo-)random numbers from a uniform distribution between two numbers a (inclusive) and b (inclusive). For example, writingx = uniform(0,10)makesx afloatvalue larger than, or equal to, 0, and smaller than,orequal to, 10. Write a script that generates an array of 6 randomnumbers between 0 and 10. Theprogramshould thensort thearray so thatnumbersappear in increasingorder. Let the programmake a formatted print of the array to the screen bothbefore and after sorting. Theprintoutsshouldappearon thescreenso thatcomparison ismade easy. Confirmthat thearrayhasbeensortedcorrectly. Filename:sort_numbers.py.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2016
Sprache
englisch
Lizenz
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
248
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python