Jerry Cain and Eric Roberts

Slides:



Advertisements
Similar presentations
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Advertisements

Histogram Most common graph of the distribution of one quantitative variable.
Simple Arrays Eric Roberts CS 106A February 10, 2010.
Business 90: Business Statistics
CSE331: Introduction to Networks and Security Lecture 17 Fall 2002.
Frequency Distributions and Their Graphs
Section 2.3: Substitution Ciphers Practice HW from Barr Textbook (not to hand in) p. 92 # 1, 2 # 3-5 (Use Internet Site)Use Internet Site.
Homework #5 New York University Computer Science Department Data Structures Fall 2008 Eugene Weinstein.
Unit 5C Statistical Tables and Graphs. TYPES OF DATA There are two types of data: Qualitative data – describes qualities or nonnumerical categories EXAMPLES:
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
Histograms Lecture 18 Sec Tue, Feb 17, 2004.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Simple Arrays Eric Roberts CS 106A February 12, 2016.
Using Data Files Eric Roberts CS 106A February 22, 2016.
Substitution Ciphers Reference –Matt Bishop, Computer Security, Addison Wesley, 2003.
Eric Roberts and Jerry Cain
Multidimensional Arrays
Mechanics of Functions
Jerry Cain and Eric Roberts
CST 1101 Problem Solving Using Computers
Eric Roberts and Jerry Cain
Chapter 4 The If…Then Statement
BUSINESS MATHEMATICS & STATISTICS.
Tuples and Lists.
Eric Roberts and Jerry Cain
Arrays: Checkboxes and Textareas
Chapter 5 - Control Structures: Part 2
Lecture 25 The Tao that can be talked about is not the true Tao
C++ Standard Library.
Introduction to Programming
Eric Roberts and Jerry Cain
Frequency Distributions and Their Graphs
Statistical Analysis with Excel
CS106A, Stanford University
CS 106A, Lecture 9 Problem-Solving with Strings
Statistical Analysis with Excel
Statistical Tables and Graphs
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Mechanics of Functions
Warm Up Problem Solve for x: 6x + 1 = 37.
Simple Arrays Eric Roberts CS 106A February 15, 2017.
Introduction to Matrices
THE STAGES FOR STATISTICAL THINKING ARE:
Encryption and Decryption
Data Tables Packet #19.
Web DB Programming: PHP
slides courtesy of Eric Roberts
slides courtesy of Eric Roberts
slides courtesy of Eric Roberts
Multidimensional Arrays
Fundamentals of Python: First Programs
THE STAGES FOR STATISTICAL THINKING ARE:
Frequency Tables number of times something occurs
Histograms Lecture 14 Sec Fri, Feb 8, 2008.
slides courtesy of Eric Roberts
Homework Reading Programming Assignments Finish K&R Chapter 1
CS106A, Stanford University
Statistical Reasoning
Suggested self-checks: Section 7.11 #1-11
CS2911 Week 2, Class 3 Today Return Lab 2 and Quiz 1
Chapter 17 JavaScript Arrays
CS 101 – Nov. 18 Finish image enhancement Communication (Chapter 15)
Descriptive Statistics
Displaying Distributions with Graphs
slides courtesy of Eric Roberts
Introduction to Computer Programming IT-104
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Univariate Description
Presentation transcript:

Jerry Cain and Eric Roberts Arrays for Tabulation Jerry Cain and Eric Roberts CS 106J May 12, 2017

Using Arrays for Tabulation Arrays turn out to be useful when you have a set of data values and need to count how many values fall into each of a set of ranges. This process is called tabulation. Tabulation uses arrays in a slightly different way from those applications that use them to store a list of data. When you implement a tabulation program, you use each data value to compute an index into an array of integers that keeps track of how many values fall into that category. The example of tabulation used in the text is a program that counts how many times each of the 26 letters appears in a sequence of text lines. Such a program would be very useful in decoding a letter-substitution ciphers, such as the one from Edgar Allan Poe’s short story “The Gold Bug,” which Eric showed in class.

Poe’s Cryptogram Puzzle The first step in solving Poe’s cryptogram was to construct a table showing how often each character appeared in the plaintext message. 8 33 ; 26 4 19 ‡ 16 ) * 13 5 12 6 11 ( 10 † 1 9 2 : 3 ? ¶ – • 5 3 ‡ † ) 6 * ; 4 8 2 • ¶ 1 ( : 9 ? – The problem here is to write a program that creates a letter-frequency table similar to the one that Poe used in his story. 3

Implementation Strategy The basic idea behind the program to count letter frequencies is to use an array with 26 elements to keep track of how many times each letter appears. As the program reads the text, it increments the array element that corresponds to each letter. T W A S B R I L L I G 1 1 1 1 2 1 2 1 1 1 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

CountLetterFrequencies.js /* * File: CountLetterFrequencies.js * ------------------------------- * This function displays a letter-frequency table for a file. */ import "file"; import "ArrayLib.js"; /* Lets the user choose a file and displays the frequency table */ function CountLetterFrequencies() { var callback = function(filename) { var text = File.read(filename); if (text === undefined) { console.log("Can't open " + filename); } else { displayFrequencyTable(createFrequencyTable(text)); } }; File.chooseFile(callback); page 1 of 2

CountLetterFrequencies.js /* Creates a letter frequency table from the specified string */ function createFrequencyTable(str) { var letterCounts = createArray(26, 0); for (var i = 0; i < str.length; i++) { var ch = str.charAt(i).toUpperCase(); if (ch >= "A" && ch <= "Z") { letterCounts[ch.charCodeAt(0) - "A".charCodeAt(0)]++; } return letterCounts; /* Displays the frequency table omitting letters whose count is 0 */ function displayFrequencyTable(letterCounts) { for (var i = 0; i < 26; i++) { var count = letterCounts[i]; if (count > 0) { var ch = String.fromCharCode("A".charCodeAt(0) + i); console.log(ch + ": " + count); /* * File: CountLetterFrequencies.js * ------------------------------- * This function displays a letter-frequency table for a file. */ import "file"; import "ArrayLib.js"; /* Lets the user choose a file and displays the frequency table */ function CountLetterFrequencies() { var callback = function(filename) { var text = File.read(filename); if (text === undefined) { console.log("Can't open " + filename); } else { displayFrequencyTable(createFrequencyTable(text)); } }; File.chooseFile(callback); page 2 of 2 skip code

Exercise: Display a Histogram Write a program that reads a file called MidtermGrades.txt containing one score (0-100) per line and displays a histogram of the scores divided into ranges 0-9, 10-19, and so on. Histogram *** ********* ******************************* ****************************************************** *********************************************************** ************************************************************** *************************************************** ******************************************************* ********************** Histograms are usually presented vertically. The one in this exercise is drawn horizontally because that program is so much easier to write.

the same image after equalization Image Histograms The statistical notions behind the histogram program are important in image processing. A few years ago, Keith Schwarz developed an assignment in which one of the exercises was to use histograms to equalize a photograph with insufficient contrast, as illustrated below: low-contrast image the same image after equalization The steps in the process are are illustrated on the next slide.

Equalization standard histogram cumulative histogram

The End