Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

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.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
 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.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 7 Arrays. Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
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.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
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.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
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.
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.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
© 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++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
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.
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic 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.
Student Book An Introduction
CSCE 210 Data Structures and Algorithms
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
Object Oriented Programming COP3330 / CGS5409
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Arrays Kingdom of Saudi Arabia
Lecture 18 Arrays and Pointer Arithmetic
Review of Everything Arrays
7 Arrays.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
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.
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
Presentation transcript:

Computer programming1 Arrays

Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef

Computer programming3 Motivation Suppose we want to compute the average of 20 marks. Do we need to declare 20 variables mark1, mark2, …, mark20 ? Do we need to write 20 cout s and 20 cins ? How about sorting a large number of ints? This is where arrays come in!

Computer programming4 Introduction to Arrays An array is a fundamental data structure used to store objects of a particular type in contiguous memory locations. Arrays allow us to randomly access this contiguous memory via indexing. There are essentially two ways to declare arrays in C++: statically and dynamically (until pointers are covered).

Computer programming5 Static Arrays Static array declaration typeName arrayName[sizeOfTheArray] typeName is the base type of the particular type of the elements of the array. Can be any type (primitive or user-defined) arrayName is the name of the array. Any valid C++ identifier sizeOfTheArray is the size or the capacity of the array The above declaration is equivalent to declaring sizeOfTheArray variables. Each variable is an indexed variable, a variable referenced using an index between 0 and sizeOfTheArray arrayName[0] is an indexed variable at the index 0 arrayName[1] is an indexed variable at the index 1 … arrayName[sizeOfTheArray-1] is an indexed variable at the index sizeOfTheArray-1

Computer programming6 Static arrays - Declaration An array consisting of 5 variables of type int is declared: int score[5]; Such a declaration creates 5 int type variables which are accessed as score[0], score[1], score[2], score[3], score[4] l These are called indexed variables, also called subscripted variables. l The number in the brackets is called an index or subscript. l Array subscripts start at 0 and run to one less than size of the array, which is 4. l Be careful not to confuse the size in a declaration of an array with the index value in an array reference. int score[size]; score[index]; // 0 <= index < size It is possible to declare arrays along with ordinary variables: int next, score[5], max;

Computer programming7

8 Static arrays - Initialization l You may initialize specific index variables int score[5]; //actually the indexed variables are initialized to the default //value (usually 0 of int) score[4] = 10; //this can also be read from cin l You can use for statement to initialized the values of an array for (int i=0;i<5;i++) score[i]=i*i+1; l The size of an array can be omitted as shown in the following definition int score[]={2, 1, 5, 4, 6}; l The following definition is also possible int score[5]={2, 1, 5, 4, 6}; l Initialization may cover only the first few elements int marks[10]={1,2}; //the first two elements are initialized to 1 and 2. The others //to the default value.

Computer programming9 Referencing and using arrays Array references may be used anywhere an ordinary variable is used. cin >> score[4] >> score[2]; cout << score[2] << “ “ << score[4]; score[0] = 32; score[3] = score[0]++; Another way of accessing/referencing the elements of an array is discussed later The for statement is a good way to go through the elements of an array Arrays are used in several occasions such as: Searching Sorting

Computer programming10 Problem 1 A set of positive data values (200) are available. It is required to find the average value of these values and to count the number of values that are more than 10% above the average value. The solution will be provided in class

Computer programming11 Problem: search Given an array a of n integers and a value val, find the index of the scripted variable that has the value val ? Linear search Binary search Solution will be provided in class

Computer programming12 Recall that a computer’s memory is like a long list of numbered locations. The numbers are called addresses, and the information written there is either some program’s instructions or data. Recall that good programmers make efficient use of memory. Suite to a running program, a memory is organized into segments Text segment: where the program instructions reside BSS segment: where static and global variables that are uninitialized Data segment: where static and global variables that are initialized go Stack segment: where local variables reside Heap segment: where dynamically allocated variables reside Every variable has an address and a number of bytes necessary to hold the variable value This number of bytes is determined by the type of the variable. Memory allocation

Computer programming13 Memory allocation int size=100;  Data int glb;  BSS int func() { int tmp = 10;  stack int* dyn = new int(200);  heap … return …; }

Computer programming14 Consider: int a[6]; The name a is the address of the element a[0] So, to access an element we could use *(a + index) instead of a[index] Here the compiler decides where in memory to put the array, and the size of the array is 6 * the size of an int. In general, the size of an array is size * sizeof(Array_Type) So, an array has three things: 1) an address of the start of the array in memory 2) a type, which tells how big each indexed variable is 3) the array size, which tells number of indexed variables. Arrays in memory

Computer programming15

Computer programming16 Indexed Variables as Function Arguments An indexed variable is just a variable whose type is the base type of the array, and may be used anywhere any other variable whose type is the base type of the array might be used. void my_function (int x) { cout << x*x <<endl; } int i, n, a[10]; my_function(n); my_function(a[3]);

Computer programming17 Entire Arrays as Function Arguments It is possible to use an entire array as a parameter for a function. This is a new parameter type called an array parameter. Declaration void fillUp(int array[], int nbrOfElts); or void fillUp(int [], int); Definition void fillUp(int array[],int nbrOfElts) { for (int i=0; i < nbrOfElts; i++) array[i]=i*i+1; } Calling the function … int score[100]; fillUp(score, 10); How come we are able to change the array without using references?

Computer programming18 Passing an array is not by value because we can change the array when passed this way. It is by reference because we cannot change the complete array by a single assignment when passed this way. The behavior, however, is like call-by-reference since we can change individual array elements. The array argument tells the caller only the address and type, but not the size of the array. Entire Arrays as Function Arguments

Computer programming19 The const Parameter Modifier Array parameters allow the function to change any value stored in the array. Frequently, this is the intent of the function. Sometimes, however, we want to avoid changing an array parameter. Use the const keyword void printArray( const int a[ ], int size_of_a) { cout << "Array values are:\n"; for ( int i = 0; i < size_of_a; i++) cout << a[i]++ << " "; cout << endl; }

Computer programming20 Pitfalls Do not consider the name of the array as a variable. We cannot increment or decrement an array Do not forget that arrays are not passed by value Array Index Out of Range The most common programming error when using arrays is the attempt to reference a non-existent array index. The statement int a[6]; declares ONLY the indexed variables a[0] through a[5]. An index value outside 0 to 5 is an out of range error, i.e., illegal a[7] = 248; C++ treats 7 as if it were legal subscript, and attempts to write to memory where a[7] would be.

Computer programming21 Problem: Sorting To know Sorting an array means putting data in a certain order. An array that contains objects that can be compared is sorted in a non- decreasing order if for every pair of indices i and j, if i < j then a[i] <= a[j]. This gives: a[0] <= a[1] <= a[2] <=... <= a[number_used - 1] Note that the array is partially filled so we must pass an additional array parameter that specifies how many array elements are used. Algorithms: Bubble sort, Selection sort, Insertion sort, Shell sort, Merge sort, Heapsort, Quicksort, Bucket sort, and Radix sort. Write a program that sorts in a non-decreasing order the elements of an array. The base type can be int, char, float, or double.

Computer programming22 Selection sort

Computer programming23 Arrays, Classes, typedef You can use arrays as attributes in a class You can have an array of classes Person students[1000]; You can use typedef with arrays as follows typedef int ScoreType[100]; … ScoreType score; //int score[100];