Chapter 6: Using Arrays

Similar documents
High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Chapter 9 Introduction to Arrays. Fundamentals of Java

Arrays and Collections. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Advanced Computer Programming

Array Structure. In C#, arrays can be declared as fixed length or dynamic. A fixed length array can store a predefined number of items.

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Object Oriented Programming. Java-Lecture 6 - Arrays

Arrays, Strings and Collections

Principles of Programming. Chapter 6: Arrays

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

How to declare an array in C?

Instructor: Eng.Omar Al-Nahal

The Warhol Language Reference Manual

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

Computer Programming: C++

Java Arrays. What could go wrong here? Motivation. Array Definition 9/28/18. Mohammad Ghafari

Chapter Nine Arrays. Sixth Edition

Arrays and Strings. Arash Rafiey. September 12, 2017

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Overview. Iteration 4 kinds of loops. Infinite Loops. for while do while foreach

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

4/29/03 Doc 25 C# Arrays, Indexers & Exceptions slide # 1

Visual C# 2012 How to Program by Pe ars on Ed uc ati on, Inc. All Ri ght s Re ser ve d.

Arrays. Eng. Mohammed Abdualal

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Programming for Engineers Arrays

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Arrays OBJECTIVES. In this chapter you will learn:

CS 230 Programming Languages

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Last Class. More on loops break continue A bit on arrays

Steps of initialisation:

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

CS 231 Data Structures and Algorithms Fall Arrays Lecture 07 - September 19, Prof. Zadia Codabux

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Lecture 13: Two- Dimensional Arrays

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 4: Making Decisions

Euclid s algorithm, 133

Array. Prepared By - Rifat Shahriyar

CS159. Nathan Sprague. September 11, 2015

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

BITG 1113: Array (Part 1) LECTURE 8

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 6. Data Types

CMSC 202. Pointers Dynamic Memory Allocation

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Arrays (Deitel chapter 7)

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

C/C++ Programming Lecture 18 Name:

Operators. Java operators are classified into three categories:

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Arrays. Week 4. Assylbek Jumagaliyev

Review of Important Topics in CS1600. Functions Arrays C-strings

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Example: Computing prime numbers

Subject: Computer Science

Long Questions. 7. How does union help in storing the values? How it differs from structure?

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Solutions to Chapter 8

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Arrays and ArrayLists. David Greenstein Monta Vista High School

BASIC ELEMENTS OF A COMPUTER PROGRAM

List of Practical for Class XII Computer Science

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

6: Arrays and Collections

PieNum Language Reference Manual

UNIT 1. Introduction to Microsoft.NET framework and Basics of VB.Net

Arrays and Applications

Collections. Collections Collection types Collection wrappers Composite classes revisited Collection classes Hashtables Enumerations

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

C# and Java. C# and Java are both modern object-oriented languages

ONE DIMENSIONAL ARRAYS

6: Arrays and Collections

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Flag Quiz Application

Chapter 6 Arrays, Part D 2d Arrays and the Arrays Class

Exercises Software Development I. 06 Arrays. Declaration, Initialization, Usage // Multi-dimensional Arrays. November 14, 2012

Chapter 6 Pointers and Arrays

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.

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables


Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Object-Oriented Programming in C# (VS 2015)

COP 3014 Honors: Spring 2017 Homework 5

CS 112 Midterm Exam Fall 2016

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

CS112 Lecture: Arrays and Collections

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Transcription:

Chapter 6: Using Arrays

Declaring an Array and Assigning Values to Array Array Elements A 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 2

Declaring an Array and Assigning Values to Array element Each object in an array Subscript (or index) Array Elements (cont d.) An integer contained within square brackets that indicates the position of one of an array s elements An 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 3

Declaring an Array and Assigning Values to Array Elements (cont d.) 4

Declaring an Array and Assigning Values to Array Elements (cont d.) Assigning a value to an array element sales[0] = 2100.00; Printing an element value Console.WriteLine(sales[19]); 5

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

Initializing an Array (cont d.) 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}; 7

Accessing Array Elements The power of arrays becomes apparent when you use subscripts Using a loop to perform arithmetic on each element for(int sub = 0; sub < 5; ++sub) myscores[sub] += 3; 8

Using the Length Property Length property A 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]); 9

Using foreach 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")); 10

Using foreach (cont d.) Used when you want to access every array element Since the iteration variable is read-only, you cannot assign a value to it 11

Searching an Array Using a Loop Searching options Using a for loop Using a while loop 12

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 The solution is valid even with parallel arrays 13

14

Using a for Loop to Search an Array (cont d.) 15

Using a while Loop to Search an Array Use a while loop to search for a match 16

17

Using a while Loop to Search an Array (cont d.) 18

Using the BinarySearch(), Sort(), and Reverse() Methods The System.Array class contains a variety of useful, built-in methods that can search, sort, and manipulate array elements 19

Using the BinarySearch() Method BinarySearch() method Finds a requested value in a sorted array A 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 20

Using the BinarySearch() Method (cont d.) 21

Using the BinarySearch() Method (cont d.) 22

Using the Sort() Method Sort() method Arranges array items in ascending order Use it by passing the array name to Array.Sort() 23

Using the Sort() Method (cont d.) 24

Using the Sort() Method (cont d.) 25

Using the Reverse() Method Reverse() method Reverses the order of items in an array An element that starts in position 0 is relocated to position Length 1 Use it by passing the array name to the method 26

Using the Reverse() Method (cont d.) 27

Using the Reverse() Method (cont d.) 28

Using Multidimensional Arrays One-dimensional or single-dimensional array Picture it as a column of values Elements can be accessed using a single subscript Multidimensional array Requires multiple subscripts to access the array elements Two-dimensional array Has two or more columns of values for each row Also called a rectangular array, a matrix, or a table 29

Using Multidimensional Arrays (cont d.) 30

Using Multidimensional Arrays (cont d.) 31

Using Multidimensional Arrays (cont d.) 32

Using Multidimensional Arrays (cont d.) 33

Using Multidimensional Arrays (cont d.) Three-dimensional array 34

Using Jagged Arrays Jagged array A one-dimensional array in which each element is another one-dimensional array Each row can be a different length 35

Using Jagged Arrays (cont d.) 36

Array Issues in GUI Programs If array values change based on user input, the array must be stored outside any method that reacts to the user s event 37

Array Issues in GUI Programs (cont d.) 38

Array Issues in GUI Programs (cont d.) 39