Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

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.
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.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
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 (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.
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.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
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.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
 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 :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
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.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
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.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
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 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
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
Presentation transcript:

Arrays

Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with the restrictions on array processing Discover how to pass an array as a parameter to a function C++ Programming: From Problem Analysis to Program Design, Fifth Edition2

Objectives (cont'd.) Learn how to search and array Discover how to manipulate data in a two- dimensional array Learn about multidimensional arrays C++ Programming: From Problem Analysis to Program Design, Fifth Edition3

Introduction A data type is called simple if variables of that type can store only one value at a time A structured data type is one in which each data item is a collection of other data items C++ Programming: From Problem Analysis to Program Design, Fifth Edition4

Arrays Array: a collection of a fixed number of components wherein all of the components have the same data type In a one-dimensional array, the components are arranged in a list form Syntax for declaring a one-dimensional array: intExp evaluates to a positive integer C++ Programming: From Problem Analysis to Program Design, Fifth Edition5

Arrays (cont'd.) Example: int num[5]; C++ Programming: From Problem Analysis to Program Design, Fifth Edition6

Accessing Array Components General syntax: where indexExp, called an index, is any expression whose value is a nonnegative integer Index value specifies the position of the component in the array [] is the array subscripting operator The array index always starts at 0 C++ Programming: From Problem Analysis to Program Design, Fifth Edition7

Accessing Array Components (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition8

Accessing Array Components (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition9

Accessing Array Components (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition10

Accessing Array Components (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition11

Processing One-Dimensional Arrays Some basic operations performed on a one-dimensional array are: –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 C++ Programming: From Problem Analysis to Program Design, Fifth Edition12

Processing One-Dimensional Arrays (cont'd.) Consider the declaration int list[100]; //array of size 100 int i; Using for loops to access array elements: for (i = 0; i < 100; i++) //Line 1 //process list[i] //Line 2 Example: for (i = 0; i < 100; i++) //Line 1 cin >> list[i]; //Line 2 C++ Programming: From Problem Analysis to Program Design, Fifth Edition13

Processing One-Dimensional Arrays (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition14

Processing One-Dimensional Arrays (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition15

Array Index Out of Bounds If we have the statements: double num[10]; int i; The component num[i] is valid if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 The index of an array is in bounds if the index >=0 and the index <= ARRAY_SIZE-1 –Otherwise, we say the index is out of bounds In C++, there is no guard against indices that are out of bounds C++ Programming: From Problem Analysis to Program Design, Fifth Edition16

Array Initialization During Declaration Arrays can be initialized during declaration –In this case, it is not necessary to specify the size of the array Size determined by the number of initial values in the braces Example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68}; C++ Programming: From Problem Analysis to Program Design, Fifth Edition17

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

Partial Initialization of Arrays During Declaration (cont'd.) The statement: int list[] = {5, 6, 3}; declares list to be an array of 3 components and initializes list[0] to 5, list[1] to 6, and list[2] to 3 The statement: int list[25]= {4, 7}; declares an array of 25 components; initializes list[0] to 4 and list[1] to 7 ; all other components are initialized to 0 C++ Programming: From Problem Analysis to Program Design, Fifth Edition19

Some Restrictions on Array Processing Consider the following statements: C++ does not allow aggregate operations on an array: Solution: C++ Programming: From Problem Analysis to Program Design, Fifth Edition20

Some Restrictions on Array Processing (cont'd.) The following is illegal too: Solution: The following statements are legal, but do not give the desired results: C++ Programming: From Problem Analysis to Program Design, Fifth Edition21

Arrays as Parameters to Functions Arrays are passed by reference only The symbol & is not used when declaring an array as a formal parameter The size of the array is usually omitted –If provided, it is ignored by the compiler C++ Programming: From Problem Analysis to Program Design, Fifth Edition22

Constant Arrays as Formal Parameters C++ Programming: From Problem Analysis to Program Design, Fifth Edition23

Base Address of an Array and Array in Computer Memory The base address of an array is the address, or memory location of the first array component If list is a one-dimensional array, its base address is the address of list[0] When we pass an array as a parameter, the base address of the actual array is passed to the formal parameter C++ Programming: From Problem Analysis to Program Design, Fifth Edition24

Base Address of an Array and Array in Computer Memory (cont’d.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition25

Functions Cannot Return a Value of the Type Array C++ does not allow functions to return a value of the type array C++ Programming: From Problem Analysis to Program Design, Fifth Edition26 Integral Data Type and Array Indices C++ allows any integral type to be used as an array index

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 searchItem with the elements in the array –Continue the search until either you find the item or no more data is left in the list to compare with searchItem C++ Programming: From Problem Analysis to Program Design, Fifth Edition27

Searching an Array for a Specific Item (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition28