Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 - Programming for Science Lecture 5: Actual Programming.

Similar presentations


Presentation on theme: "CSC 107 - Programming for Science Lecture 5: Actual Programming."— Presentation transcript:

1 CSC 107 - Programming for Science Lecture 5: Actual Programming

2 Today’s Goal At the end of today’s lecture, you should be able to write a reasonable C program  This includes input & output!  We will have a real lab this week!

3 Variables Variables name memory location for program to store data  Variable’s initial value is unknown  Assignments update data stored at memory location  Variable’s value used whenever program uses variable

4 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

5 More Data Types Each numeric data type is actually 2 types  signed (default) and unsigned int i, j, k; unsigned double l, m, n; Unsigned data must be non-negative  Upper range of variable is doubled  Useful for some data: degreesKelvin, timeObservation, weight, homeworkScore C compiler emits warning when mixing signed & unsigned data

6 Printing Out Results C defines function to print information out  Function is called printf  Information is printed out in window where program is run  Arguments to printf determine what is printed printf’s arguments can be very esoteric  Do not memorize; this is why manuals exist

7 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

8 Even Better Magic Must tell printf how to format variables C will not complain for wrong specifier, but results are bad Variable TypeSpecifier int%i, %d short%hi, %hd long int%li, %ld unsigned int%u unsigned short%hu unsigned long%lu float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG char%c

9 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

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

11 scanf C defines scanf to get user’s input  Very similar in usage to printf  Sets variables equal to values input on keyboard scanf waits until it has input  Wait could be years if nothing is input  Lines cannot be read until enter is pressed

12 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

13 scanf Examples double d; int i; unsigned int u; scanf(“%u”, &u); scanf(“%f %d\n”, &d, &i); scan(“%f\n%d”, &d, &i); scan f (“%f%\n”, &d); Ampersand (&) before each variable name is vital  Needed to assign variable a new value

14 Your Turn Get into groups and complete daily activity

15 For Next Lecture Read through Section 2.6 of book  Do not need to understand all the details  But important knowing what is not understood Start homework assignment for week 3  Covers material from this week’s lectures


Download ppt "CSC 107 - Programming for Science Lecture 5: Actual Programming."

Similar presentations


Ads by Google