Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Arrays and Strings.
Introduction to arrays
Programming and Data Structure
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Programming with Collections Collections in Java Using Arrays Week 9.
Chapter 10 Additional Features of Arrays Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Chapter 2 Basic Elements of Fortan
Chapter 11 Additional Intrinsic Data Types Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
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 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Multi-Dimensional Arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables.
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering Computing I Chapter 5 Pointers and Arrays.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
Chapter 9: Sorting and Searching Arrays
Introduction to Algorithms
An Introduction to Programming with C++ Sixth Edition
Arrays 2.
Programming For Nuclear Engineers Lecture 6 Arrays
Array processing and Matrix manipulation
Arrays, For loop While loop Do while loop
7 Arrays.
Starting Out with Programming Logic & Design
Introduction to Algorithms
Array processing and Matrix manipulation
Introduction to Pointers
Presentation transcript:

Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 ( ) Fax: +90 ( )

Introduction to Arrays - Part I Introduction to Arrays An array is a group of variables or constants, all of the same type, that are referred to by a single name. The values in the group occupy consecutive locations in the computer's memory. An individual value within the array is called an array element; it is identified by the name of the array together with a subscript pointing to the particular location within the array. For example, the first variable shown in Figure. The subscript of an array is of type INTEGER. Either constants or variables may be used for array subscripts. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 2

Introduction to Arrays - Part I As we shall see, arrays can be extremely powerful tools. They permit us to apply the same algorithm over and over again to many different data items with a simple DO loop. For example, suppose that we need to take the square root of 100 different real numbers. If the numbers are stored as elements of an array a consisting of 100 real values, then the code © 2010, Dr. ALİ CAN TAKİNACI Slide No: 3 EasyHard

Introduction to Arrays - Part I Declaring Arrays Before an array can be used, its type and the number of elements it contains must be declared to the compiler in a type declaration statement, so that the compiler will know what sort of data is to be stored in the array, and how much memory is required to hold it. For example, a real array voltage containing 16 elements could be declared as follows. Similarly, an array of fifty 20-character-long variables could be declared as above. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 4 Same

Introduction to Arrays - Part I Array constants may also be defined. An array constant is an array consisting entirely of constants. It is defined by placing the constant values between special delimiters, called array constructors. The starting delimiter of a Fortran 95 array constructor is (/, and the ending delimiter of an array constructor is /). For example, the expression shown below defines an array constant containing five integer elements: © 2010, Dr. ALİ CAN TAKİNACI Slide No: 5

Introduction to Arrays - Part I USING ARRAY ELEMENTS IN FORTRAN STATEMENTS Array Elements Are Just Ordinary Variables Each element of an array is a variable just like any other variable, and an array element may be used in any place where an ordinary variable of the same type may be used. Array elements may be included in arithmetic and logical expressions, and the results of an expression may be assigned to an array element. For example, assume that arrays index and temp are declared as: © 2010, Dr. ALİ CAN TAKİNACI Slide No: 6

Introduction to Arrays - Part I Initialization of Array Elements Just as with ordinary variables, the values in an array must be initialized before use. If an array is not initialized, the contents of the array elements are undefined. In the following Fortran statements, array j is an example of an uninitialized array. The array j has been declared by the type declaration statement, but no values have been placed into it yet. Since the contents of an uninitialized array are unknown and can vary from computer to computer, the elements of the array should never be used until they are initialized to known values. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

Introduction to Arrays - Part I The elements in an array may be initialized by one of three techniques: 1. Arrays may be initialized by using assignment statements. 2. Arrays may be initialized in type declaration statements at compilation time. 3. Arrays may be initialized by using READ statements. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 8

Introduction to Arrays - Part I 1. Initializing arrays with assignment statements Initial values may be assigned to the array by using assignment statements, either element by element in a DO loop or all at once with an array constructor. For example, the following DO loop will initialize the elements of array array 1 to 0.0, 2.0, 3.0, etc. one element at a time: © 2010, Dr. ALİ CAN TAKİNACI Slide No: 9

Introduction to Arrays - Part I A program to calculate the squares of the integers from 1 to 10, using assignment statements to initialize the values in array number. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 10

Introduction to Arrays - Part I 2. Initializing arrays in type declaration statements Initial values may be loaded into an array at compilation time by declaring their values in a type declaration statement. To initialize an array in a type declaration statement, we use an array constructor to declare its initial values in that statement. For example, the following statement declares a five-element The five-element array constant (/ 1, 2, 3, 4, 5 I) was used to initialize the five-element array array2. In general, the number of elements in the constant must match the number of elements in the array being initialized. Either too few or too many elements will result in a compiler error. This method works well to initialize small arrays, but what do we do if the array has 100 (or even 1000) elements? © 2010, Dr. ALİ CAN TAKİNACI Slide No: 11

Introduction to Arrays - Part I Writing out the initial values for a 100-element array would be very tedious and repetitive. To initialize larger arrays, we can use an implied DO loop. An implied DO loop has the general form Initializing arrays with READ statements Arrays may also be initialized with READ statements. The use of arrays in I/O statements will be described in detail in next week. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 12

Introduction to Arrays - Part I A program to calculate the square roots of the integers from 1 to 10, using a type declaration statement to initialize the values in array value. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 13

Introduction to Arrays - Part I USING WHOLE ARRAYS AND ARRAY SUBSETS IN FORTRAN STATEMENTS Under certain circumstances, whole arrays may be used in arithmetic calculations as though they were ordinary variables. If two arrays are the same shape, then they can be used in ordinary arithmetic operations, and the operation will be applied on an element-by- element basis (see Figure). © 2010, Dr. ALİ CAN TAKİNACI Slide No: 14

Introduction to Arrays - Part I © 2010, Dr. ALİ CAN TAKİNACI Slide No: 15

Introduction to Arrays - Part I Two arrays can be used as operands in an intrinsic operation (addition etc.) if and only if they have the same shape. This means that they must have the same number of dimensions (the same rank), and the same number of elements in each dimension (the same extent). Two arrays of the same shape are said to be conformable. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 16

Introduction to Arrays - Part I Array Subsets A subset of an array is called an array section. It is specified by replacing an array subscript with a subscript triplet or vector subscript. A subscript triplet has the general form where subscript_1 is the first subscript to be included in the array subset, subscript_2 is the last subscript to be included in the array subset, and stride is the subscript increment through the data set. Then the array subset array (1:10:2) would be an array containing only elements array(1), array(3), array(5), array(7), and array(9). © 2010, Dr. ALİ CAN TAKİNACI Slide No: 17