Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to C Programming
Chapter 8: Arrays.
Programming and Data Structure
Kernighan/Ritchie: Kelley/Pohl:
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Topic 8 – Introduction to Pointers and Function Output Parameters.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
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.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
 Pearson Education, Inc. All rights reserved Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
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?
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.
Sections 10.1 – 10.4 Introduction to Arrays
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
EKT150 : Computer Programming
Declaration, assignment & accessing
Lecture 18 Arrays and Pointer Arithmetic
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CISC181 Introduction to Computer Science Dr
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Programming Fundamental
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Topic 9 – Introduction To Arrays

CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single memory cell which holds one variable in memory. In order to solve some programming problems, it is often useful to store a set of data (more than one variable) in a specific form, or structure, in memory.

CISC105 – Topic 9 We have seen that when a variable is declared, the operating system finds some location in memory to store it. The location one variable is stored in has no relation to the location other variables are stored in. int x; int y; int z; Variables in Memory (y) (x) (z)

CISC105 – Topic 9 The Array The simplest data structure is that of an array. An array refers to a collection of two or more contiguous (adjacent) memory cells that hold variables of the same data type. Each variable in the array is referred to as an array element.

CISC105 – Topic 9 Array Structure Each element in the array is referenced using two components, the array name and an index. The first is simple the name of the array. The second is the array index. This is simply an integer that indicates which element you are referencing. For example, the first element has index 0, the second has index 1, the third has index 2, and so forth. Notice that the array elements are numbered starting with zero (0) and not one (1).

CISC105 – Topic 9 Array Structure in Memory When an array is declared, the operating system finds some location in memory to store it. Here, the operating system does not simply need to find one memory cell, but one memory cell for each element in the array. Therefore, if an array of 40 double s were to be declared, the OS would allocate a section of memory that is big enough to store 40 double variables, one after the other.

CISC105 – Topic 9 Array Structure in Memory For example, if x was declared to be an array of four integers, a section of memory consisting of four memory cells, one to hold each of the array elements, is allocated. Note that the starting memory address is determined by the operating system (just like each simple variable) (x[0]) (x[1]) (x[2]) (x[3])

CISC105 – Topic 9 Declaring Arrays An array is declared much like a simple variable. After the array name, a bracket “[” is found, then the size of the array (an integer value) and then a closing bracket “]”. Thus, an array of four integers, named x, would be declared as: int x[4];

CISC105 – Topic 9 Declaring Arrays An array can be made of any simple data type. An array of six double s, named y: An array of twelve characters, named p: An array of nine float s, named floaters: double y[6]; char p[12]; float floaters[9];

CISC105 – Topic 9 Referencing Array Elements Once an array has been declared, there must be a method of accessing each element in the array. This is done by using the array name, the bracket “[”, the index of the element being referenced, and then a closing bracket “]”. Thus, in order to access the first element in the array grades: grades[0]

CISC105 – Topic 9 Referencing Array Elements Note that the first element in an array has index 0. The second element has index 1, the third index has index 2, etc… Any statement that can be used to manipulate a simple variable can also be used to manipulate an array element, when used in this manner.

CISC105 – Topic 9 Manipulating Array Elements These are all valid statements (assuming x is an array of float s): printf(“%f”,x[0]); x[3] = 25.0; sum = x[0] + x[1]; sum += x[2]; x[3] += 1.0; x[2] = x[0] + x[1];

CISC105 – Topic 9 Initializing Arrays With simple variables, we can initialize them (set an initial value) when we declare them: int x = 27; float y = 29.4; char grade = ‘C’; double num = 2.1;

CISC105 – Topic 9 Initializing Arrays We can also initialize arrays when we declare them: int array[6] = { 23, 45, 220, -23, 22, 0 }; Array Size Open the initialization list with a “{“ brace. Provide one initialization value for each array element. Separate the initialization list with commas. Close the initialization list with a “}“ brace.

CISC105 – Topic 9 Initializing Arrays This sets array[0] to 23, array[1] to 45, array[2] to 220, array[3] to –23, array[4] to 22, and array[5] to 0. int array[6] = { 23, 45, 220, -23, 22, 0 };

CISC105 – Topic 9 Array Indices: A Closer Look As we have seen, array elements are accessed with two components, the name of the array and the index, which indicates the index of the element of the array we are accessing. This index can range from zero (0) through the number of elements minus one (1). For example, an array with 16 elements has indices ranging from 0 to 15.

CISC105 – Topic 9 Array Indices: A Closer Look When an array element is accessed, the index does not have to be a simple integer. Any expression that evaluates to an integer can be used as the array index. Thus, any expression that evaluates to an integer can be placed inside the brackets “[” “]” in a reference to an array element. Whatever is inside the brackets is first evaluated and then that integer is used as the index. Note that this MUST evaluate to an integer.

CISC105 – Topic 9 Array Indices: Examples What is the output of the following? float x[10] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; int i = 2; printf(“%f”,x[4]); printf(“%f”,x[i]); printf(“%f”,x[i] + 1); printf(“%f”,x[i+1]); printf(“%f”,x[(int)x[4]]); printf(“%f”,x[i++]); printf(“%f”,x[(4+2)/3+i+2]);

CISC105 – Topic 9 Array Index Ranges: Errors This index can range from zero (0) through the number of elements minus one (1). For example, an array with 16 elements has indices ranging from 0 to 15. The index into an array should not go above this range. For example, in an array with 16 elements, the reference array[16] is invalid, as this would attempt to reference the 17 th element.

CISC105 – Topic 9 Array Index Ranges: Errors If this is done, the resulting value is indeterminate (garbage). This is similar to what happens when a variable is used before it is initialized (set to something). It is equal to whatever happens to be in memory at the time, as the program has not yet put anything there.

CISC105 – Topic 9 Array Index Ranges: Errors In this example, of an integer array of four elements, attempting to access x[4] would access whatever is currently in memory at location 1016, which is indeterminate. Thus, don’t do this! (x[0]) (x[1]) (x[2]) (x[3])

CISC105 – Topic 9 Array Index Ranges: Errors Overstepping the bounds of an array is actually much worse than using a variable before it is initialized. When you use a variable before it is initialized, this is like attempting to look into a box (the variable) before you put anything in there (the value). However, the variable has been allocated by the operating system; that memory cell has been reserved by the OS for that variable. No other data will be placed there.

CISC105 – Topic 9 Array Index Ranges: Errors When you overstep the bounds of an array, that memory cell you are referencing has NOT been allocated by the operating system for the array. It may been be allocated for some other purpose (such as storing another variable). Thus, attempting to read (or change) a value is this manner is VERY dangerous.

CISC105 – Topic 9 Array Index Ranges: Errors An Example In this example, z is first allocated and assigned a memory cell at The array x is then allocated at memory cell 1008 – If the bounds on the array x are overstepped, the variable z can be accessed. If you attempt to store a value in x[4], that will change z! (x[0]) (x[1]) (x[2]) (x[3]) (z) int z; int x[4];

CISC105 – Topic 9 Array Index Ranges: Errors An Example Note that this example (where x[4] would modify z) is dependant on the operating system allocating z immediately following the array x. This will not always happen, as where the OS will allocate variable is not known when writing the program. Many times, the memory cell accessed when the array is overstepped will not be allocated, and will thus be filled with garbage (x[0]) (x[1]) (x[2]) (x[3]) (z)

CISC105 – Topic 9 Array Index Ranges: Errors Thus, it is important to keep in mind the C will NOT check to see if the index the program is attempting to access is actually a correct index (within the size of the array). Incorrect array indices will result in unpredictable and potentially hazardous results when the program runs. So…make sure to check your array indices!

CISC105 – Topic 9 Sequential Array Access Very often, a programmer desires to access each element of an array in sequence. This is common as array typically hold data that is related. This is commonly accomplished in C using a for loop statement, creating a counting loop which runs from zero to the size of the array minus one. The loop control variable (the counter) is used as the array index inside the loop body.

CISC105 – Topic 9 Sequential Array Access For example, suppose the array x holds four integers and we wish to put the sum of all four elements in the variable sum int count=0, sum = 0; int x = { 2, 4, 6, 8 }; for (count = 0; count <= 3; count++ ) { sum += x[count]; } (count) 0 (sum) 0 (x[0]) (x[1]) (x[2]) (x[3])