(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.

Slides:



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

Introduction to C Programming
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Introduction to arrays
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Module 1: Array ITEI222 - Advance Programming Language.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
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.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS.
Computer Programming BCT 1113
Arrays and Records.
EKT150 : Computer Programming
(1-1) C Review: Pointers, Arrays, Strings, & Structs
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Presentation transcript:

(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University

C. Hundhausen, A. O’Fallon 2 What is an array? A sequence of items that are contiguously allocated in memory All items in the array are of the same data type and of the same size All items are accessed by the same name, but a different index

C. Hundhausen, A. O’Fallon 3 More About Arrays An array is a data structure – A data structure is a way of storing and organizing data in memory so that it may be accessed and manipulated efficiently

C. Hundhausen, A. O’Fallon 4 Uses for Arrays? Store related information – Student ID numbers – Names of players on the Seattle Mariners roster – Scores for each combination in Yahtzee – Many more…

C. Hundhausen, A. O’Fallon 5 The Many Dimensions of an Array A single dimensional array is logically viewed as a linear structure A two dimensional array is logically viewed as a table consisting of rows and columns What about three, four, etc., dimensions?

C. Hundhausen, A. O’Fallon 6 Declaring a Single Dimensional Array (1) Arrays are declared in much the same way as variables: int a[6]; declares an array a with 6 cells that hold integers: Notice that array indexing begins at a[0]a[1]a[2]a[3]a[4]a[5]

C. Hundhausen, A. O’Fallon 7 Declaring a Single Dimensional Array (2) We can declare arrays alongside simple variables: int students[100], count, teachers[50]; double gpa[100], average; char ch, name[100]; /* name is actually a string */

C. Hundhausen, A. O’Fallon 8 Manipulating Array Cells Assuming the previous array: all of the following statements are valid: a[0] = 4; /* changes the value of a[0] from 10 to 4 */ a[2] += 2; /* sets the value of a[2] to 2 */ a[5] = a[3] – a[4]; /* sets the value of a[5] to 88 */ a[0]a[1]a[2]a[3]a[4]a[5]

C. Hundhausen, A. O’Fallon 9 Parallel Arrays (1) Often, we'd like to associate the values in one array with those in another array – A list of student numbers, together with their class standings, for example We can declare parallel arrays to accomplish this: #define NUM_STUDENTS 100 typedef enum {freshman, sophomore, junior, senior} class_t; int id[NUM_STUDENTS]; class_t class[NUM_STUDENTS];

C. Hundhausen, A. O’Fallon 10 Parallel Arrays (2) The parallel arrays of student numbers and class standings might look something like this: id[0]id[1]id[2]id[3]id[4]id[5] freshmanseniorjunior sopho- more juniorfreshman class[0]class[1]class[2]class[3]class[4]class[5]

C. Hundhausen, A. O’Fallon 11 Initializing Arrays We can initialize arrays at the time we declare them Just as int count = 0; is valid, so too is int student_id[] = {3423, 8794, 4595, 1423, 4311, 5153, 9182, 1481, 1253, 1222, 2521, 2251, 2111}; Notice how you can omit the size of the array; the compiler deduces the size from the number of values listed.

C. Hundhausen, A. O’Fallon 12 Array Subscripts We can do arithmetic on array subscripts! Assume this array: Then all of the following are valid: int x = 2; printf("%d",a[x + 2]); /* a[4] == 1 */ printf("%d",a[2 * x – 1]); /* a[3] == 89 */ printf("%d",a[x] – a[x-1]); /* -12 */ printf("%d",a[++x]); /* a[3] == 89; x == 3 */ a[x – 1] = a[x – 2]; /* assigns 12 to a[2] */ printf("%d",a[x + 4]);/* Does a[7] exist? */ a[0]a[1]a[2]a[3]a[4]a[5]

C. Hundhausen, A. O’Fallon 13 You Try It (1) Write a segment of code that creates an array of 10 double values, populates the array with the values 1.0 through 10.0, and finally exchanges the 1 st and 10 th values.

C. Hundhausen, A. O’Fallon 14 You Try It (2) Solution: double array[] = {1.0, 2.0, 3.0, 4.0, 5.0, , 8.0, 9.0, 10.0}; double temp; temp = array[9]; array[9] = array[0]; array[0] = temp;

C. Hundhausen, A. O’Fallon 15 Using Loops to Access Array Elements (1) We often need to process each element of an array in turn – Example: Computing the average, minimum, and maximum of a group of values (sound familiar?) We can accomplish this with a for loop that goes from 0 to one less than the array size

C. Hundhausen, A. O’Fallon 16 Using Loops to Access Array Elements (2) int scores [] = {56,78,12,90,85,74,95,80,40,95}; int count = 10, i, sum = 0, max = 0, min = 100; double average; for (i = 0; i < count; ++i) /* we loop from 0 to 9 */ { sum += scores[i]; if (scores[i] > max) max = scores[i]; if (scores[i] < min) min = scores[i]; } average = (double) sum / (double) count; printf("average: %.2f\n",average); printf("maximum: %d\n",max); printf("minimum: %d\n",min); /* Could also display a differences table here, just as the book does (see Fig. 8.3, p. 377 */

C. Hundhausen, A. O’Fallon 17 Passing Arrays as Parameters The previous example would exhibit better top- down design if it broke the problem down into functions: – get_scores /* Let's assume that the scores should be read from an input file */ – compute_stats /* Given an array of values, computes the high, low, and average */ – display_stats /* Displays the high, low, and average */ – display_differences_table /* displays a table of the values read in and the difference between each value and the mean */

C. Hundhausen, A. O’Fallon 18 Next Lecture… We'll continue our exploration of arrays: – Searching and sorting algorithms – Multidimensional arrays

C. Hundhausen, A. O’Fallon 19 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, 2016 P.J. Deitel & H.M. Deitel, C How to Program (7 th Ed.), Pearson Education, Inc., 2013.

C. Hundhausen, A. O’Fallon 20 Collaborators Chris Hundhausen