Arrays November 8, 2017.

Slides:



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

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,
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
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. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Arrays.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
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: 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)
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
CS1010E Programming Methodology Tutorial 6 Arrays and Search C14,A15,D11,C08,C11,A02.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Computer Programming for Engineers
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Arrays. Arrays are objects that help us organize large amounts of information.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Pointers What is the data type of pointer variables?
Array in C# Array in C# RIHS Arshad Khan
EGR 2261 Unit 9 One-dimensional Arrays
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Two Dimensional Array Mr. Jacobs.
Foundations of Programming: Arrays
Lecture-5 Arrays.
Learning Objectives Pointers Pointer in function call
A simple way to organize data
Introduction to C++ October 2, 2017.
C++ Arrays.
CS 1430: Programming in C++.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Variables with Memory Diagram
Lecture 8b: Strings BJ Furman 15OCT2012.
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Building Java Programs
Strings A collection of characters taken as a set:
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Lecture 12 Oct 16, 02.
INC 161 , CPE 100 Computer Programming
Multidimensional Arrays
By Hector M Lugo-Cordero September 17, 2008
Arrays ICS2O.
MSIS 655 Advanced Business Applications Programming
2 code samples int [] array; int i, j, temp; for(i = 0; i < array.length/2; i++) { j = array.length-1-i; temp = array[i]; array[i] = array[j]; array[j]
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Building Java Programs
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
Arrays in Java.
CS150 Introduction to Computer Science 1
Suggested self-checks: Section 7.11 #1-11
Fundamental Programming
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
CS150 Introduction to Computer Science 1
CS31 Discussion 1D Winter19: week 4
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

Arrays November 8, 2017

Arrays An array (also known as a list) is essentially a collection of variables of the same type. These variables are known as the elements of the array. int score[5]; declares an array of type int with five values. This is equivalent to int score_1, score_2, score_3, score_4, score_5; The elements in the array are numbered beginning with index 0, not 1! N.B. On the AP exam, all lists begin with index 1.

Array Elements To refer to a specific element in the array, place its index in brackets. score[0] refers to the first element, score[1] refers to the second, and so on until score[4], the last element. Important: If we declare an array of size n, the indices of the elements range from 0 to n – 1! Remember: on the AP exam, indices start at 1, so an array of size n has indices ranging from 1 to n.

Initializing Arrays An array can be initialized when it is declared. Consider the following code: int scores[3] = {2, 12, 1}; This is equivalent to the following: int scores[3]; scores[0] = 2; scores[1] = 12; scores[2] = 1;

Initializing Arrays If you list fewer values than the total number, the remaining values will be initialized to 0 of the array type. int array[3] = {1, 2}; //array is {1, 2, 0} double array[3] = {1.5, 2.5}; //array is {1.5, 2.5, 0} char array[3] = {'a', 'b'}; //array is {a, b, null} string array[3] = {"a", "b"}; //array is {a, b, null}

Initializing Arrays If an array is initialized when it is declared, the size can be omitted—it will be given the minimum size required to store the given elements. The following declarations are equivalent: int a[] = {1, 2, 3}; int a[3] = {1, 2, 3};

Array Practice What is the output of the following code? char symbol[3] = {'a', 'b', 'c'}; for(int index = 0; index < 3; index++) { cout << symbol[index]; }

Array Practice What is the output of the following code? int a[3] = {1, 2, 3}; a[1] = a[2]; cout << a[0] << " " << a[1] << " " << a[2] << endl;

Array Practice What is the output of the following code? int i, temp[10]; for(i = 0; i < 10; i++) { temp[i] = 2 * i; } cout << temp[i] << " ";

Array Practice What is the output of the following code? int i, temp[10] = {0}; for(i = 0; i < 10; i = i + 2) { temp[i] = 2 * i; } for(i = 0; i < 10; i++) cout << temp[i] << " ";