Arrays An Array is an ordered collection of variables

Slides:



Advertisements
Similar presentations
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Advertisements

CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays. Arrays are objects that help us organize large amounts of information.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
CSC 211 Java I for loops and arrays.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
Chapter 8 Multidimensional Arrays
Two-Dimensional Arrays
Chapter 10 Arrays.
Chapter 7 Multidimensional Arrays
Computer Programming BCT 1113
Chapter 8 Multidimensional Arrays
Chapter 8 Arrays Objectives
Chapter 7 Arrays.
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
JavaScript: Functions.
Chapter 6 Arrays.
Chapter 8 Multidimensional Arrays
Chapter 10 Arrays.
Example: Finding the Mode
Chapter 8 Multidimensional Arrays
Chapter 8 Multi-Dimensional Arrays
Chapter 8 Arrays Objectives
Chapter 7 Multidimensional Arrays
Chapter 5 Arrays Introducing Arrays
int [] scores = new int [10];
Java Programming Arrays
Chapter 8 Multidimensional Arrays
Introduction To Programming Information Technology , 1’st Semester
Chapter 8 Multidimensional Arrays
JavaScript Arrays.
Arrays .
Multidimensional Arrays
CS2011 Introduction to Programming I Arrays (I)
Data Types and Data Structures
Chapter 8 Multidimensional Arrays
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
int [] scores = new int [10];
Multidimensional Arrays
Arrays Week 2.
Building Java Programs
Chapter 8 Multidimensional Arrays
Chapter 8 Arrays Objectives
Arrays in Java.
Chapter 7 Multidimensional Arrays
Arrays 2-May-19.
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
1D Arrays and Lots of Brackets
Arrays.
Presentation transcript:

Arrays An Array is an ordered collection of variables One of our first “data structures” Gives some relationships between different data Usage: “store the grades of everybody in this class” Without arrays: one variable for each student  With arrays: one variable for the whole class! It just stores one entry for each student

Arrays Arrays have a “Base Type” E.g. “An array of ints” E.g. “An array of doubles” E.g. ”An array of Strings” Can be any built-in Java type, or a user-defined one (later) Each object inside the array is called an element

Arrays Declared by adding [ ] to the type: General syntax: double[] testScores; // Only declares, does not create space testScores = new double[100]; // Allocates space in memeory // Now we can start using it! General syntax: ⟨array-variable ⟩ = new ⟨base-type ⟩[⟨array-length ⟩];

Arrays An array in memory is like a “list” of particular length int[] a = new int[5]; // Creates 5 elements in memory A A[0] A[1] A[2] A[3] A[4] The whole array is called A but individual elements are referenced with the [ ] operator The array has an attribute called “length” which returns the number of elements: A.length == 5 // will be true

Arrays To access individual elements of an array variable, use the [ ] operator: testScores[0] = 100.0; // Counting starts at 0 testScores[7] = 95.5; // Assigns 95 to the 8’th element We can use an int variable to index: int i = 10; testScores[i] = 80;

Default values Every variable gets set to a default when it is created However, usually shouldn’t be relied on

Arrays and For-Loops We can use a for-loop to very easily traverse an array: int[] myArray = new int[100]; // Assigns each element random between 1 and 10 for( int i = 0; i < myArray.length; i++ ){ myArray[i] = (int) (10.0*Math.random() + 1); }

Arrays and For-loops Calculate the average of our random array: double avg; double sum = 0; for( int = 0; i < myArray.length; i++ ){ sum = sum + myArray[i]; // OR sum += myArray[i]; } avg = sum / myArray.length;

Largest Array element Use linear search: check one-by-one, remember the biggest so far int biggest = myArray[0]; for( int i = 0; i<myArray.length; i++ ){ if ( myArray[i] > biggest ){ biggest = myArray[i]; } System.out.println(”The largest element is: “ + biggest);

Array Access Using a for-loop is an example of sequential access Access elements one-by-one Another common way to access is by random access Can’t predict which will be used next Has hardware and memory implications

Example: Birthday paradox How many people do you need to select at random until two have the same birthday? Internal bias: we only typically consider our own birthday, but there are hundreds of comparisons to be checked in a room of ~20 people! In pure statistics: with 23 randomly selected birthdays, there is a 50% chance that two of them are the same! Program goal: simulate drawing random birthdays until you get a match. Count the number of draws until a collision happens.

Multidimensional Arrays Can be used to hold many “dimensions” of data E.g. Can think of an “array of arrays” Acts like a matrix or a table: int[][] myMatrix = new int[5][10]; // Declares 2-d, 5-by-10 array // with 50 int elements myMatrix[3][2] = 100; // Access like a matrix, each dimension at a time

Multidimensional Arrays Example task: create a 2-d array to hold test scores of multiple students at once Print the average of each student Method: use a matrix where each student is a row and each column is an exam score

Example: Input Reverse Algorithm Ask the user for up to 100 positive integers, enter a 0 to stop Store them each in an array Count how many the user entered Print the integers back to the user, in reverse order of how they were entered

Example: Finding the Mode Algorithm: Generate an array of 100 random numbers from 1 to 10 Use a length 10 array to count how many times each number appears in the array Report the one that appears the most, or multiple if there are ties