EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages 519-549 in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.

Slides:



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

Introduction to C Programming
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.
CHAPTER 10 ARRAYS II Applications and Extensions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Arrays Chapter 6.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
 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.
Chapter 9: Arrays and Strings
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 9: Arrays and Strings
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
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.
Programming Logic and Design Fourth Edition, Comprehensive
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Arrays.
EGR 2261 Unit 10 Records (structs)
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2006 Pearson Education, Inc. All rights reserved Arrays.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
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.
Chapter 8 Arrays and Strings
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 8: Arrays.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
EGR 2261 Unit 11 Pointers and Dynamic Variables
EGR 2261 Unit 10 Two-dimensional Arrays
EGR 2261 Unit 9 One-dimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
7 Arrays.
Presentation transcript:

EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.

Chapter 8 Arrays and Strings

Introduction Simple data type: variables of these types can store only one value at a time. Structured data type: a data type in which each data item is a collection of other data items. 3 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Arrays Array: a collection of a fixed number of components, all of the same data type. One-dimensional array: components are arranged in a list form. Syntax for declaring a one-dimensional array: where intExp is any constant expression that evaluates to a positive integer. 4 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Accessing Array Components Syntax for accessing an array component: where indexExp is called the index. – It’s An expression with a nonnegative integer value. Value of the index is the position of the item in the array. [] is called the array subscripting operator – Array index always starts at 0. 5 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Accessing Array Components (cont’d.) 6 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Accessing Array Components (cont’d.) 7 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Processing One-Dimensional Arrays Basic operations on a one-dimensional array: – Initializing – Inputting data – Outputting data stored in an array – Finding the largest and/or smallest element Each operation requires ability to step through the elements of the array. – Easily accomplished by a loop. 8 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Processing One-Dimensional Arrays (cont’d.) Given the declaration: int list[100]; //array of size 100 int i; Use a for loop to access all array elements: for (i = 0; i < 100; i++)//Line 1 cin >> list[i]; //Line 2 9 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Array Index Out of Bounds Index of an array is in bounds if the index is >=0 and <= ARRAY_SIZE-1. – Otherwise, the index is out of bounds. In C++, there is no guard against indices that are out of bounds. You (the programmer) must insure that you never use out-of-bounds indices. 10 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Array Initialization During Declaration Arrays can be initialized during declaration. – Values are placed between curly braces. – Size determined by the number of initial values in the braces. Example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68}; 11 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Partial Initialization of Arrays During Declaration The statement: int list[10] = {0}; – Declares an array of 10 components and initializes all of them to zero. The statement: int list[10] = {8, 5, 12}; – Declares an array of 10 components and initializes list[0] to 8, list[1] to 5, list[2] to 12. – All other components are initialized to C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Some Restrictions on Array Processing Aggregate operation: any operation that manipulates the entire array as a single unit. – Not allowed on arrays in C++. Example: Solution: 13 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Arrays as Parameters to Functions Arrays are passed by reference only. Do not use symbol & when declaring an array as a formal parameter. Size of the array is usually omitted. – If you provide the size, the compiler ignores it. Example: 14 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Constant Arrays as Formal Parameters 15 Can prevent a function from changing the actual parameter when passed by reference. – Use const in the declaration of the formal parameter. Example: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Base Address of an Array and Array in Computer Memory Base address of an array: address (memory location) of the first array component. Example: – If list is a one-dimensional array, its base address is the address of list[0]. When an array is passed as a parameter, the base address of the actual array is passed to the formal parameter. 16 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Functions Cannot Return a Value of the Type Array C++ does not allow functions to return a value of type array. 17 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Integral Data Type and Array Indices C++ allows any integral type to be used as an array index. – Improves code readability Example: 18 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Other Ways to Declare Arrays 19 Examples: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Searching an Array for a Specific Item Sequential search (or linear search): – Searching a list for a given item, starting from the first array element. – Compare each element in the array with value being searched for. – Continue the search until item is found or no more data is left in the list. 20 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Sorting Selection sort: rearrange the list by selecting an element and moving it to its proper position. Steps: – Find the smallest element in the unsorted portion of the list. – Move it to the top of the unsorted portion by swapping with the element currently there. – Start again with the rest of the list. 21 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Selection Sort (cont’d.) 22 C++ Programming: From Problem Analysis to Program Design, Seventh Edition