Visual C# 2005 Using Arrays. Visual C# 20052 Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Microsoft Visual Basic 2010: Reloaded Fourth Edition
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Chapter 9: Advanced Array Concepts
Using Arrays and File Handling
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 8 Arrays and Strings
C# Programming: From Problem Analysis to Program Design1 7 Arrays and Collections.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Two Dimensional Arrays Found in chapter 8, Section 8.9.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 6: Using Arrays.
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Presentation transcript:

Visual C# 2005 Using Arrays

Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access array elements Use the Length property Use foreach to control array access

Visual C# Objectives (continued) Search an array to find an exact match Search an array to find a range match Use the BinarySearch() method Use the Sort() and Reverse() methods Use multidimensional arrays

Visual C# Declaring an Array and Assigning Values to Array Elements Array –List of data items that all have the same data type and the same name –Each item is distinguished from the others by an index Declaring and creating an array double[] sales; sales = new double[20]; new operator –Used to create objects You can change the size of an array

Visual C# Declaring an Array and Assigning Values to Array Elements (continued) Array element –Each object in an array Subscript (or index) –Integer contained within square brackets that indicates the position of one of an array’s elements –Array’s elements are numbered beginning with 0 “Off by one” error –Occurs when you forget that the first element in an array is element 0

Visual C# Declaring an Array and Assigning Values to Array Elements (continued)

Visual C# Declaring an Array and Assigning Values to Array Elements (continued) Assigning a value to an array element sales[0] = ; Printing an element value Console.WriteLine(sales[19]);

Visual C# Initializing an Array In C#, arrays are objects –Arrays are instances of a class named System.Array Initializing objects –Numeric fields: 0 –Character fields: ‘\0’ or null –bool fields: false Initializer list –List of values provided for an array

Visual C# Initializing an Array (continued) Initializer list examples int[] myScores = new int[5] {100, 76, 88, 100, 90}; int[] myScores = new int[] {100, 76, 88, 100, 90}; int[] myScores = {100, 76, 88, 100, 90};

Visual C# Using Subscripts to Access Array Elements The power of arrays becomes apparent when you use subscripts –Can be variables rather than constant values Using a loop to perform arithmetic on each element for (int sub = 0; sub < 5; ++sub) myScores[sub] += 3;

Visual C# Using the Length Property Length property –Member of the System.Array class –Automatically holds an array’s length Examples int[] myScores = {100, 76, 88, 100, 90}; Console.WriteLine(“Array size is {0}”, myScores.Length); for (int x = 0; x < myScores.Length; ++x) Console.WriteLine(myScores[x]);

Visual C# Using foreach to Control Array Access foreach statement –Cycles through every array element without using a subscript –Uses a temporary iteration variable Automatically holds each array value in turn Example double[] payRate = {6.00, 7.35, 8.12, 12.45, 22.22}; foreach(double money in payRate) Console.WriteLine(“{0}”, money.ToString(“C”));

Visual C# Using foreach to Control Array Access (continued) When to use –When you want to access every array element –Since the iteration variable is read-only You cannot assign a value to it

Visual C# Searching an Array for an Exact Match Searching options –Using a for loop –Using a while loop

Visual C# Using a for Loop to Search an Array Use a for statement to loop through the array –Set a Boolean variable to true when a match is found Solution is valid even with parallel arrays

Visual C# Using a for Loop to Search an Array (continued)

Visual C# Using a for Loop to Search an Array (continued)

Visual C# Using a while Loop to Search an Array

Visual C# Searching an Array for a Range Match Range match –Determines the pair of limiting values between which a value falls

Visual C# Searching an Array for a Range Match (continued)

Visual C# Using the BinarySearch() Method BinarySearch() method –Finds a requested value in a sorted array –Member of the System.Array class Do not use BinarySearch() under these circumstances –If your array items are not arranged in ascending order –If your array holds duplicate values and you want to find all of them –If you want to find a range match rather than an exact match

Visual C# Using the BinarySearch() Method (continued)

Visual C# Using the Sort() and Reverse() Methods Sort() method –Arranges array items in ascending order –Use it by passing the array name to Array.Sort() Reverse() method –Reverses the order of items in an array –Element that starts in position 0 is relocated to position Length – 1 –Use it by passing the array name to the method

Visual C# Using the Sort() and Reverse() Methods (continued)

Visual C# Using the Sort() and Reverse() Methods (continued)

Visual C# Using Multidimensional Arrays One-dimensional or single-dimensional array –Picture as a column of values –Elements can be accessed using a single subscript Multidimensional arrays –Require multiple subscripts to access the array elements –Two-dimensional arrays Have two or more columns of values for each row Also called rectangular array, matrix, or a table

Visual C# Using Multidimensional Arrays (continued)

Visual C# Using Multidimensional Arrays (continued)

Visual C# Using Multidimensional Arrays (continued)

Visual C# Using Multidimensional Arrays (continued) Three-dimensional array

Visual C# Using Multidimensional Arrays (continued) Jagged array –One-dimensional array in which each element is another one-dimensional array –Each row can be a different length

Visual C# Using Multidimensional Arrays (continued)

Visual C# You Do It Activities to explore –Creating and using an array –Initializing an array –Using a for loop with an array –Using the Length property with an array –Using the Sort() and Reverse() methods

Visual C# Summary An array is a list of data items –All of which have the same type and the same name –Items are distinguished using a subscript or index In C#, arrays are objects of a class named System.Array The power of arrays becomes apparent when you begin to use subscripts Subscript you use remains in the range of 0 through length -1

Visual C# Summary (continued) foreach statement cycles through every array element without using subscripts You can compare a variable to a list of values in an array You can create parallel arrays to more easily perform a range match The BinarySearch() method finds a requested value in a sorted array The Sort() method arranges array items in ascending order

Visual C# Summary (continued) The Reverse() method reverses the order of items in an array Multidimensional arrays require multiple subscripts to access the array elements Types of multidimensional arrays –Two-dimensional arrays (rectangular arrays) –Three-dimensional arrays –Jagged arrays