Arrays.

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
Programming with Collections Collections in Java Using Arrays Week 9.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
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.
Multiple-Subscripted Array
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
Chapter 7 Arrays. Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional 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.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 6: Arrays 数据序列, 数组 1.
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 :
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Arrays.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
ARRAYS (C) KHAERONI, M.SI. OVERVIEW Introduction to Arrays Arrays in Functions Programming with Arrays Multidimensional Arrays.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
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 6 Arrays in C++ 2nd Semester King Saud University
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Programming fundamentals 2 Chapter 1:Array
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
Chapter 2 Elementary Programming
Data type List Definition:
Arrays November 8, 2017.
Counting Loops.
Review of Everything Arrays
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Programming Arrays.
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
Presentation transcript:

Arrays

Introduction to Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why do we need arrays? Imagine keeping track of 5 test scores, or 100, or 1000 in memory How would you name all the variables? How would you process each of the variables?

Arrays An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, collection of doubles).

Array Applications Given a list of test scores, determine the maximum and minimum scores. Read in a list of student names and rearrange them in alphabetical order (sorting). Given the height measurements of students in a class, output the names of those students who are taller than average.

Array Declaration Syntax: <type> <arrayName>[<array_size>] Ex. int Ar[10]; The array elements are all values of the type <type>. The size of the array is indicated by <array_size>, the number of elements in the array. <array_size> must be an int constant or a constant expression. Note that an array can have multiple dimensions.

Array Declaration // array of 10 uninitialized ints int Ar[10]; -- Ar 4 5 6 3 2 8 9 7 1 1 2 3 4 5

Subscripting Declare an array of 10 integers: int Ar[10]; // array of 10 ints To access an individual element we must apply a subscript to array named Ar. A subscript is a bracketed expression. The expression in the brackets is known as the index. First element of array has index 0. Ar[0] Second element of array has index 1, and so on. Ar[1], Ar[2], Ar[3],… Last element has an index one less than the size of the array. Ar[9] Incorrect indexing is a common error.

Subscripting 1 // array of 10 uninitialized ints int Ar[10]; int x = Ar[3]; 1 -- -- 1 Ar 4 5 6 3 2 8 9 7 Ar[4] Ar[5] Ar[6] Ar[3] Ar[0] Ar[2] Ar[8] Ar[9] Ar[7] Ar[1]

Loops And Arrays for-loops are commonly used to step through arrays Example: for (i = 0; i < 5; i++) { cout << score[i] << " off by " << (max – score[i]) << endl; } could display the difference between each score and the maximum score stored in an array Last index is (size – 1) First index is 0

Variables and Declarations Most compilers do not allow the use of a variable to declare the size of an array Example: cout << "Enter number of students: "; cin >> number; int score[number]; This code is illegal on many compilers

Subscripting Example //For loop to fill & print a 10-int array int main ( ) { int index, ar[10]; // array for 10 integers // Read in 10 elements. cout << "Enter 10 integers: "; for(index = 0; index < 10; index ++) cin >> ar[index]; cout << endl; cout << "The integers are "; cout << ar[index] << " "; return 0; }

Array Element Manipulation Consider int Ar[10], i = 7, j = 2, k = 4; Ar[0] = 1; Ar[i] = 5; Ar[j] = Ar[i] + 3; Ar[j+1] = Ar[i] + Ar[0]; Ar[Ar[j]] = 12; cin >> Ar[k]; //where the next input value is 3 -- 8 6 1 Ar 5 3 12 4 2 9 7 Ar[4] Ar[5] Ar[6] Ar[3] Ar[0] Ar[2] Ar[8] Ar[9] Ar[7] Ar[1]

Array Initialization 6 -1 Ar[3] = -1; int Ar[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; Ar[3] = -1; 8 7 6 9 Ar 4 3 2 5 1 6 -1 8 7 -1 9 Ar 4 3 2 5 1 6