CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Arrays.
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 Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
The University of Texas – Pan American
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
Chapter 8 Arrays and Strings
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Arrays.
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.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
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.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
A DVANCED P ROGRAMMING C HAPTER 8: A RRAYS Dr Shahriar Bijani Spring 2016.
7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are.
Array in C# Array in C# RIHS Arshad Khan
Arrays Chapter 7.
Chapter 6: Using Arrays.
Computer Programming BCT 1113
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
The University of Texas – Pan American
Advanced Programming Chapter 8: Arrays
Java How to Program, Late Objects Version, 10/e
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Chapter 5 Arrays Introducing Arrays
Introduction To Programming Information Technology , 1’st Semester
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Arrays Chapter 7.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX

Objectives In this chapter, you will: –Learn how arrays are used to store elements –Get familiar with the declaration, initialization, and reference to elements of the array –Know how to pass arrays to methods –Learn how to use multidimensional arrays –Learn how to use foreach statement to iterate through elements in the array 2

Arrays An array is a group of variables (called elements) containing values that all have the same data type For most languages, the number of elements are static and defined at declaration Single dim, two dim and multiple dim 3

Example of an Array The first element is the zeroth element The highest position number is array’s upper bound –Array C on RHS: 11 Access array elements –Use index (or subscript) –E.g., C[8] C[0]-45 C[1]6 C[2]0 C[3]72 C[4]34 C[5]39 C[6]98 C[7]-1345 C[8]939 C[9]10 C[10]40 C[11]33 4

Arrays (cont'd) Data structure consisting of related data items of the same type One variable name, multiple elements Arrays are passed as reference type, not value To access an element we use name of the array and index in square bracket 5

Declaring and Creating Arrays Declaration –int[] array = new int[12]; (creates indices 0 to 11) Or do it this way: –int[] array; –array = new int[12]; Or this way: –int[] array; –const int size = 12; –array = new int[size]; Since array is an object, we can resize the array as follows: –Array.Resize(ref array, 10); 6

Arrays of Other Data Types string [] str = new string [100]; char [] ch = new char [ 50]; … 7

Initializing an Array // creates 5-element array Initializing arrays while declaring –int [] arr = new int [] {10, 20, 30, 40, 50}; –int [] array = {10,20,30,40,50}; 8

Sum in an Array int[] array = {10,20,30,40,50}; // creates 5-element array Finding the sum double sum=0; double average; for (int counter =0; counter < array.Length; counter++) sum = sum+array[counter]; size of the array 9

Average in an Array int[] array = {10,20,30,40,50}; // creates 5-element array Finding the sum double sum=0; double average; for (int counter =0; counter < array.Length; counter++) sum = sum+array[counter]; average = sum/array.Length; 10

Standard Deviation Explained In your assignment, assuming array has length n, the standard deviation  is given by: 11

foreach Statements In addition to for loops, you can also work with: Syntax –foreach (type identifier in arrayName) REMEMBER: YOU HAVE TO START WITH ELEMENT 0! 12

Syntax of foreach Statements Example of computing summation of elements in an array called scores: foreach (int score in scores) { sum = sum + score; } 13

Passing Arrays by Value Declaration –int[] firstArray={1, 2, 3}; –int[] firstArrayCopy = firstArray; –public static void FirstDouble (int [] array) { for (int i = 0; i<array.Length; i++) array[i]*=2; array = new int [] {11, 12, 13}; } Call –FirstDouble(firstArray); 14

Passing Arrays by Reference Declaration –int[] secondArray={1, 2, 3}; –int[] secondArrayCopy = firstArray; –public static void SecondDouble (ref int [] array) { for (int i = 0; i<array.Length; i++) array[i]*=2; array = new int [] {11, 12, 13}; } Call –SecondDouble(ref secondArray); 15

Rectangular Arrays Rectangular array is a two dimensional array Similar to a table with rows and columns –arrayName[row, column] int [, ] a = new int [3, 4]; 16 a[0, 0]a[0, 1]a[0, 2]a[0, 3] a[1, 0]a[1, 1]a[1, 2]a[1, 3] a[2, 0]a[2, 1]a[2, 2]a[2, 3] Column 0Column 1Column 2Column 3 Row 0 Row 1 Row 2

Nested Array Initializer int [, ] arr = {{1, 2}, {3, 4}};

Example of Rectangular Arrays public int [,] hands = new int[5, 14]; for (i=1; i<=52; i++) { hands[player, hand] = sdeck[i]; } 18

Jagged Arrays Tables that have rows of unequal columns –Somewhat like an array of arrays –Fixed students and exams not everyone taking it.. int [] [] scores = new int [students][]; scores[0] = new int [5]; // student 0 takes 5 exams scores[1] = new int [4]; // student 1 takes 4 exams 19

Sort an Array C[0]-45 C[1]6 C[2]0 C[3]72 C[4]34 C[5]39 C[6]98 C[7]-1345 C[8]939 C[9]10 C[10]40 C[11]33 20 C[0]-1345 C[1]-45 C[2]0 C[3]6 C[4]10 C[5]33 C[6]34 C[7]39 C[8]40 C[9]72 C[10]98 C[11]939

Sort Method of Array You need to write your own sort methods later own For now you can use: –Array.Sort(scores); The identifier, scores, is the array you created 21

Search Method Searching: to determine whether a value (called search key) is in the array –Linear search Compare each element of the array with the search key For either sorted or unsorted array Efficient for small array –Binary search For sorted array 22

Linear Search 23 C[0]-45 C[1]6 C[2]0 C[3]72 C[4]34 C[5]39 C[6]98 C[7]-1345 C[8]939 C[9]10 C[10]40 C[11]33 search key

Binary Search You need to write your own binary search method later But now you can use: –int index = Array.BinarySearch(array, value); –If index < 0, then value is not found in the array –Otherwise, index stores the index of value in the array 24

Binary Search 25 C[0]-1345 C[1]-45 C[2]0 C[3]6 C[4]10 C[5]33 C[6]34 C[7]39 C[8]40 C[9]72 C[10]98 C[11]939 search key: 10 first last middle

Binary Search 26 C[0]-1345 C[1]-45 C[2]0 C[3]6 C[4]10 C[5]33 C[6]34 C[7]39 C[8]40 C[9]72 C[10]98 C[11]939 search key: 10 first last middle

Binary Search 27 C[0]-1345 C[1]-45 C[2]0 C[3]6 C[4]10 C[5]33 C[6]34 C[7]39 C[8]40 C[9]72 C[10]98 C[11]939 search key: 10 first last middle

Binary Search 28 C[0]-1345 C[1]-45 C[2]0 C[3]6 C[4]10 C[5]33 C[6]34 C[7]39 C[8]40 C[9]72 C[10]98 C[11]939 search key: 10 first last middle

Copying an Array You can copy one element at a time or the entire array –Array.Copy(fromArray, fromIndex, toArray, to Index, length); Note: the following statement does not copy, but creates another reference to the array –string[] copyDeck = oDeck; 29

30