1D Arrays and Lots of Brackets

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
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.
Building Java Programs Chapter 7.5
1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
COMP More About Arrays Yi Hong June 05, 2015.
int [] scores = new int [10];
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];
Multidimensional Arrays Computer and Programming.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Arrays 3/4 By Pius Nyaanga.
Chapter 10 Arrays.
Arrays 2/4 By Pius Nyaanga.
Foundations of Programming: Arrays
Inheritance & Polymorphism
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Counted Loops.
Objects First with Java CITS1001
Lecture 9 Arrays Richard Gesick.
Chapter 10 Arrays.
Arrays An Array is an ordered collection of variables
Chapter 6 Arrays.
Nested Loop Review and Two-Dimensional Arrays
Variables, Loops, Decision Statements, etc
Chapter 8 Slides from GaddisText
Review of Classes and Arrays
Defining methods and more arrays
ARRAYS 1 GCSE COMPUTER SCIENCE.
برنامه نویسی پیشرفته اصول جاوا.
CS2011 Introduction to Programming I Arrays (I)
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
int [] scores = new int [10];
Lecture 4 2d Arrays CSE /26/2018.
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Methods and Data Passing
Review of Classes and Arrays
CSE Module 1 A Programming Primer
Lecture 14 2D Arrays Richard Gesick.
Module 8 – Searching & Sorting Algorithms
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
C++ Array 1.
1D Arrays and Lots of Brackets
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Module 8 – Searching & Sorting Algorithms
Review of Classes and Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Introduction to Arrays Reading for this Lecture:
First Semester Review.
Module 8 – Searching & Sorting Algorithms
1D Arrays and Lots of Brackets
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
MIS 222 – Lecture 12 10/9/2003.
Presentation transcript:

1D Arrays and Lots of Brackets Ps Module 7 – Part I 1D Arrays and Lots of Brackets 4/3/2019 CSE 1321 Module 7

2. Creating and Initializing Arrays You must choose: 1) Create an empty arrays and assign initial values with a loop later on: CREATE myArray[5] OR 2) Create and initialize the array in one line. This is helpful when we already know those value (e.g. days of the week, etc.). 4/3/2019 CSE 1321

Array Creation Example 1) Create an empty array. By default, 0s are stored in the array. int[] myArray = new int[5]; 2) Creates and initializes in one line. The stored values are show in { }; int[] myArray = {10, 20, 30, 40, 50}; 4/3/2019 CSE 1321

Horrible, but works… // creates empty array with 5 slots, 0-4 CREATE myArray [5] //Assigns literal values to each index in the array. myArray[0] ← 10 myArray[1] ← 20 myArray[2] ← 30 myArray[3] ← 40 myArray[4] ← 50 Ps 4/3/2019 CSE 1321

Accessing and Modifying //Accessing int secondNumber = myArray[1]; PRINT(secondNumber); //Modifying myArray[3] = 42; PRINT(myArray[3]); 4/3/2019 CSE 1321 Module 4

Ps Traversing the Array You will use a loop to visit every cell of the array Problem: create an array of 5 bytes and fill each slot with the number 42 Solution: CREATE myArray [5] FOR i ← 0 to 4 myArray[i] ← 42 END FOR Ps 4/3/2019 CSE 1321

Traversing an Array Use a FOR, WHILE or DO-WHILE starting at index 0 and going thru .Length-1 for (int i=0; i < data_storage.Length; i++) { // do something } OR use a foreach loop foreach (Trophy t in data_storage) { // do something as long as it is not assignment } // We told you it was exciting 4/3/2019 CSE 1321

Ps Another Trace CREATE smallestSoFar smallestSoFar ← randomArray[0] FOR counter ← 1 to 4 IF (smallestSoFar > randomArray[counter]) THEN smallestSoFar ← randomArray[counter] END IF END FOR // Done counter 5 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

Finding the Minimum using a Method private static int FindMin (int[] A) { int temp = A[0]; for (int i = 1; i < A.Length(); i++) { if (temp > A[i]) { temp = A[i];} } return temp; Parameters match in position and type, so the name doesn't matter. Arrays are a reference type in C#. 4/3/2019 CSE 1321

Finding the sum or average using a method METHOD FINDAVERAGE ( parameter: nums[]) BEGIN sum ← 0 FOR i ← 0 to nums.length - 1 // MOST IMPORTANT LINE IS HERE sum = sum + nums[i] ENDFOR average = sum / nums.length return average END FINDAVERAGE Ps 4/3/2019 CSE 1321

Finding a sum and or average using a method private static float FindAverage (int [] B) { int sum = 0; for (int i = 0; i < B.Length; i++) sum += B[i]; } return (float)sum / B.Length; 4/3/2019 CSE 1321

Ps Defining a 2D array CREATE array nums [numRows][numColumns] 4/3/2019 CSE 1321

C# - Define a 2D Array <type>[ , ]<name> = new <type>[<row_size>,<column_size>]; For example: int[ , ] grid = new int [10,20]; 4/3/2019 CSE 1321

Working with 2D arrays Usually involves nested loops, as shown below. Problem Statement: Create an array of 4 rows and 5 columns. Populate the array with the numbers 1-20. --------------------------------------------------------------------- Create array grid[4][5] count ← 1 FOR each element in a row FOR each element in a column     grid[row][col] = count count ← count + 1 END INNER FOR END FOR 4/3/2019 CSE 1321

Working with 2D arrays using System; class MainClass { public static void Main (string[] args) { int count = 1; //declare a 2D array with 4 rows and 5 columns int[,] grid = new int[4, 5]; //getting the rows in the array for (int row = 0; row < 4; row++) { //populate the values in the array (row) we're currently on from the outer loop for (int column = 0; column < 5; column++) grid[row, column] = count; count++; } //for each array in the grid (represented by rows) foreach(int number in grid) //for each element in the array (row) we're currently on Console.Write(number + ","); if(number % 5 == 0) { Console.WriteLine(" "); } 4/3/2019 CSE 1321

Working with Array Lists using System; using System.Collections; class MainClass { public static void Main (string[] args) { //create list ArrayList colors = new ArrayList(); //add colors colors.Add("purple"); colors.Add("pink"); colors.Add("green"); //contains returns a boolean if list contains argument Console.WriteLine("Does the arraylist contain the color pink?"); if (colors.Contains("pink")) { Console.WriteLine("Yes"); Console.WriteLine("\n"); } else { Console.WriteLine("No"); } 4/3/2019 CSE 1321

C# - Working with Array Lists CONTINUED FROM PREVIOUS SLIDE //indexOf returns the position of the argument Console.WriteLine("Which position is pink in?"); Console.WriteLine(colors.IndexOf("pink")); Console.WriteLine("\n"); //remove removes the argument from the list colors.Remove("purple"); //can reference by position in the array Console.WriteLine("The item in index 1 of the list is:"); Console.WriteLine(colors[1]); //items are stored as objects and can be referenced as such Console.WriteLine("The Colors Arraylist:"); foreach (Object o in colors) { Console.WriteLine(o); } 4/3/2019 CSE 1321