phy 107 – programming for science. the week’s goal

Post on 21-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

LECTURE 5:INPUT & OUTPUT FUNCTIONS

PHY 107 – Programming For Science

The Week’s Goal

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

Write (small, useless) C programs

Announcements

Weekly assignment #2 available on to D2L

Program Outline

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

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?

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

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

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

Warning #1

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

Warning #2

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

ugly

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

scanf Example

scanf Example

scanf Example

Program Outline

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

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

Some Stuff Happens

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

today

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?

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)

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

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

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

Your Turn

Get in groups & work on following activity

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!

top related