Compsci 100, Fall 2009 16’.1 Views of programming l Writing code from the method/function view is pretty similar across languages  Organizing methods.

Slides:



Advertisements
Similar presentations
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
Advertisements

COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
Binary Representation Introduction to Computer Science and Programming I Chris Schmidt.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Connecting with Computer Science, 2e
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Introduction to Computer Science I.
Introduction to Programming with Java, for Beginners
CMPT 120 Introduction to Computer Science and Programming I Chris Schmidt.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Professor Jennifer Rexford COS 217
CEG 320/520: Computer Organization and Assembly Language Programming1 CEG 320/520 Computer Organization and Assembly Language Programming.
Computer Science 101 Number Systems. Humans Decimal Numbers (base 10) Decimal Numbers (base 10) Sign-Magnitude (-324) Sign-Magnitude (-324) Decimal Fractions.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
The Study of Computer Science Chapter 0 Intro to Computer Science CS1510, Section 2.
CENG 311 Machine Representation/Numbers
1 CS/COE0447 Computer Organization & Assembly Language Pre-Chapter 2.
1 The Design of C: A Rational Reconstruction Jennifer Rexford.
1 TOPIC 1 INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING Topic 1 Introduction to Computer Science and Programming Notes adapted from Introduction to.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
The Study of Computer Science Chapter 0 Intro to Computer Science CS1510.
Computer Science 111 Fundamentals of Programming I Number Systems.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
CompSci 100 Prog Design and Analysis II
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Compsci 06/101, Spring What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Pointers OVERVIEW.
Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries.
Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control (BNL Phone #)
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
Compsci 06/101, Spring What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in.
Compsci 100, Spring What’s left to talk about? l Transforms  Making Huffman compress more  Understanding what transforms do Conceptual understanding,
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CPS 100, Fall What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in our lives?
CompSci 100e Program Design and Analysis II April 7, 2011 Prof. Rodger CompSci 100e, Spring p 1 h 1 2 e 1 r 1 4 s 1 * 2 7 g 3 o
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
CompSci Problem: finding subsets l See CodeBloat APT, requires finding sums of all subsets  Given {72, 33, 41, 57, 25} what is sum closest (not.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
Computer Science I CSCI Summer 2009 David E. Goldschmidt, Ph.D.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CompSci 100e 8.1 Plan for the Course! l Understand Huffman Coding  Data compression  Priority Queues  Bits and Bytes  Greedy Algorithms l Algorithms.
Compsci 101, Fall LWoC l Review Recommender, dictionaries, files  How to create recommendations in order? food.txt  Toward a Duke eatery-recommender.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Compsci 06/101, Fall What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in our.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
CompSci 100e 12.1 Sorting: In 2 Slides l Why do people study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm analysis.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
C is a high level language (HLL)
Compsci 101.2, Fall Plan for LDO101 l Ethical webpage scraping  Illustrate power of regular expressions  Python makes trying things relatively.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
CompSci 100e 10.1 Binary Digits (Bits) l Yes or No l On or Off l One or Zero l
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
From bits to bytes to ints
The Study of Computer Science Chapter 0
Engineering Problem Solving With C An Object Based Approach
Bottom up meets Top down
March 2006 Saeid Nooshabadi
Views of programming Writing code from the method/function view is pretty similar across languages Organizing methods is different, organizing code is.
Operations and Arithmetic
The Study of Computer Science Chapter 0
From bit to byte to char to int to long
Presentation transcript:

Compsci 100, Fall ’.1 Views of programming l Writing code from the method/function view is pretty similar across languages  Organizing methods is different, organizing code is different, not all languages have classes,  Loops, arrays, arithmetic, … l Program using abstractions and high level concepts  Do we need to understand 32-bit twos- complement storage to understand x =x+1?  Do we need to understand how arrays map to contiguous memory to use ArrayLists?

Compsci 100, Fall ’.2 Bottom up meets Top down l We teach programming by teaching abstractions  What’s an array? What’s an ArrayList?  What’s an array in C?  What’s a map, Hashmap? Treemap? l How are programs stored and executed in the JVM?  Differences on different architectures?  Similarities between Java and C++ and Python and PHP? l What about how the CPU works? How memory works? Shouldn’t we study this too?

Compsci 100, Fall ’.3 From bit to byte to char to int to long l Ultimately everything is stored as either a 0 or 1  Bit is binary digit a byte is a binary term (8 bits)  We should be grateful we can deal with Strings rather than sequences of 0's and 1's.  We should be grateful we can deal with an int rather than the 32 bits that comprise an int l If we have 255 values for R, G, B, how can we pack this into an int?  Why should we care, can’t we use one int per color?  How do we do the packing and unpacking?

Compsci 100, Fall ’.4 More information on bit, int, long l int values are stored as two's complement numbers with 32 bits, for 64 bits use the type long, a char is 16 bits  Standard in Java, different in C/C++  Facilitates addition/subtraction for int values  We don't need to worry about this, except to note: Infinity + 1 = - Infinity (see Integer.MAX_VALUE ) Math.abs(-Infinity) > Infinity l Java byte, int, long are signed values, char unsigned  What are values for 16-bit char? 8-bit byte?  Why did this matter in Burrows Wheeler?

Compsci 100, Fall ’.5 Signed, unsigned, and why we care l Some applications require attention to memory-use  Differences: one-million bytes, chars, and int First requires a megabyte, last requires four megabytes When do we care about these differences?  Memory is cheaper, faster, …But applications expand to use it Java signed byte : , # bits?  What if we only want 0-255? (Huff, pixels, …)  Convert negative values or use char, trade-offs? Java char unsigned: 0..65,536 # bits?  Why is char unsigned? Why not as in C++/C?

Compsci 100, Fall ’.6 More details about bits l How is 13 represented?  … _0_ _0_ _1_ _1_ _0_ _1_  Total is = 13 l What is bit representation of 32? Of 15? Of 1023?  What is bit-representation of 2 n - 1 ?  What is bit-representation of 0? Of -1? Study later, but -1 is all 1’s, left-most bit determines < 0 l Determining what bits are on? How many on?  Understanding, problem-solving

Compsci 100, Fall ’.7 How are data stored? l To facilitate Huffman coding we need to read/write one bit  Why do we need to read one bit?  Why do we need to write one bit?  When do we read 8 bits at a time? 32 bits? l We can't actually write one bit-at-a-time. We can't really write one char at a time either.  Output and input are buffered,minimize memory accesses and disk accesses  Why do we care about this when we talk about data structures and algorithms? Where does data come from?

Compsci 100, Fall ’.8 How do we buffer char output? l Done for us as part of InputStream and Reader classes  InputStreams are for reading bytes  Readers are for reading char values  Why do we have both and how do they interact? Reader r = new InputStreamReader(System.in);  Do we need to flush our buffers? l In the past Java IO has been notoriously slow  Do we care about I? About O?  This is changing, and the java.nio classes help Map a file to a region in memory in one operation

Compsci 100, Fall ’.9 Buffer bit output l To buffer bits we store bits in a buffer (duh)  When the buffer is full, we write it.  The buffer might overflow, e.g., in process of writing 10 bits to 32-bit capacity buffer that has 29 bits in it  How do we access bits, add to buffer, etc.? l We need to use bit operations  Mask bits -- access individual bits  Shift bits – to the left or to the right  Bitwise and/or/negate bits

Compsci 100, Fall ’.10 Representing pixels l A pixel typically stores RGB and alpha/transparency values  Each RGB is a value in the range 0 to 255  The alpha value is also in range 0 to 255 Pixel red = new Pixel(255,0,0,0); Pixel white = new Pixel(255,255,255,0); l Typically store these values as int values, a picture is simply an array of int values void process(int pixel){ int blue = pixel & 0xff; int green = (pixel >> 8) & 0xff; int red = (pixel >> 16) & 0xff; }

Compsci 100, Fall ’.11 Bit masks and shifts void process(int pixel){ int blue = pixel & 0xff; int green = (pixel >> 8) & 0xff; int red = (pixel >> 16) & 0xff; } l Hexadecimal number: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f  f is 15, in binary this is 1111, one less than  The hex number 0xff is an 8 bit number, all ones l Bitwise & operator creates an 8 bit value, 0—255  Must use an int/char, what happens with byte?  1&1 == 1, otherwise we get 0 like logical and  Similarly we have |, bitwise or

Compsci 100, Fall ’.12 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries  Safe for advanced programming and beginners  Harder to shoot ourselves in the foot l Why don't we use C++ (or C)?  Standard libraries weak or non-existant (comparatively)  Easy to make mistakes when beginning  No GUIs, complicated compilation model  What about other languages?

Compsci 100, Fall ’.13 Why do we learn other languages? l Perl, Python, PHP, Ruby, C, C++, Java, Scheme, ML,  Can we do something different in one language? Depends on what different means. In theory: no; in practice: yes  What languages do you know? All of them.  In what languages are you fluent? None of them l In later courses why do we use C or C++?  Closer to the machine, we want to understand the machine at many levels, from the abstract to the ridiculous Or at all levels of hardware and software  Some problems are better suited to one language What about writing an operating system? Linux?

Compsci 100, Fall ’.14 Unique words in Java import java.util.*; import java.io.*; public class Unique { public static void main(String[] args) throws IOException{ Scanner scan = new Scanner(new File("/data/melville.txt")); TreeSet set = new TreeSet (); while (scan.hasNext()){ String str = scan.next(); set.add(str); } for(String s : set){ System.out.println(s); }

Compsci 100, Fall ’.15 Bjarne Stroustrup, Designer of C++ l Numerous awards, engineering and science  ACM Grace Hopper l Formerly at Bell Labs  Now Texas A&M l “There's an old story about the person who wished his computer was as easy to use as his telephone. That wish has come true, since I no longer know how to use my telephone.” Bjarne Stroustrup

Compsci 100, Fall ’.16 Unique words in C++ #include using namespace std; int main(){ ifstream input("/data/melville.txt"); set unique; string word; while (input >> word){ unique.insert(word); } set ::iterator it = unique.begin(); for(; it != unique.end(); it++){ cout << *it << endl; } return 0; }

Compsci 100, Fall ’.17 PHP, Rasmus Lerdorf and Others l Rasmus Lerdorf  Qeqertarsuaq, Greenland  1995 started PHP, now part of it  l Personal Home Page  No longer an acronym l “When the world becomes standard, I will start caring about standards.” Rasmus Lerdorf

Compsci 100, Fall ’.18 Unique words in PHP <?php $wholething = file_get_contents("file:///data/melville.txt"); $wholething = trim($wholething); $array = preg_split("/\s+/",$wholething); $uni = array_unique($array); sort($uni); foreach ($uni as $word){ echo $word." "; } ?>

Compsci 100, Fall ’.19 Guido van Rossum l BDFL for Python development  Benevolent Dictator For Life  Late 80’s began development l Python is multi-paradigm  OO, Functional, Structured, … l We're looking forward to a future where every computer user will be able to "open the hood" of their computer and make improvements to the applications inside. We believe that this will eventually change the nature of software and software development tools fundamentally. Guido van Rossum, 1999!

Compsci 100, Fall ’.20 Unique Words in Python #! /usr/bin/env python import sys import re def main(): f = open('/data/melville.txt', 'r') words = re.split('\s+',f.read().strip()) allWords = set() for w in words: allWords.add(w) for word in sorted(allWords): print "%s" % word if __name__ == "__main__": main()

Compsci 100, Fall ’.21 Kernighan and Ritchie l First C book, 1978 l First ‘hello world’ l Ritchie: Unix too!  Turing award 1983 l Kernighan: tools  Strunk and White l Everyone knows that debugging is twice as hard as writing a program in the first place. So if you are as clever as you can be when you write it, how will you ever debug it? Brian Kernighan

Compsci 100, Fall ’.22 How do we read a file in C? #include int strcompare(const void * a, const void * b){ char ** stra = (char **) a; char ** strb = (char **) b; return strcmp(*stra, *strb); } int main(){ FILE * file = fopen("/data/melville.txt","r"); char buf[1024]; char ** words = (char **) malloc(5000*sizeof(char **)); int count = 0; int k;

Compsci 100, Fall ’.23 Storing words read when reading in C while (fscanf(file,"%s",buf) != EOF){ int found = 0; // look for word just read for(k=0; k < count; k++){ if (strcmp(buf,words[k]) == 0){ found = 1; break; } if (!found){ // not found, add to list words[count] = (char *) malloc(strlen(buf)+1); strcpy(words[count],buf); count++; } l Complexity of reading/storing? Allocation of memory?

Compsci 100, Fall ’.24 Sorting, Printing, Freeing in C qsort(words,count,sizeof(char *), strcompare); for(k=0; k < count; k++) { printf("%s\n",words[k]); } for(k=0; k < count; k++){ free(words[k]); } free(words); } l Sorting, printing, and freeing  How to sort? What’s analgous to comparator?  Why do we call free? Necessary in this program? Why?