csc 107 - programming for science lecture 5: actual programming
TRANSCRIPT
![Page 1: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/1.jpg)
CSC 107 -Programming for Science
Lecture 5:
Actual Programming
![Page 2: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/2.jpg)
Today’s Goal
At the end of today’s lecture, you should be able to write a reasonable C programThis includes input & output!We will have a real lab this week!
![Page 3: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/3.jpg)
Variables
Variables name memory location for program to store dataVariable’s initial value is unknownAssignments update data stored at memory
locationVariable’s value used whenever program uses
variable
![Page 4: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/4.jpg)
Data Types
Each variable also has data type Specifies how program treats variable’s value
C defines 6 numeric data types Integer types: short, int, long Decimal types: float, double, long double Does NOT specify ranges for each type
char data type holds a character C will only allow certain assignments
Assign an integer to a decimal variable --- easy Assign decimal to integer variable --- not easy
![Page 5: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/5.jpg)
More Data Types
Each numeric data type is actually 2 typessigned (default) and unsignedint i, j, k;unsigned double l, m, n;
Unsigned data must be non-negativeUpper range of variable is doubledUseful for some data: degreesKelvin,
timeObservation, weight, homeworkScore C compiler emits warning when mixing
signed & unsigned data
![Page 6: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/6.jpg)
Printing Out Results
C defines function to print information outFunction is called printf Information is printed out in window where
program is runArguments to printf determine what is
printed printf’s arguments can be very esoteric
Do not memorize; this is why manuals exist
![Page 7: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/7.jpg)
printf
printf(“Hi, Mom\n”); First argument must be in quotes Except for a few magic commands, it
prints out what you specify\n print out newline\t print out tab\\ print out “\” character\b sound error bell
![Page 8: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/8.jpg)
Even Better Magic
Must tell printf how to format variables
C will not complain for wrong specifier, but results are bad
Variable Type Specifierint %i, %dshort %hi, %hdlong int %li, %ldunsigned int %uunsigned short %huunsigned long %lufloat %f, %e, %E, %g, %Gdouble %lf, %le, %lE, %lg, %lGlong double %Lf, %Le, %LE, %Lg, %LGchar %c
![Page 9: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/9.jpg)
Printing Out Data
Identifier placed in first argument Identifier replaced with value of variable, symbolic
constant, or literal
Values supplied in next arguments Must be included in order identifiers appear
caveat emptor when using function Compiler does NOT check identifiers or argument Too many can cause later problems Too few arguments results in odd printouts
![Page 10: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/10.jpg)
printf Examples
double d;int i;unsigned int u;printf(“This is a boring example.\n”);
printf(“Prints a double -- %f\n”, d);
printf(“Prints 2 digits beyond decimal -- %.2f\n”, d);
printf(“Prints an int -- %d\n”, i);
printf(“Prints an unsigned int %u\n”, u);
printf(“Prints i’s (%d) & u’s (%u) value”, i, u);
printf(“i + u = %u\t i + d = %d\n”, i+u, i+d);
printf(“Printing a percent sign is 100%% hard”);
![Page 11: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/11.jpg)
scanf
C defines scanf to get user’s inputVery similar in usage to printfSets variables equal to values input on
keyboard scanf waits until it has input
Wait could be years if nothing is inputLines cannot be read until enter is pressed
![Page 12: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/12.jpg)
Arguments to scanf
First argument must be in quotes Must state type of data to be read
Uses same identifiers as printf Afterward specify variables to assign
Data types should match (otherwise program results will be odd)
Need to list “&” before each variable name
![Page 13: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/13.jpg)
scanf Examples
double d;int i;unsigned int u;scanf(“%u”, &u);
scanf(“%f %d\n”, &d, &i);
scan(“%f\n%d”, &d, &i);
scanf(“%f%%\n”, &d); Ampersand (&) before each variable name is vital
Needed to assign variable a new value
![Page 14: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/14.jpg)
Your Turn
Get into groups and complete daily activity
![Page 15: CSC 107 - Programming for Science Lecture 5: Actual Programming](https://reader036.vdocuments.mx/reader036/viewer/2022082610/56649ce15503460f949abd06/html5/thumbnails/15.jpg)
For Next Lecture
Read through Section 2.6 of bookDo not need to understand all the detailsBut important knowing what is not understood
Start homework assignment for week 3Covers material from this week’s lectures