CPS120: Introduction to Computer Science Lecture 15 Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
 2006 Pearson Education, Inc. All rights reserved 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.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
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
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
CPS120: Introduction to Computer Science
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Working with Arrays in MATLAB
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.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Objectives You should be able to describe: One-Dimensional Arrays
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
7 Arrays.
Presentation transcript:

CPS120: Introduction to Computer Science Lecture 15 Arrays

Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can be single or multi-dimensioned Vector classifications are object-oriented representations of arrays

Declaring an Array To declare an array before it is used in the body of your program, you must use a statement like: int scores[10]; This would declare an array of integers, named "scores". In this case, scores can store up to 10 different integer values. The positions of the array are identified by their index positions which run from 0 to 9 (not 1 to 10.) Each one of the 10 variables in scores is called an element

Initializing an Array If you wish to initialize each element of an array to a specific value, you can use the statement, int scores[] = {65, 76, 45, 83, 99}; You don't even have to specify a size of the array in this case since the initialization statement would cause the compiler to declare an array of size 5 since there are five values in the set of curly braces

Using Arrays When you visualize an array or represent it on paper, you might think of it like this: int scores[5] = {65, 76, 45, 83, 99}; scores index position01234 element's value

Pitfalls of Using Arrays Be careful not to attempt to access an index position that is not actually part of an array. You may not experience a syntax or run-time error. Rather, you may accidentally overwrite another variable int scores[5]; // scores has 5 elements which have index positions 0 through 4 scores[5] = 99; // this assignment statement causes an error

Loops and Arrays Use loops in conjunction with arrays to allow the user to input data into an array Use loops with arrays to display data stored in an array

Relationship Between Pointers &Arrays state_code … M I \

C++ and Strings C++ numbers the individual characters in a string beginning with index position 0. After the declaration statement, string stateName = "Michigan"; 'M' is considered to be in the index position 0, the first 'i' in the index position 1, the 'c' in the index position 2, and so on. Note that index positions may contain a blank space, which is, of course, a valid character. A blank space has an ASCII value of 32.

Using Arrays Elements must be accessed individually to change their values They are accessed via their subscripts which run from zero to 1 less than the number of elements declared Remember, you can define an element outside of your array and create an unpredictable situation

Changing Values in an Array You may use subscript notation to change the value of one specific character within a string. The assignment statement, stateName[1] = 'I'; would change the lowercase e originally found in index position 1 of the example above to an uppercase I. So the string is now "MIchigan".

Templates Allow you to create classes and functions that can be used with any data type An array class can be created that will allow the programmer to choose the data type used to tore the data in the array Templates allow a programmer to construct a class that can be utilized by any data type

Parallel Arrays Sometimes, two one-dimensional arrays are used together to tie two sets of data together For example, you can store 5 student ID numbers in a one-dimensional array of integers, while storing the grades of the same 5 students in an array of doubles. As long as the student grades are sorted in the same representative order as the student ID numbers, one can use the two arrays as parallel arrays. This allows the same index position to refer to a given student's ID number and his/her grade by accessing the appropriate elements of the two arrays

Multi-dimensional Arrays Arrays of more than one dimension can be used to arrange more complicated sets of data Two-dimensional arrays are used quite often For example, a spreadsheet can be thought of as 2- dimensional array (sometimes called a tabl.) The same variable name (that is, identifier) is used to access each element of the 2-dimensional array but the proper index position subscripts must be used

Declaring a Multi-dimensional Array To declare an array of integers called studentGrades to be a 2-dimensional array with 3 rows and 4 columns, you would use the statement: int studentGrades[3] [4]; where the first integer value is used to specify the number of rows in the array and the second value specifies the number of columns Think of remote control

Initializing a Multi-dimensional Array You can initialize the 2-dimensional array when you declare it by using commas and braces appropriately int studentGrades[3] [4] = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12} }; Be careful though when assigning values to a specific position within a 2-dimensional array. Just like one-dimensional arrays, the subscript positions with regard to the rows and the columns begin at 0, not In the example above the value of the variable studentGrades[0] [2] is 3

Displaying a Multi-dimensional Arrray A two dimensional array can be obtained as input and displayed as a table or matrix using double nested loops The use of '\t' causes the elements to be neatly separated by tabs. The cout << endl; statement separates the rows