1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
Chapter 10.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Multiple-Subscripted Array
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with 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
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.
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Object-Oriented Programming in C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS 261 – Data Structures Introduction to C Programming.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
POINTERS.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
General comments [1]  array is a fixed sized group in which the elements are of the same type  The general form of array's definition is as follows:
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
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
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Pointers and Pointer-Based Strings
Arrays.
Call by Value Call by Reference Review
7 Arrays.
CS150 Introduction to Computer Science 1
CHAPTER 2 Arrays and Vectors.
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
CHAPTER 2 Arrays and Vectors.
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3

2 Agenda Arrays – What are they ?  Declaring Arrays Array storage and retrieval Initializing an array Using loops to process an array Array index out of bounds Passing an array to a function

3 Arrays – What are they ? A sequence of objects which have the same type

4 Arrays – Think of Lockers Much like a locker room with lockers of the same size Each locker is identified by a unique number or an index

5 Arrays – Analogy to Locker Numbers in a Gym 0123 Lockers Locker Number Stuff that’s in the locker In most programming languages including C, C++ and Java, the first array index would start with 0 and not 1. This is known as zero-based indexing. The Locker Number is also called the “Index” or “Position”

6 Arrays – Referring to a particular “locker” The name of the Array as a whole This cell is referred to as lockers[0] 0123 Lockers

7 Agenda Arrays – What are they ? Declaring Arrays  Array storage and retrieval Initializing an array Using loops to process an array Array index out of bounds Passing an array to a function

8 Declaring an Array of Integers Declaring an array type name-of-array[size-of-array]; Example int myarray[10]; myarray myarray holds 10 integers: myarray[0] through myarray[9]

9 Defining an array of char Defining an array type name-of-array[size-of-array]; Example char word[6]; word word holds 6 char data: word[0] through word [5] Trivia: this is what Is called a C-string!

10 Agenda Arrays – What are they ? Declaring Arrays Array storage and retrieval  Initializing an array Using loops to process an array Array index out of bounds Passing an array to a function

11 Array storage and retrieval #include void main() { float a[3]; a[0] = 55.55; a[1] = 11.11; a[2] = 33.33; cout << “a[0] = “<< a[0] << endl; cout << “a[1] = “<< a[1] << endl; cout << “a[2] = “<< a[2] << endl; } a Put a 55 in first cell Program Output: a[0] =55.55 a[1] =11.11 a[2] =33.33 Output last cell to console

12 More Array Storage and Retrieval Each cell of the array is just like a separate variable cin>>a[2]; // input cell 2 of array a cout<<a[1];// display cell 1 of array a cout<<a[0]<<“ “<<a[1]<<“ “<<a[2]<<endl; a[1]=a[2]; //assign cell2 of a to cell1 a[0]=a[1]+a[2]; //put sum of 1 & 2 in 0 File input and output is just as easy infile>>a[0]>>a[1]>>a[2];

13 Agenda Arrays – What are they ? Declaring Arrays Array storage and retrieval Initializing an array  Using loops to process an array Array index out of bounds Passing an array to a function

14 Arrays – Initialization List When arrays are declared, they contain garbage data Arrays can be initialized similar to structs float a[3] = {22.2, 44.4, 66.6}; a ONLY AT DECLARATION TIME!

15 Arrays – Initialization List Arrays of char (C-strings) can be initialized one of two ways: char word[6] = {‘H’,’e’,’l’,’l’,’o’}; char word[6] = “Hello”; word Hello\0 Method 2 puts a string terminator symbol (\0) in the array You should make size one larger than number of letters Terminator symbol

16 Arrays – Partial Initializing When the list is smaller than the size of the array, 0’s are used to fill in remaining cells float a[6] = { 22.2, 44.4, 66.6}; a

17 Your Turn, I Draw the following arrays float data[8]={6.2, 3.4, 3.5}; char text[10]=“Albatross”; int temp[6]={3};

18 Your Turn, II Draw the following arrays after modifying as shown: float data[8]={6.2, 3.4, 3.5}; data[6]=data[2]; data[5]=data[2]+data[1]; char text[10]=“Albatross”; text[1]=text[2]=‘x’;

19 Agenda Arrays – What are they ? Declaring Arrays Array storage and retrieval Initializing an array Using loops to process an array  Array index out of bounds Passing an array to a function

20 Arrays – Using for loops For loops are a more compact way of processing loops Instead of this: cin>>a[0]; cin>>a[1]; cin>>a[2]; cin>>a[3]; cin>>a[4]; cin>>a[5]; You can do this: for (k=0; k<6; k++) cin>>a[k];

21 Arrays – Using for loops Anytime you see a pattern in what you are doing, you can replace it with a for-loop: Instead of this: a[0]=a[9]; a[1]=a[8]; a[2]=a[7]; a[3]=a[6]; a[4]=a[5]; You can do this: for (k=0; k<5; k++) a[k] = a[9-k];

22 Advantage of for loops More compact, Saves typing Easier to modify if size of array changes Since for loops are so common, a technique of using a const int for size of the array: const int SIZE=10; int k, data[SIZE]; for (k=0; k >data[k];

23 Your Turn III Draw the following arrays float k, data[8]; for (k=0; k<8; k++) data[k]=k; float k, data[8]; for (k=0; k<8; k++) data[k]=k-5; float k, data[8]; for (k=0; k<8; k++) data[k]=7-k;

24 Agenda Arrays – What are they ? Declaring Arrays Array storage and retrieval Initializing an array Using loops to process an array Array index out of bounds  Passing an array to a function

25 Arrays – NOTE Initializing an array with more values than the size of the array would lead to a run-time (not compiler!) error Referring to an array index larger than the size of the array would lead to a run- time (not compiler!) error

26 Arrays – Index out of bounds #include void main() { const int SIZE=4; float a[SIZE] = { 33.3, 44.4, 55.5, 66.6 }; for (int i=0; i < 7; i++) cout << "a[" << i << "] = " << a[i] << endl; } You can get very weird results! Slots 4-6 are out of bounds

27 Arrays – Index out of bounds This would display the entries of the array The last three element displayed are junk since this is not part of the array and is part of memory which are not initialized Sometimes you can accidentally modify data that does not belong to your array! Like…another variable!!

28 Agenda Arrays – What are they ? Declaring Arrays Array storage and retrieval Initializing an array Using loops to process an array Array index out of bounds Passing an array to a function 

29 Passing an Array to a Function When passing an array to a function, we need to tell the compiler what the type of the array is and give it a variable name, similar to an array declaration float a[] We don’t want to specify the size so function can work with different sized arrays. Size comes in as a second parameter This would be a parameter in fn header

30 Passing an Array to a Function #include void Display(int data[], int N) {int k; cout<<“Array contains”<<endl; for (k=0; k<N; k++) cout<<data[k]<<“ “; cout<<endl; } void main() { int a[4] = { 11, 33, 55, 77 }; Display(a, 4); } An int array parameter of unknown size The size of the array The array argument, no []

31 Passing an Array to a Function #include int sum(int data[], int n); //PROTOTYPE void main() { int a[] = { 11, 33, 55, 77 }; int size = sizeof(a)/sizeof(int); cout << "sum(a,size) = " << sum(a,size) << endl; } int sum(int data[], int n) { int sum=0; for (int i=0; i<n;i++) sum += data[i]; return sum; } An int array parameter of unknown size The size of the array The array argument, no []

32 Arrays are always Pass By Reference Arrays are automatically passed by reference. Do not use &. If the function modifies the array, it is also modified in the calling environment void Zero(int arr[], int N) { for (int k=0; k<N; k++) arr[k]=0; }

33 Your Turn Add function calls to Zero out the array below, and Display it: int samples[10000]; // your call to Zero here: // your call to Display here:

34 That’s a wrap ! What we learned today: What are arrays Array indexing Array initialization Array index out of bound error Passing arrays to functions