Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

For(int i = 1; i
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Josephus Problem: Build the Circular Linked List
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
2.1 Functions. f x y z f (x, y, z) 3 Functions (Static Methods) Java function. n Takes zero or more input arguments. n Returns one output value. Applications.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October 21, 2015.
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
CS177 RECITATION WEEK 7 Input and Output (Text & Graphical)
CompSci 100E 3.1 Random Walks “A drunk man wil l find his way home, but a drunk bird may get lost forever”  – Shizuo Kakutani Suppose you proceed randomly.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 9/10/08 9/10/08.
Repetition Statements while and do while loops
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
Computer Science 320 Massive Parallelism. Example Problem: Breaking a Cipher Somehow obtain a sample plaintext and its ciphertext Then search for the.
2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
Take out a piece of paper and PEN. The quiz starts ONE minute after the tardy bell rings. You will have 45 – 90 seconds per question. Determine the output.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
CompSci 100E 4.1 Google’s PageRank web site xxx web site yyyy web site a b c d e f g web site pdq pdq.. web site yyyy web site a b c d e f g web site xxx.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
CS001 Introduction to Programming Day 6 Sujana Jyothi
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Introduction to programming in java Lecture 05 Review of 1 st four lectures and Practice Questions.
Chapter 10 – Trigonometric (Functions) Q1
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Department of Computer Science
Arrays 3/4 By Pius Nyaanga.
Command Line Arguments
using System; namespace Demo01 { class Program
Chapter 10 Arrays.
Introduction to programming in java
Week 8 - Friday CS 121.
Java 24th sep /19/2018 CFILT.
Function Call Trace public class Newton {
Chapter 10 Arrays.
Chapter 6 Arrays.
Computing Adjusted Quiz Total Score
Multidimensional Arrays
Java so far Week 7.
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
محاور المحاضرة خوارزمية الفقاعات الهوائية (Bubble Sort)
Week 9 – Audio Processing
Code Animation Examples
References, Objects, and Parameter Passing
Method Overloading in JAVA
CS2011 Introduction to Programming I Arrays (II)
Recursive GCD Demo public class Euclid {
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Why did the programmer quit his job?
while while (condition) { statements }
1.5 Input and Output Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 2/14/11.
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/6/11 12:33.
Introduction to Computer Science I.
Local variables and how to recognize them
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Consider the following code:
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
PROGRAMMING ASSIGNMENT I
First Semester Review.
MIS 222 – Lecture 12 10/9/2003.
Presentation transcript:

Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010

Plotting a Function Graph

Sampling y=sin(4x) + sin(20x) public class PlotFunction { // Plot y = sin(4x) + sin(20x) public static void main(String[] args) { int N = Integer.parseInt(args[0]); double[] a = new double[N+1]; for (int i = 0; i <= N; i++) a[i] = Math.sin(4*Math.PI*i/N) + Math.sin(20*Math.PI*i/N); StdDraw.setXscale(0, N); StdDraw.setYscale(-2.0,2.0); for (int i = 1; i <= N; i++) StdDraw.line(i-1, a[i-1], i , a[i]); //StdDraw.save("sine.png"); } 5 20 200

Random Numbers import java.awt.Color; 500 int N = Integer.parseInt(args[0]); double scale = 1.0/N; double[] rands = new double[N]; StdDraw.setPenRadius(0.005); for (int i = 0; i < N; i++) { rands[i] = Math.random(); StdDraw.point(i*scale, rands[i]); } StdDraw.setPenRadius(0.01); StdDraw.setPenColor(new Color(255,0,0)); double sum = 0; sum += rands[i]; StdDraw.point(i*scale, sum/i); //StdDraw.save("rand.png"); 500

Sound C:523.25 B:493.88 A:440.0 G:391.59 sin(2пt × f) F:348.50

Sampling Sound Hz:440 int sps = Integer.parseInt(args[0]); double hz = Double.parseDouble(args[1]); double duration = 1.0/40.0; int N = (int) (sps * duration); double[] a = new double[N+1]; for (int i = 0; i <= N; i++) a[i] = Math.sin(2*Math.PI*i*hz/sps); StdDraw.setXscale(0, N); StdDraw.setYscale(1.1,-1.1); for (int i = 1; i <= N; i++) StdDraw.point(i , a[i]); StdDraw.setPenRadius(0.002); StdDraw.line(i-1, a[i-1], i , a[i]); 5512 11025 44100

Sampling Sound Hz:440 int sps = Integer.parseInt(args[0]); double hz = Double.parseDouble(args[1]); double duration = 1.0/40.0; int N = (int) (sps * duration); double[] a = new double[N+1]; for (int i = 0; i <= N; i++) a[i] = Math.sin(2*Math.PI*i*hz/sps); StdAudio.play(a); 5512 11025 44100

Notes = Songs A A B C A A B C A int sps = Integer.parseInt(args[0]); double[] notes = {440.0, 493.88, 523.25, 440.0}; double duration = 0.5; int N = (int) (sps * duration); double[] a = new double[2*notes.length*(N+1)]; for (int i = 0; i < 2; i++) { for (int j = 0; j < notes.length; j++) { for (int k = 0; k < N; k++) { int c = i * notes.length*N + j*N + k; a[c] = Math.sin(2*Math.PI* c*notes[j]/sps); } StdAudio.play(a); A N samples per note A B C A A B C A 4 notes

Array Input/Output //double[] notes = {440.0, 493.88, 523.25, 440.0}; int sps = Integer.parseInt(args[0]); double[] notes = {440.0, 493.88, 523.25, 440.0}; double duration = 0.5; int N = (int) (sps * duration); double[] a = new double[2*notes.length*(N+1)]; for (int i = 0; i < 2; i++) { for (int j = 0; j < notes.length; j++) { for (int k = 0; k < N; k++) { int c = i * notes.length*N + j*N + k; a[c] = Math.sin(2*Math.PI* c*notes[j]/sps); } StdAudio.play(a); //double[] notes = {440.0, 493.88, 523.25, 440.0}; double[] notes = StdArrayIO.readDouble1D();

Arrays as Arguments public static void plotPoints(double[] a) { int N = a.length; StdDraw.setXscale(0, N-1); StdDraw.setPenRadius(1.0 / (3.0 * N)); for (int i = 0; i < N; i++) { StdDraw.point(i, a[i]); } public static void plotLines(double[] a) { StdDraw.setPenRadius(); for (int i = 1; i < N; i++) { StdDraw.line(i-1, a[i-1], i, a[i]); public static void plotBars(double[] a) { StdDraw.setPenRadius(0.5 / N); StdDraw.line(i, 0, i, a[i]);