csc 107 - programming for science lecture 4: beginning programming

21
CSC 107 - Programming for Science Lecture 4: Beginning Programming

Upload: hilary-wilson

Post on 18-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 107 - Programming for Science Lecture 4: Beginning Programming

CSC 107 -Programming for Science

Lecture 4:

Beginning Programming

Page 2: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Problem of the Day

At what times do the minute and hour hands on an analog clock line up?

Page 3: CSC 107 - Programming for Science Lecture 4: Beginning Programming

The Week’s Goal

At the end of the week, you should be able to write (small, useless) C programs on your ownBut, for this week, requires some “magic”Will examine material in greater depth later in

term

Page 4: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Comments

Key to any program Describe code in simple English

Sie konnen auch auf Deutsch screibeno U c%d wrte n txt msg

Should be used liberally I add comments anywhere where I cannot

immediately tell what code does Impossible to have too many comments

Page 5: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Comments in C Program

Double slash comments out rest of linea = a – 4; // Hi, Mom!// This entire line is a comment!

/* … */ comments can go across linesa = a - /* Hi, Mom! */ 4;/* This comment takes an entire line. *//* This is a really long comment that * goes on to multiple lines. The stars on * lines 2 and on are optional, but * makes things easier to read. */

Page 6: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Pre-processor Directives

Code “pre-processed” before compilationNo need to request it --- automatically occursUsed to make code simpler & easier to read

Notice a recurring theme?

Pre-processor directives start with #Each directive must be on own lineDirectives should not span multiple lines

Page 7: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Starting a File

C code files usually named something.cSomething could be any legal name you’d like

Nearly all “*.c” files start with 2 directives: /* For now, think of these as magic */ #include <stdio.h>#include <stdlib.h>

Page 8: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Symbolic Constants

Directive can be used to name a constantUse name on any/all lines BELOW directive

Pre-processor replaces name with valueCompiler only sees the constant valueProgrammer only sees the nameMakes code far easier to read, write, debug

Names traditionally in all CAPITAL letters

Page 9: CSC 107 - Programming for Science Lecture 4: Beginning Programming

What You Write And Work With

#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = PI * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);

Page 10: CSC 107 - Programming for Science Lecture 4: Beginning Programming

What The Compiler Sees

#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = PI * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);

#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = 3.1415962 * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);

#define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEarea = 3.1415962 * (r * r);puts(MY_NAME);puts(DUMB_EXAMPLE);

#define DUMB_EXAMPLE “Matthew Hertz”area = 3.1415962 * (r * r);puts(“Matthew Hertz”);puts(DUMB_EXAMPLE);

area = 3.1415962 * (r * r);puts(“Matthew Hertz”);puts(“Matthew Hertz”);

Page 11: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Beginning of Every C Program

Programs must include function named mainThis is where the program start executingFunctions discussed in more detail next weekFor this week, think of this as more “magic”

Page 12: CSC 107 - Programming for Science Lecture 4: Beginning Programming

More About main()

int main(int argc, char* argv[]) { // Put code here

/* Add these 2 lines at the end */ return 0;}

Page 13: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Variables

Variables name a memory location where program can store dataValue at memory location is initially unknownAssignments to variable update the memory

locationWhen variable used in program, computer

uses value stored at that memory location

Page 14: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Variable Declarations

Variables must be declared before its useDeclarations must be at start of function

Each declaration includes two pieces:Type of data that the variable storesName of the variable

Page 15: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Variable Names

Begin with letter or underscore (_)Then use any letters, numbers, or underscore

Names are case-sensitiveMass, mass, & masS are different

Each variable must have unique nameComputer does not know which of your 1,000

“bob” variables to use Cannot use one of C’s reserved words

List on p. 38 should say “int”, not “ints”

Page 16: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Variable Name Conventions

Usually begin with lowercase letterHelps clarify variables & symbolic constants

Provide good idea of what variable storesSplit multiple uses into multiple variablestmp, b, and anything you would not say in

front of your parents/priest are not good

Page 17: CSC 107 - Programming for Science Lecture 4: Beginning Programming

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, longDecimal types: float, double, long doubleDoes NOT specify ranges for each type

char data type can hold a character

Page 18: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Writing Variable Declarations

Single variable declared as: type name;double goodNameExample;short bad;

Declare multiple variables at one time:int i, j;long double k, l, m, n, o, p;float thisIsAReallyLongName, thisIsAnotherLongName;

Page 19: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Writing Variable Declarations

Could also specify initial value for variableint i = 0.0;long j = -1;long double k = -0.000123928478812;long l = j, many, minusJ = -j;char c = ‘a’;char newLine = ‘\n’;char tab = ‘\t’;

Page 20: CSC 107 - Programming for Science Lecture 4: Beginning Programming

Your Turn

Divide into groups of 3 and complete the daily activity

Page 21: CSC 107 - Programming for Science Lecture 4: Beginning Programming

For Next Lecture

Read through Section 2.3 of bookDo not need to understand all the detailsBut important knowing what is not understood

Review homework assignment for week 2Covers material from this week’s lectures