Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
Advertisements

Microsoft Visual Basic 2010: Reloaded Fourth Edition
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Chapter 8 Arrays and Collections Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
JavaScript, Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
© 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.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
IE 212: Computational Methods for Industrial Engineering
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
Array Processing.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
8-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Arrays Chapter 8.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 8 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Chapter 8 Arrays Programming In Visual Basic.NET.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 8 Arrays Brief description on how to navigate within this presentation (PPT) The first time a Key Term from the chapter is used in the PPT, it.
Chapter 6: Using Arrays.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Brief description on how to navigate within this presentation (ppt)
Arrays.
Programming In Visual Basic .NET
Presentation transcript:

Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

8- 2 Objectives Establish an array and refer to individual elements in the array with subscripts Use a foreach loop to traverse the elements of an array Create a structure for multiple fields of related data Accumulate totals using arrays

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Objectives cont. Distinguish between direct access and indirect access of a table Write a table lookup for matching an array element Combine the advantage of list box controls with arrays Store and look up data in multidimensional arrays

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Single-Dimension Arrays An array is a list or series of values, similar to a list box without the box Create an array to keep a series of variables for later processing Each individual variable in an array is referenced by the same name Arrays also called tables or subscripted variables Each individual variable is an element of the array Subscript (or index) inside parentheses is the position of the element within the array

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Subscripts Subscripts may be constants, variables, or numeric expressions Subscripts must be integers Specify the number of elements in the array in the array’s declaration statement Array subscripts are zero based You declare a data type for the array and all array elements must be the same data type A subscript must reference a valid element of the array Exception will be thrown if a subscript is out of range

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Declaration Statement for Arrays General Form Datatype[] ArrayName = new DataType[NumberofElements]; Datatype[] ArrayName = new DataType[] {InitialValueList}; Datatype[] ArrayName = {InitialValueList}; Examples string[] strName = new string[25]; decimal[] decBalance = new decimal[10]; string[] strProduct = new string[99]; int[] intValue = new int[] {1,5,12,18,20}; string[] strName = {“Sean”, “Sam”, “Sally”, “Sara”};

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved foreach Statement foreach is a looping construct that does not require manipulation of the array subscripts General Form foreach (DataType ElementName in ArrayName) { //Statement(s) in loop } C# automatically references each array element, assigns its value to ElementName, and makes one pass through the loop The foreach loop will execute if the array has at least one element

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved foreach Examples foreach (string strOneName in strName) { Console.WriteLine(strOneName); //Write one element of the array } int intSum; foreach (int intOneTotal in intTotal) { intSum += intOneTotal; //Add each element of the array to intSum }

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Structures Combine multiple fields of related data to create a new structure Defining a structure is similar to defining a new data type Struct declaration cannot go inside a method By default, a structure is public If an array is included inside a structure, you cannot specify the number of elements

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Structures cont. General Form [public | private] struct NameOfStruct { public Datatype FirstField; public Datatype SecondField;... } Example public struct Product { public string strDescription; public string strID; public int intQuantity; public decimal decPrice; }

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Structures cont. Once a structure is created, declare variables of the structure Each field of data in variable declared as a structure is called an element of the structure C# does not allow declaration of the number of elements in an array in the struct declaration Using a value as an index to an array is called a direct reference View contents of array elements in break time using the Autos window by clicking on the plus sign at left of the array name

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Table Lookup Determine which array element to update or access using a table lookup Use a while loop for a table lookup Validate input before performing table lookup Compare input to each element in array to find a match It is not necessary to arrange fields being searched in any particular sequence

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Using List Boxes with Arrays It is a good idea to use a list box for input of information to lookup in array Use the SelectedIndex property to determine the array subscript –SelectedIndex holds the position or index of the selected item from the list

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Multidimensional Arrays Two-dimensional arrays have rows and columns Array declaration specifies number of rows and columns in the array Row is horizontal and column is vertical Must always use two subscripts to refer to individual elements of table –Row is first subscript –Column is second subscript

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Multidimensional Arrays cont. General Form DataType[,] ArrayName = new Datatype[NumberOfElements,NumberOfElements]; DataType[,] ArrayName = new DataType[, ] = {ListOfValues}; Cannot specify the number of elements within parentheses and specify the initial values Must use a comma to specify two dimensions to the array Examples: string[,] strName = new string[3,4]; string[,] strName = new string[, ] = {{“James”, “Mary”, “Sammie”, “Sean”}, {“Tom”, “Lee”, “Leon”, “Larry”}, {“Maria”, “Margaret”, “Jill”, “John”}};

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Initialize Two-Dimensional Arrays Numeric array elements are initialized to 0 and strings are initialized to empty strings Use nested for loops to initialize array elements int intRow, intColumn; for (intRow = 0; intRow < 3; intRow++) { for (intColumn = 0; intColumn < 4; intColumn++) { strName[intRow, intColumn] = “”; }

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Printing a Two-Dimensional Table Use a foreach loop to print contents of a two-dimensional table –Prints one array element per line Use a for loop and specify x and x coordinates to print multiple elements per line

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summing a Two-Dimensional Table There are several ways to sum a table –Sum the columns –Sum the rows –Sum the figures in both directions and double- check the totals To sum an array in both directions, create two one-dimensional arrays to store the total fields

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Lookup Operation for Two-Dimensional Tables Use direct reference and table lookup as in single- dimensional arrays Limitations –Row and column subscripts must be available to use direct reference –Table lookup is the most common lookup technique Lookup processes may require additional one- dimensional arrays or lists –Use list boxes or text boxes

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Your Hands-On Programming Example Create a project for R ‘n R – Reading ‘n Refreshment that determines the price per pound for bulk coffee sales. The coffees are divided into categories: regular, decaf, and special blend. The prices are set by the quarter pound, half pound, and full pound. Use a Find Price button to search for the appropriate price based on the selections.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary A series of variables with the same name and data type is called an array. The individual values are referred to as elements, and each element is accessed by its subscript, which is a position number. Array subscripts or indexes are zero based; they must be integers in the range of the array elements. C# rounds noninteger values. You can assign initial values in the declaration or specify the number of elements.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary cont. A special form of the for loop called foreach is used to work with arrays. The foreach eliminates the need to manipulate the subscripts of the array. You can declare a structure to combine related fields then declare variables and arrays of the structure. The struct statement should appear at the class level. Array elements are used like any other variables; they can be used to accumulate a series of totals or to store values for a lookup procedure.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary cont. The information in arrays may be accessed directly by subscript, or a table lookup is used to determine the correct table position. You can use the SelectedIndex property of a list box as a subscript of an array. Arrays may be multidimensional. A two- dimensional table contains rows and columns. Accessing a multidimensional array frequently requires the use of nested loops.