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

Similar documents
C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Chapter 6: Using Arrays

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

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)

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

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

Programming for Engineers Arrays

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

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Outline. 7.1 Introduction. 7.2 Arrays. 7.2 Arrays

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

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

Chapter 7 Array. Array. C++, How to Program

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

Instructor: Eng.Omar Al-Nahal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Arrays. Week 4. Assylbek Jumagaliyev

Arrays (Deitel chapter 7)

Computer Science & Engineering 150A Problem Solving Using Computers

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

1 Lexical Considerations

C Arrays Pearson Education, Inc. All rights reserved.

Chapter 9 Introduction to Arrays. Fundamentals of Java

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Introduction to Computer Science Midterm 3 Fall, Points

Arrays. Arrays: Declaration and Instantiation. Array: An Array of Simple Values

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Objectives of This Chapter

Lexical Considerations

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.

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Chapter 7 : Arrays (pp )

Lecture 04 FUNCTIONS AND ARRAYS

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

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

C++ PROGRAMMING SKILLS Part 4: Arrays

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

All answers will be posted on web site, and most will be reviewed in class.

Array. Prepared By - Rifat Shahriyar

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

Principles of Programming. Chapter 6: Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays

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

The Warhol Language Reference Manual

CMAT Language - Language Reference Manual COMS 4115

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Array Basics: Outline

Arrays Data structures Related data items of same type Remain same size once created Fixed-length entries

Lexical Considerations

Language Reference Manual

Arrays Introduction. Group of contiguous memory locations. Each memory location has same name Each memory location has same type

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

Array Basics: Outline

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Arrays and Applications

C++ Programming Chapter 7 Pointers

Lecture 05 POINTERS 1

Advanced Computer Programming

egrapher Language Reference Manual

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Senet. Language Reference Manual. 26 th October Lilia Nikolova Maxim Sigalov Dhruvkumar Motwani Srihari Sridhar Richard Muñoz

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

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

by Pearson Education, Inc. All Rights Reserved.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Review of the C Programming Language for Principles of Operating Systems

(Refer Slide Time 01:41 min)

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

Computer Programming: C++

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

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

Steps of initialisation:

An array can hold values of any type. The entire collection shares a single name

Flag Quiz Application

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

ARRAYS and ARRAYLISTS

Multiple-Subscripted Arrays

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Problem Solving with C++

ARG! Language Reference Manual

Bits, Bytes, and Precision

CS313D: ADVANCED PROGRAMMING LANGUAGE

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

by Pearson Education, Inc. All Rights Reserved. 2

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

INTRODUCTION 1 AND REVIEW

Transcription:

El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays An array is a group of contiguous memory locations that all have the same name and type. To refer to a particular location or element in the array, we specify the name of the array and the position number (a value that indicates a specific location within the array) of the element to which we refer.

Common Programming Error 7.1 It is important to note the difference between the seventh element of the array and array element seven. Array subscripts begin at 0, thus the seventh element of the array has a subscript of 6, while array element seven has a subscript of 7 and is actually the eighth element of the array. This confusion is a source of off-by-one errors. Declaring and Allocating Arrays Arrays occupy space in memory. The programmer specifies the type of the elements and uses operator new to allocate dynamically the number of elements required by each array. Arrays are allocated with new because arrays are objects and all objects must be created with new. We will see an exception to this rule shortly. The declaration int[] c = new int[ 12 ]; allocates 12 elements for integer array c. The preceding statement can also be performed in two steps as follows: int[] c; // declares the array c = new int[ 12 ]; // allocates the reference to the array When arrays are allocated, the elements are initialized to zero for the numeric primitivedatatype variables, to false for bool variables and to null for reference types.

Common Programming Error 7.2 Unlike in C or C++, in C# the number of elements in the array is never specified in the square brackets after the array name. The declaration int[ 12 ] c; causes a syntax error. 7.2 Memory may be reserved for several arrays with a single declaration. The following declaration reserves 100 elements for string array b and 27 elements for string array x: string[] b = new string[ 100 ], x = new string[ 27 ]; Similarly, the following declaration reserves 10 elements for array1 and 20 elements for array2 (both of type double): double[] array1 = new double[ 10 ], array2 = new double[ 20 ]; Arrays may be declared to contain most data types. In an array of value types, every element of the array contains one value of the declared type. For example, every element of an int array is an int value. In an array of reference types, every element of the array is a reference to an object of the data type of the array. For example, every element of a string array is a reference to a string. Each of these string references has the value null by default. // Computing the sum of the elements in an array. class SumArray { // main entry point for application static void Main( string[] args ) { int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int total = 0; for ( int i = 0; i < a.length; i++ ) total += a[ i ]; MessageBox.Show( "Total of array elements: " + total, "Sum the elements of an array", MessageBoxButtons.OK, MessageBoxIcon.Information ); } // end Main } // end class SumArray Passing Arrays by Value and by Reference In C#, a variable that stores an object, such as an array, does not actually store the object itself. Instead, such a variable stores a reference to the object (i.e., the location in the computer s memory where the object itself is stored). The distinction between reference variables and primitive data type variables raises some subtle issues that programmers must understand to create secure, stable programs.

When a program passes an argument to a method, the called method receives a copy of that argument s value. Changes to the local copy do not affect the original variable that the program passed to the method. If the argument is of a reference type, the method makes a local copy of the reference itself, not a copy of the actual object to which the reference refers. The local copy of the reference also refers to the original object in memory. Thus, reference types are always passed by reference, which means that changes to those objects in called methods affect the original objects in memory. Multiple-subscripted arrays A double-subscripted array b with two rows and two columns could be declared and initialized with int[,] b = new int[ 2, 2 ]; b[ 0, 0 ] = 1; b[ 0, 1 ] = 2; b[ 1, 0 ] = 3; b[ 1, 1 ] = 4; or this can be written on one line using an initializer list as shown below: int[,] b = { { 1, 2 }, { 3, 4 } }; SUMMARY An array is a group of contiguous memory locations that all have the same name and type.

To refer to a particular location or element in an array, specify the name of the array and the position number of the element within the array. The first element in every array is the zeroth element (i.e., element 0). The position number in square brackets is more formally called a subscript (or an index). This number must be an integer or an integer expression. To reference the ith element of a single-dimensional array, use i-1 as the index. The brackets that enclose the subscript of an array are operators that have the same level of precedence as parentheses. When arrays are allocated, the elements are initialized to zero for the numeric primitive-data-type variables, to false for bool variables or to null for reference types. Arrays may be declared to contain most data types. In an array of primitive data types, every element of the array contains one value of the declared data type of the array. In an array of a reference type, every element of the array is a reference to an object of the data type of the array. For example, every element of a string array is a reference to a string and that reference has the value null by default. The elements of single-dimensional and rectangular arrays can be allocated and initialized in the array declaration by following the declaration with an equal sign and a comma-separated initialize list enclosed in braces ({ and }). A const variable must be declared and initialized in the same statement. Constants also are called named constants. They often are used to make a program more readable. Unlike its predecessors C and C++,.NET-compliant languages provide mechanisms to prevent accessing elements outside the bounds of the array. When a reference is made to a nonexistent element of an array, an IndexOutOfRangeException occurs. To pass an array argument to a method, specify the name of the array without any brackets. Although entire arrays are passed by reference, individual array elements of primitive data types are passed by value, as are simple variables. To pass an array element to a method, use the subscripted name of the array element as an argument in the method call. Sorting data (i.e., placing the data into a particular order, such as ascending or descending) is one of the most important computing applications. The chief virtue of the bubble sort is that it is easy to program. However, the bubble sort runs slowly,

which becomes apparent when sorting large arrays. The linear search method works well for small or unsorted arrays. However, for large arrays, linear searching is inefficient. After each comparison, the binary search algorithm eliminates from consideration half the elements in the array being searched. The algorithm locates the middle array element and compares it to the search key. If they are equal, the search key has been found, and the subscript of that element is returned. Otherwise, the problem is reduced to searching half the array. If the search key is less than the middle array element, the first half of the array is searched; otherwise, the second half of the array is searched. The search continues until the search key is equal to the middle element of a subarray, or until the subarray consists of one element that is not equal to the search key (i.e., the search key is not found). The maximum number of comparisons needed for the binary search of any sorted array is the exponent of the first power of 2 that is greater than the number of elements in the array. There are two types of multiple-subscripted arrays rectangular and jagged. In general, an array with m rows and n columns is referred to as an m-by-n array. Multiple-subscripted arrays can be initialized in declarations, as can singlesubscripted arrays. The compiler determines the number of columns in each row by counting the number of initialize values in the sub-initializer list for that row. Jagged arrays are maintained as arrays of arrays. Unlike rectangular arrays, rows in jagged arrays can be of different lengths. Many common array manipulations use for repetition structures. When used with one-dimensional arrays, foreach behaves like a for structure that iterates through the range of indices from 0 to the array s Length. For rectangular arrays, the repetition of the foreach structure begins with the element whose indices are all zero, then iterates through all possible combinations of indices, incrementing the rightmost index first. When the rightmost index reaches its upper bound, it is reset to zero, and the index to the left of it is incremented by 1.