1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : 85 79 92 57 68 80... 0 1 2 3 4.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
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.
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.
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.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
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
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.
Arrays.
Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
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.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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)
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Copyright Curt Hill Arrays in C/C++ What? Why? How?
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.
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.
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.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
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.
Module 1: Array ITEI222 - Advance Programming Language.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
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.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
EGR 2261 Unit 9 One-dimensional Arrays
Computer Programming BCT 1113
C++ Data Types Simple Structured Address Integral Floating
Arrays … The Sequel Applications and Extensions
Arrays and Strings Chapter 9.
EKT150 : Computer Programming
Dr Tripty Singh Arrays.
Arrays Arrays A few types Structures of related data items
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

1 One Dimensional Arrays Chapter 11

2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting arrays Stepping through arrays Passing arrays as parameters

3 Design Problem l Consider a program to calculate class average Why?? ?

4 Add to Design Problem l Now your client says, I need to ALSO calculate and display “deviations” from the average Describe why this will or will NOT work

5 l Enter in the scores again l Use 100 separate variables » and cout and cin commands l Read (then re-read) from a file l The real answer … Possible Solutions  Use arrays !!  

6 Simple vs Structured Data Types l Simple data type => data element contains a single value l Structured data type => a data element contains a collection of data values x : 15 avg : ch : ‘A’ scores : name : ‘C’ ‘L’ ‘Y’ ‘D’ ‘E’

7 Arrays l Arrays are Structured Data Types l They have a means of accessing individual components l Values can be retrieved from and stored in the structure scores : cout << scores[2]; scores[0] = 100;

8 One Dimensional Array l Structured collection of components » All of the same type l Structure given a single name l Individual elements accessed by index indicating relative position in collection l Type of elements stored in an array can be “just about” anything l Index of an array must be an integer

9 Use of Array for Our Problem l Store elements in array as read in l Go back and access for deviations Note declaration

10 Declaring Arrays l Syntax: Data_type Array_name [constant]; l Note declaration from our example Tells how many elements set aside

11 Declaring Arrays l Example specifies an array… » each element is an integer » there is space for 100 elements » the are numbered 0 through 99 scores :

12 Accessing Individual Components l Use the name of the array l Followed by an integer expression inside the square brackets [ ] scores : max = scores[0]; for (x = 0; x max) max = scores[x]; Index can be: - constant - variable - expression MUST be an integer

13 Out of Bounds Index l What happens if … l C++ does NOT check for index out of range l Possible to walk off into “far reaches” of memory -- clobbers... » other variable locations ».exe code » the operating system (??) float f_list [50]; f_list [100] = ; float f_list [50]; f_list [100] = ;

14 Initializing Arrays in Declarations l Possible to declare the size & initialize l Possible to omit size at declaration » Compiler figures out size of array int results [5] = {14, 6, 23, 8, 12 } float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }

15 Aggregate Operations l Defn => an operation on the data structure as a whole » as opposed to operation on a SINGLE element within the structure l Example » would be nice to read in a WHOLE array

16 Lack of Aggregate Operations l Would be nice but... C++ does NOT have... l Assignment operator for whole array l Arithmetic operations for whole array (think matrix) l Comparisons for arrays (not even = =) l Return of an array type by a function

17 How to Accomplish Aggregate Operations? l Most such tasks (assignment, read, write) can be performed some other way » CS II course will write “classes” to provide these functions l Otherwise » these operations must be performed by the programmer » element by element in a loop

18 Arrays as Parameters l This is one task that CAN be done to the WHOLE array l C++ always passes arrays by reference

19 Arrays as Parameters l The name of the array is a pointer constant l The address of the array is passed to the function l Size of the array also passed to control loop

20 Arrays as Parameters l Note the empty brackets in parameter list » A number can be placed here but it will be ignored

21 Sub-array Processing l Note we specified an array size of 100 » but we don’t anticipate that many scores l Array always declared larger than needed l Must keep track of how many have been used » this is our limit when doing other things to the array

22 Design Problem l Consider the task of keeping track of data about parts for manufacture » part number, description, qty needed, unit price

23 Design Problem l Use “Parallel” arrays l One array each for part num, descrip, qty, price l n th item in any one of the arrays associated with same n th item of all the arrays part #descripqtyprice A100xxx B25yyy

24 Testing and Debugging Hints l Range of legal index values is 0 to array_size - 1 l Individual elements of the array are of the component type l No aggregate operations in arrays » you must write the code to do this l If array parameter is incoming, specify formal parameter as const » prevents function from modifying

25 Testing and Debugging Hints l Omitting array size in declaration » when array declared formal parameter » when array initialized at declaration l Don’t pass component when function expects entire array l Declare array size as max ever needed » process only part of array which is used l Pass array name and length to functions which process array or sub array