Arrays. Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related.

Slides:



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

Chapter 7: Arrays In this chapter, you will learn about
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays Chapter 6.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Computer Science I Arrays Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
 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 Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Array (continue).
 Pearson Education, Inc. All rights reserved 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.
 Pearson Education, Inc. All rights reserved Arrays.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Arrays 1.
Arrays 2.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Declarations CSCI N305
Visual Basic 2010 How to Program
JavaScript: Functions.
INPUT & OUTPUT scanf & printf.
7 Arrays.
Object Oriented Programming in java
JavaScript: Arrays.
Chapter 8 JavaScript: Control Statements, Part 2
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays

Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related data items of the same type. © by Pearson Education, Inc. All Rights Reserved.

Arrays An array is a group of memory locations related by the fact that they all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. Figure 6.1 shows an integer array called c. This array contains 12 elements. Any one of these elements may be referred to by giving the name of the array followed by the position number of the particular element in brackets ( () ). © by Pearson Education, Inc. All Rights Reserved.

Arrays (Cont.) The first element of array c is referred to as c(1), the second element of array c is referred to as c(2), the seventh element of array c is referred to as c(7), and, in general, the ith element of array c is referred to as c(i ). Array names, like other variable names, can contain only letters, digits and underscores. Array names cannot begin with a digit. The position number contained within brackets is more formally called a subscript (or index). A subscript must be an integer or an integer expression. © by Pearson Education, Inc. All Rights Reserved.

Arrays (Cont.) If a program uses an expression as a subscript, then the expression is evaluated to determine the subscript. For example, if a = 5 and b = 6, then the statement C( a + b ) = C( a + b ) + 2; adds 2 to array element c(11). © by Pearson Education, Inc. All Rights Reserved.

Arrays (Cont.) Let’s examine array c (Fig. 6.1) more closely. The array’s name is c. Its 12 elements are referred to as c(1), c(2), …, c(12). The value stored in c(1) is –45, the value of c(2) is 6, the value of c(3) is 0, the value of c(8) is 62 and the value of c(12) is 78. To print the sum of the values contained in the first two elements of array c, we’d write fprintf( ‘%d’, c(1) + c(2) ); © by Pearson Education, Inc. All Rights Reserved.

Defining Arrays b=[1, 3, 5]; String (array of characters): c= ['a', 'b', 'c']; © by Pearson Education, Inc. All Rights Reserved.

Array Examples Next program uses for statements to initialize the elements of a 10-element integer array n to zeros and print the array in tabular format. The first fprintf statement displays the column heads for the two columns printed in the subsequent for statement. © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) for i=1:10 n(i) = 0; end fprintf('%s%13s\n','Element','value'); for i=1:10 fprintf('%7d%13d\n',i, n(i)); end © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) Next program initializes the elements of a 10-element array s to the values 4, 6, …, 22 and prints the array in tabular format. The values are generated by multiplying the loop counter by 2 and adding 2. © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) for i=1:10 s(i) = 2+2*i; end for i=1:10 fprintf('%7d%13d\n',i, s(i)); end © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.)

© by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) Next program sums the values contained in the 12-element integer array a. The for statement’s body does the totaling. © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) a = [1, 3, 5, 22, 6, 7, 90, 2, 88, 12, 45, 32]; total = 0; for i=1:12 total = total + a(i); end fprintf('Total of element values is %d\n',total); © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) Our next example uses arrays to summarize the results of data collected in a survey. Consider the problem statement. Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the poll. This is a typical array application (see Fig. 6.7). We wish to summarize the number of responses of each type (i.e., 1 through 10). © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) The array responses is a 40-element array of the students’ responses. Use an 10-element array frequency to count the number of occurrences of each response. This allows us to use each response directly as the subscript in the frequency array. © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) Our next example (Fig. 6.8) reads numbers from an array and graphs the information in the form of a bar chart or histogram—each number is printed, then a bar consisting of that many asterisks is printed beside the number. Note the use of printf( "\n" ) to end a histogram bar (line 24). © by Pearson Education, Inc. All Rights Reserved.

Array Examples (Cont.) Roll a single six-sided die 6000 times to test whether the random number generator actually produces random numbers. © by Pearson Education, Inc. All Rights Reserved.