PHY 107 – Programming For Science. The Week’s Goal.

23
LECTURE 5: INPUT & OUTPUT FUNCTIONS PHY 107 – Programming For Science

Transcript of PHY 107 – Programming For Science. The Week’s Goal.

Page 1: PHY 107 – Programming For Science. The Week’s Goal.

LECTURE 5:INPUT & OUTPUT FUNCTIONS

PHY 107 – Programming For Science

Page 2: PHY 107 – Programming For Science. The Week’s Goal.

The Week’s Goal

At end of todays’s lecture, you will be able to

Write (small, useless) C programs

Page 3: PHY 107 – Programming For Science. The Week’s Goal.

Announcements

Weekly assignment #2 available on to D2L

Page 4: PHY 107 – Programming For Science. The Week’s Goal.

Program Outline

Once upon a time… … some stuff happens… … and they all lived happily ever after

Page 5: PHY 107 – Programming For Science. The Week’s Goal.

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?

Page 6: PHY 107 – Programming For Science. The Week’s Goal.

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

Page 7: PHY 107 – Programming For Science. The Week’s Goal.

Even Better Magic

Must tell scanf types it will be reading readVariable Type Specifierint %i, %dlong int %li, %ldunsigned int %uunsigned long %lufloat %f, %e, %E, %g, %Gdouble %lf, %le, %lE, %lg, %lGlong double %Lf, %Le, %LE, %Lg, %LGchar %c

Page 8: PHY 107 – Programming For Science. The Week’s Goal.

Programming Using scanf

Used to read one or more values at once:

scanf(“%d”,&variable);scanf(“%f

%ld”,&variable1,&variable2); Reads where last scanf stopped reading

input Automatically skips whitespace between

numbers

Specifier determines what is read Stops reading at first non-usable value in

input If input is not usable, sets variable equal to

0

Page 9: PHY 107 – Programming For Science. The Week’s Goal.

Warning #1

Incorrect scanf specifier will not trigger an error Specifier, not variable, determines data read

Page 10: PHY 107 – Programming For Science. The Week’s Goal.

Warning #2

Incorrect scanf specifier will not trigger an error Specifier, not variable, determines data read Illegal assignments allowed, but result is

ugly

Page 11: PHY 107 – Programming For Science. The Week’s Goal.

Warning #3

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++

Page 12: PHY 107 – Programming For Science. The Week’s Goal.

scanf Example

Page 13: PHY 107 – Programming For Science. The Week’s Goal.

scanf Example

Page 14: PHY 107 – Programming For Science. The Week’s Goal.

scanf Example

Page 15: PHY 107 – Programming For Science. The Week’s Goal.

Program Outline

Once upon a time… Get the input using scanf from the

keyboard … some stuff happens… … and they all lived happily ever after

Page 16: PHY 107 – Programming For Science. The Week’s Goal.

Some Stuff Happens

This really depends on specific project details Focus of most of term, we will skip this

today

Page 17: PHY 107 – Programming For Science. The Week’s Goal.

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?

Page 18: PHY 107 – Programming For Science. The Week’s Goal.

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)

Page 19: PHY 107 – Programming For Science. The Week’s Goal.

Can Print Out Multiple Items printf can also print out value of

variablesint 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);

Page 20: PHY 107 – Programming For Science. The Week’s Goal.

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

Page 21: PHY 107 – Programming For Science. The Week’s Goal.

Formatting Output

#include <stdio.h>#include <stdlib.h>

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);}

Page 22: PHY 107 – Programming For Science. The Week’s Goal.

Your Turn

Get in groups & work on following activity

Page 23: PHY 107 – Programming For Science. The Week’s Goal.

For Next Lecture

Read p. 144-146 & web page for Wednesday How can we use the variables? What operations exist for us to use? Computers good at math; can we make it

do more?

Week #2 weekly assignment due Tuesday at 5PM Problems available on D2L If problem takes more than 10 minutes,

TALK TO ME!