LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First...

54
CS102: Introduction to Computer Science LESSON 4: © 2015 Sean Cusack © xkcd

Transcript of LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First...

Page 1: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

CS102:Introduction to Computer Science

LESSON 4:

© 2015 Sean Cusack

© xkcd

Page 2: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Firing one of our Translators

● Instead of:gcc -S foo.c -o foo.sgcc foo.s -o foo

● We can now only pay for one translator goingforwards (someone who knows all 3):

gcc foo.c -o foo● Hooray for simplification!

Page 3: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Raid kills bugs... dead

● cd cs102e/lab-4

● cp ../homework-3/repetitive.c ./

● gcc -g repetitive.c -o repetitive

● gdb repetitive

● The "gdb" program runs another program, lineby line – as long as it was compiled with -g

● It's not as important today, but good to use, andwill be invaluable as programs get morecomplex (and especially when they crash)

Page 4: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 5: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Git off my lawn, ye 'varmint!

● "git" is a tool on UNIX that does "revision control"● This means you can keep records of files over time,

and track how they change● You can also save your work to go back to previous

versions, if the present version doesn't work● ...Or if you accidentally deleted it!● By the way, you can also download and install this at

home: http://git-scm.com/

Page 6: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

My First Git

● Once only, on students, never again:git config --global user.name "Sean Cusack"

git config --global user.email [email protected]

● In your lab-4 directorygit init

git status

git add repetitive.c

git commit -a -m "my first commit"

Page 7: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 8: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Ignoring what we don't want to see

Page 9: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Viewing differences...

Page 10: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Compile, test, commit...

Page 11: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

SAVE ME!

● For when you accidentally delete something...

Page 12: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

HELP!

● man cp

● man ls

● git help

● git help commit

● man gcc

● man man

Page 13: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Last week's sum

● Just calculating a sum doesn't do much good if the program cannotcommunicate this information back to the user...

int i = 1;

int sum = 0;

while( i <= 10 )

{

sum = sum + i;

i = i + 1;

}

Page 14: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

fprintf mistake

● If you tell it to print "sum = i" it will print exactly that, character-for-character...

int i = 1;

int sum = 0;

while( i <= 10 )

{

sum = sum + i;

i = i + 1;

}

fprintf( stdout, "sum = sum\n" );

Page 15: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

fprintf

● Functions in math may have multiple arguments,like f(x,y,z)

● Likewise fprintf has until now had two:– stdout

– "stuff in quotes"

● It can also accept more arguments. How many?It depends on what is in the quotes.

● Whenever it encounters a percent sign %something special happens.

Page 16: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

fprintf's friend "%d"

● Inside the double quotes of fprintf(), a %d is a "placeholder" for aninteger that follows the quotes and a comma

int i = 1;

int sum = 0;

while( i <= 10 )

{

sum = sum + i;

i = i + 1;

}

fprintf( stdout, "sum = %d\n", sum );

Page 17: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

More on fprintf

● Just like "\n" doesn't literally get printed as a backslash and an n,neither does %d get printed as a percent and a "d".

● Instead, it says, when we're done with stuff in quotes, look for avariable to get an integer value from.

● If there are two, then after the quotes, it will expect two integervalues, and replace them in order:i = 1;fprintf( stdout, "foo %d bar %d\n", 5, i+1 );

● This would print the following:foo 5 bar 2

Page 18: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Programming methods

● Sometimes you know what you need to do but don'tknow how to program it

● And sometimes you know the bits and parts ofprogramming you think are necessary, or solve arelated problem, but don't know how to applythem

● We will run into both issues in this class● It's best to build up a "tool belt" of common

programming paradigms and methods, so youhave a few options to try on new problems

Page 19: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Counter terminology

● A variable, used just to count things so that aloop (while or for) can stop at the right time, iscalled either:

– counter

– iterator

● And "i"terator is why the variable "i" is used sooften, you may also see "ii"

● Or in our case, "x", since we're about to graph afunction

Page 20: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Common Trends

● Much of programming has to do withmanipulating either f(x), or f(x-something), ina loop

● Why? Because computers are good at doingrepetitive, boring tasks over and over, like re-evaluating a function for many, many X

● We already can print out the iterator's numbers,now we just print out the result of a formulathat gets re-evaluated each time

Page 21: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Data Transformations● We start with a simple list, something we have to

assume we can do by now (count in a loop):(1, 2, 3, 4, 5...)

● And transform it into (or produce using it) intomultiples of 13:(13, 26, 39, 52, 65...)

● Like one gear powering more gears... the iteratoris like the tick of the clock that the rest of theprogram clicks along to

● If we know what we need to do but can't programit yet, one way is to lay it out in a spreadsheet...

Page 22: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Multiples of 13 Spreadsheet

i multiple command1 13 print multiple, move to next i2 26 print multiple, move to next i3 39 print multiple, move to next i3 52 print multiple, move to next i… … …11 130 STOP

Page 23: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

New Program

/* Print multiples of 13 */#include <stdio.h>int main( int argc, char **argv ){ int i = 1; int multiple = 0; while( i <= 10 ) { multiple = i * 13; fprintf( stdout, "multiple = %d\n", multiple ); i = i + 1; } return 0;}

Page 24: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Multiples of 13 Spreadsheet

i multiple command1 01 13 multiple = i * 131 13 print multiple2 13 i = i + 12 13 i <= 10 ?2 26 multiple = i * 132 26 print multiple3 26 i = i + 13 26 i <= 10 ?3 39 multiple = i * 133 39 print multiple4 39 i = i + 14 39 i <= 10 ?… … …11 130 i <= 10 ?

Page 25: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

OR

/* Print multiples of 13 */#include <stdio.h>int main( int argc, char **argv ){ int i = 1;

while( i <= 10 ) {

fprintf( stdout, "multiple = %d\n", ( i * 13 ) ); i = i + 1; } return 0;}

Page 26: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Multiples of 13 Spreadsheet

i i*13 command1 131 13 print i*132 13 i = i + 12 13 i <= 10 ?2 26 print i*133 26 i = i + 13 26 i <= 10 ?3 39 print i*134 39 i = i + 14 39 i <= 10 ?… … …11 130 i <= 10 ?

Page 27: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

NOT This Program

/* Print multiples of 13 */#include <stdio.h>int main( int argc, char **argv ){ int i = 1; int multiple = 0; multiple = i * 13; /* bad - we put 1*13 in a bucket */ while( i <= 10 ) { /* and then never update what's in the bucket */ fprintf( stdout, "multiple = %d\n", multiple ); i = i + 1; } return 0;}

Page 28: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Multiples of 13 [Error] Spreadsheet

i multiple command1 01 13 multiple = i * 131 13 print multiple2 13 i = i + 12 13 i <= 10 ?2 13 print multiple3 13 i = i + 13 13 i <= 10 ?3 13 print multiple4 13 i = i + 14 13 i <= 10 ?… … …11 13 i <= 10 ?

Page 29: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

● Bring up 2 windows on students.cooper.edu in lab-4

● On one, open a new file multi.c - keep the file open and savebut don't close

● Use the other for gcc, running ./multi, and git

● If you haven't done “git init” yet, do so now, if you did, don't

● git add multi.c

● git commit -a -m 'my first commitment'

● Fill the file with our example, save - in the other window, gccit, run it, and if it works, commit it again(no init or add again)

Lab 0 - Work on together

Page 30: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

More Multiples

/* Print multiples of 13 and another value (more) as well */#include <stdio.h>int main( int argc, char **argv ){ int i = 1; int multiple = 0; int more = 0; fprintf( stdout, "multiple,more\n" ); while( i <= 10 ) { multiple = i * 13; more = multiple + 5; fprintf( stdout, "%d,%d\n", multiple, more ); i = i + 1; } return 0;}

Page 31: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

More Multiples Spreadsheet

i multiple more command1 0 01 13 0 multiple=i*131 13 18 more=multiple+51 13 18 print multiple, more2 13 18 i = i + 12 13 18 i <= 10 ?2 26 18 print i*132 26 18 multiple=i*132 26 31 more=multiple+53 26 31 print multiple, more3 26 31 i <= 10 ?3 39 31 multiple=i*133 39 44 more=multiple+53 39 44 print multiple, more4 39 44 i = i + 14 39 44 i <= 10 ?… … …11 130 135 i <= 10 ?

Page 32: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

More Multiples Output

multiple,more

13,18

26,31

39,44

52,57

65,70

78,83

91,96

104,109

117,122

130,135

Page 33: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Git Lost?

● Make a change to the program, like change themultiplier to 20 instead of 13, save

● In the other window, gcc and run it● Run “git diff”● You can see what you did● git commit -a -m 'describe your changes'

Page 34: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Git broken

● Break your program - edit it and make it notwork. Save. Exit pico.

● In the other window: git diff● Try to gcc it...

Oh noes! Program broken, all is lost!● git reset --hard HEAD● Here, “HEAD” means the latest commit (or save)

of the directory, so you can get your programback to the way it was

Page 35: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

More Multiples Output - Graphing

● Let's learn to use excel to graph the data weprinted

● Make sure multi.c is back to working order● gcc, run, git commit

Page 36: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 37: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 38: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Paste from your program output

Page 39: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 40: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 41: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 42: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"
Page 43: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Labs, and git

● Make sure you do this all in lab-4 on students● Call your programs lab1.c, lab2.c, lab3.c etc● From UNIX, make sure to "git add" the .c files● And every time you make some milestone (get

something that works), make sure to "gitcommit" it

● If you're working from home, you can useanything you want, and scp the files in

Page 44: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

In-Class Lab #1

● Given the initial values:– Start at x = 0

– End at x = 200

– In steps of 2

– Use polynomial “y = 1 + x2”

● Print x,y for those x, with “x,y” at the top● Note that there is no "squared" operator in C, so

x-squared is x*x, x-cubed is x*x*x, etc

Page 45: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Y(X) Spreadsheet

X Y Command0 00 1 y=1+x*x0 1 print x, y2 1 next x2 1 x <= 200 ?2 5 y=1+x*x2 5 print x, y4 5 next x4 5 x <= 200 ?… … …

202 40001 x <= 200 ?

Page 46: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

In-Class Lab #2

● Given the values:

– Start at x = 0

– End at x = 200

– In steps of 2

– Use polynomial “y = 1 + x2”

– Do not derive the function on paper first!You don't need calculus to do this. Do not do yprime=2*x

– Instead, expand the functions:

yprime = (y(x)-y(x-h))/h

● Print x,y,yprime and header line “x,y,yprime”

● Also, for those that may know how, don't use functions, either

Page 47: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Y'(X) Spreadsheet

X Y Yp Command0 0 00 1 0 y=1+x*x0 1 -20 1 -22 1 -2 next x2 1 -2 x <= 200 ?2 5 -2 y=1+x*x2 5 22 5 24 5 2 next x4 5 2 x <= 200 ?… … …

202 40001 398 x <= 200 ?

yp=(y-(what?))/?print x, y, yp

yp=(y-(what?))/?print x, y, yp

Page 48: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Rise over Run

x-h x

f(x-h)f(x)

h

Page 49: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

In-Class Lab #3● Given the values:

– Start at x = 0

– End at x = 200

– In steps of 2

– Use polynomial “y = 1 + x2”

– Do not integrate the function on paper first!

– Instead, expand the functions:

= ((y(x)+y(x-h))/2)*h

● Print x,y,yprime,yint and header

● Also, for those that may know how, don't use functions, either

∫ y dx

Page 50: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

INT(Y(X)) SpreadsheetX Y Yp Yi Command0 0 0 00 1 0 0 y=1+x*x0 1 -2 02 1 -2 60 1 -2 62 1 -2 6 next x2 1 -2 6 x <= 200 ?2 5 -2 6 y=1+x*x2 5 2 62 5 2 62 5 2 64 5 2 6 next x4 5 2 6 x <= 200 ?… … …

202 40001 398 79206 x <= 200 ?

yp=(y-(what?))/?yi=(y+(what?))/? * ?

print x, y, yp, yi

yp=(y-(what?))/?yi=(y+(what?))/? * ?

print x, y, yp, yi

Page 51: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Area

x-h x

f(x-h)f(x)

Page 52: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

In-Class Lab #4● Given the values:

– Start at x = 0

– End at x = 200

– In steps of 2

– Use polynomial “y = 1 + x2”

– Calculate the sum of ((y(x)+y(x-h))/2)*h at each point

– You already have the value, just keep a running total“ysum”, just like the “sum of integers 1-10” from lastweek

● Print x,y,yprime,yint,ysum and header

● Also, for those that may know how, don't use functions, either

Page 53: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Running Sum INT(Y(X)) SpreadsheetX Y Yp Yi Command0 0 0 0 00 1 0 0 0 y=1+x*x0 1 -2 0 02 1 -2 6 02 1 -2 6 60 1 -2 6 62 1 -2 6 6 next x2 1 -2 6 6 x <= 200 ?2 5 -2 6 6 y=1+x*x2 5 2 6 62 5 2 6 62 5 2 6 122 5 2 6 124 5 2 6 12 next x4 5 2 6 12 x <= 200 ?… … …

202 40001 398 79206 2667006 x <= 200 ?

Ysum

yp=(y-(what?))/?yi=(y+(what?))/? * ?

ysum = ysum + what?print x, y, yp, yi, ysum

yp=(y-(what?))/?yi=(y+(what?))/? * ?

ysum = ysum + what?print x, y, yp, yi, ysum

Page 54: LESSON 4 - students.cooper.edustudents.cooper.edu/cusack/cs102c-fall-2016/Lecture04.pdf · My First Git Once only, on students, never again: git config --global user.name "Sean Cusack"

Running Sum

x-h x

f(x-h)f(x)

Note 0-h