Download presentation
Presentation is loading. Please wait.
Published byCamron Terry Modified over 9 years ago
1
PHY 107 – Programming For Science
2
The Week’s Goal
3
Announcements Weekly assignment #2 available on to D2L
4
Program Outline Once upon a time… … some stuff happens… … and they all lived happily ever after
5
Program Outline Once upon a time… All programs must begin somewhere Defines what is worked upon during rest of program For non-trivial programs, requires receiving input When starting program, first steps always same: 1. What is the input? 2. What will the input look like? 3. How will the data be entered?
6
Reading From The Keyboard Easiest to get input from the keyboard Reading from files possible; discussed later in term C lacks standard, so writing GUI program harder C defines scanf to get user’s input As easy to use as delivering food to Granny’s house When scanf hit, program waits until it has input User must press enter for line to be able to be read Editing not seen by program; only receives final line
7
Even Better Magic Must tell scanf types it will be reading read Variable TypeSpecifier int%i, %d long int%li, %ld unsigned int%u unsigned long%lu float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG char%c
8
Programming Using scanf
9
Warning #1 will not Incorrect scanf specifier will not trigger an error Specifier, not variable, determines data read
10
Warning #2 will not Incorrect scanf specifier will not trigger an error Specifier, not variable, determines data read Illegal assignments allowed, but result is ugly
11
Warning #3 will not Incorrect scanf specifier will not trigger an error Specifier, not variable, determines data read Illegal assignments allowed, but result is ugly YOUR RESPONSIBILITY to check in C/C++
12
scanf Example
15
Program Outline Once upon a time… Get the input using scanf from the keyboard … some stuff happens… … and they all lived happily ever after
16
Some Stuff Happens This really depends on specific project details Focus of most of term, we will skip this today
17
Program Outline … and they all lived happily ever after All good processing comes to end & report results Shows program worked and provides feedback Results takes many forms, focus on printing today When starting program, second steps ask: 1. What must be output? 2. How can output be presented best? 3. Will it be pretty?
18
Using printf to Print Already seen how to print text using printf printf(“Hello World\n”); Prints out whatever is placed between quotes Use escape sequences for fancier text output \n newline (move to start of next line) \t tab (go to next column that is multiple of 8) \\ \ (backslash character) \” “ (quotation mark)
19
Can Print Out Multiple Items printf can also print out value of variables int bob = 2; double j = 4.5; char var = ‘a’; printf(“Hello ”); printf(“%d\n”, bob); printf(“%lf\n”, j); printf(“%c\n”, var); printf(“%lf is not %d”, j, bob); printf(“%d equals bob\n”, bob); printf(“%c%d%lf”, var, bob, j);
20
But Can It Be Used? printf built to provide basics needed to work Prints out as many digits as needed No extra spaces or tabs used Scientific notation cannot be used Often want to format results Significant digits can matter Tables make reading faster
21
Formatting Output #include #include int main() { int k = 4, bigK = 100; double stuff = 1.3245; printf(“%d\n”, k); printf(“%3d%d\n”, k, k); printf(“%-4d%2d\n”, k, bigK); printf(“%05d\n”, bigK); printf(“%lf\n”, stuff); printf(“%06lf\n”, stuff); printf(“%.2lf\n”, stuff); printf(“%10.2lf\n”, stuff); }
22
Your Turn Get in groups & work on following activity
23
For Next Lecture
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.