Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 10.

Slides:



Advertisements
Similar presentations
Structures Spring 2013Programming and Data Structure1.
Advertisements

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Array in C# Array in C# RIHS Arshad Khan
Arrays Chapter 7.
Precept 3 : C Arrays, Strings, and Pointers
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Computer Science 210 Computer Organization
Standard Version of Starting Out with C++, 4th Edition
Arrays (review) CSE 2011 Winter May 2018.
AKA the birth, life, and death of variables.
Chapter 9: Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Array An “average.cpp” program
Alg2_1c Extra Material for Alg2_1
CNG 140 C Programming (Lecture set 10)
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Java Review: Reference Types
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Computer Science 210 Computer Organization
CS 200 Arrays, Loops and Methods
Local Variables variables which are declared within a
Object Oriented Programming COP3330 / CGS5409
Pass by Reference, const, readonly, struct
C Arrays.
Chapter 9: Pointers.
Arrays.
CS150 Introduction to Computer Science 1
Object Oriented Programming in java
Parameter Passing in Java
Javascript Arrays.
Topics discussed in this section:
Pointers.
Arrays Chapter 7.
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
CS150 Introduction to Computer Science 1
Pointers Pointers point to memory locations
CSC 142 Arrays [Reading: chapter 12].
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
Suggested self-checks: Section 7.11 #1-11
EECE.2160 ECE Application Programming
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
CS31 Discussion 1H Fall18: week 6
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Introduction to Pointers
Presentation transcript:

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10

Note Set 10 Overview Arrays Array Examples Variable-length argument lists

Example 1 Declare an int array of length 20 and initialize each element to the square of its subscript location. int[] arr = new int [20]; for (int i = 0; i < arr.length; i++) arr[i] = i * i;

Shuffle/Randomize the elements of an array Randomize the elements of the array arr from the previous example Random r = new Random(); for (int i = 0; i < arr.length; i++){ int j = r.nextInt (arr.length); //swap element i with element j int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }

Rolling Dice Use an array and random number generator to count the outcomes of rolling a pair of dice 10,000 times. Print the frequencies. Random r = new Random(); int[] frequency = new int[13]; for (int i = 0; i < 10000; i++) { frequency[ r.nextInt(11) + 2 ] ++; System.out.println(“%s%10s\n”, “Total”, “Frequency”); for (int i = 2; i < frequency.length; i++) System.out.printf(“%4d%10d\n”, i, frequency[i]);

Variable Length Argument Lists Ever notice that you can send 1, 2, 3, or more arguments to printf? It is possible to implement a method that takes a variable number of arguments public static double average(double... numbers) { double total = 0.0; for (double d : numbers) total += d; return total/numbers.length; } use the ellipsis to indicate variable length argument numbers acts like an array and can be manipulated as such

Rules of Variable Length Argument Lists parameter with ellipsis must be at the end of the parameter list can only be one ellipsis in a parameter list public static void foo(int... vals, int x, int y);//Don’t you dare! public static void bar(int... vals, int… otherVals);//NO NO! This is sometimes called a vararg for variable-length argument list public static double average(double... numbers)

Array Issues Arrays are statically sized once they are declared, they cannot change size you can allocate a new, larger array and copy the contents of the old array over. can be computationally expensive especially for large array or if this function is performed often. If you attempt to access past the end of an array, you’ll get a ArrayOutOfBounds remember that x[i] is a reference to a memory location so if you access an invalid subscript i the you would be trying to access memory you do not have access to OR the reference is NULL and thus can’t be accessed.