Anda di halaman 1dari 29

COMPUTER SCIENCE SEMINAR ON ARRAYS

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION COPYRIGHT 2011 2012 ALL RIGHTS RESERVED

ARRAYS SEMINAR A SHUBHAM TRIVEDI

Overview

Motivation

Introduction to Arrays Declaring and referencing arrays For-loops and arrays Initializing Arrays
Arrays in Functions Arrays as function arguments

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

Why Arrays ?

Need some way to hold onto all the individual data items after processing them Making individual identifiers x1, x2, x3,... is not practical or flexible The answer is to use an ARRAY!!!!!!!!!! A data structure - bigger than an individual variable or constant

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

Objectives
In this seminar we will: Learn about arrays Explore how to declare and manipulate data into arrays Become familiar with the restrictions on array processing Discover how to pass an array as a parameter to a function

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

Example: Ms. White's Test Score Analyzer


Problem: Ms. White needs to analyze students' test performance. She needs a program to display a name and let her enter the test score. Then it should compute and display the average. Finally it should give a report with names, scores, and deviation from the average.
6/23/2012 ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED 5

Motivation

Suppose we want to compute the average marks of 20 students. For doing that do we need to declare 20 variables mark1, mark2, , mark20? Do we need to write 20 couts and 20 cins? How about sorting a large number of ints? This is where arrays come in!

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

Introduction to Arrays

Array definition:

A collection of data of same type where a data structure used to store objects of a particular type in contiguous memory locations.

First "aggregate" data type


Means "grouping" int, float, double, char are simple data types

Used for lists of like items


Test scores, temperatures, names, etc. Avoids declaring multiple simple variables Can manipulate "list" as one entity

Arrays allow us to randomly access this contiguous memory via indexing.

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

Definition of an Array
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.

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION

Array Usage

Powerful storage mechanism

Can issue command like:


"Display all elements of array score" "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score"

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

C++ Arrays start at 0 !!!!!!!


Array indexes always start with zero! If we declare an array of n elements the last one is number n-1 Zero is "first" number to computer scientists If you try to access element number n it is an error! C++ will "let" you go beyond range Unpredictable results Compiler will not detect these errors! ARRAYS SEMINAR A SHUBHAM Up to programmerPRODUCTION TRIVEDI in range" to "stay 6/23/2012
ALL RIGHTS RESERVED

10

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] These are called indexed variables, also called subscripted variables. The number in the brackets is called an index or subscript. Array subscripts start at 0 and run to one less than size of the array, which is 4. 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;
6/23/2012 ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED 11

Array Initialization

With simple variables

Arrays can be initialized while they are being declared Not necessary to specify the size of the array

When initializing arrays while declaring them

Size of array is determined by the number of initial values in the braces For example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68};

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

12

To make things easier.

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

13

Array Program Example: Display 5.1 Program Using an Array (1 of 2)

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

14

Array Program Example: Display 5.1 Program Using an Array (2 of 2)

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

15

for-loops with Arrays


Natural counting loop Naturally works well "counting through" elements of an array Example: for (idx = 0; idx<5; idx++) { cout << score[idx] << "off by " << max score[idx] << endl; } Loop control variable (idx) counts from 0 5
ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED 16

6/23/2012

Multidimensional Arrays

Arrays studied so far have one dimension length It is also possible to define arrays with more than one dimension Two dimensional arrays can store values arranged in rows and columns Three dimensional arrays can store values arranged in rows, columns, and ranks (or pages)
ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED 17

6/23/2012

Sample Problem:

Consider a trucking business with centers in 6 cities. The owner requires a computerized mileage chart for the drivers Given any of the two cities, the program must display the approximate mileage between them

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

18

Program Behavior

Display a numbered menu of cities Read the numbers of two cities from keyboard Look up mileage between the cities in a two dimensional table of values Display the mileage

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

19

Objects
Objects Menu of cities Kind constant Type String Name CITY_MENU

Number of 1st city Number of 2nd city


Mileage chart The mileage
6/23/2012

varying
varying constant varying

int
int int[][] int

city1
city2 MILEAGE_CHART mileage
20

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

Algorithm

Define
MILEAGE_CHART (2D array) CITY_MENU (list of supported cities)

Display CITY_MENU Read two integers from keyboard into city1 and city2 Calculate mileage by looking up MILEAGE_CHART[city1][city2] Display mileage
ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED 21

6/23/2012

Two-Dimensional Array

A two-dimensional array is a collection of components, all of the same type, structured in two dimensions, (referred to as rows and columns) Individual components are accessed by a pair of indexes representing the components position in each dimension The two expressions intexp1 and intexp2 specify the number of rows and the number of columns, respectively, in the array Two-dimensional arrays are sometimes called matrices or tables

DataType ArrayName[ConstIntExpr][ConstIntExpr]...;
ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED 22

6/23/2012

Why 2-dimensional arrays?

Data sometimes has more structure to it than just "a list has rows and columns and uses two subscripts to locate an item Syntax

int a[5][4]; // row then column twenty elements, numbered from [0][0] to [4][3]

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

23

Accessing 2D Array Elements


Requires two indices Which cell is MILEAGE_CHART [3][2]?


[0] [0] [1] [2] [3] [4] 0 97 90 268 262 130 [1] 97 0 74 337 144 128 [2] 90 74 0 354 174 201 [3] 268 337 354 0 475 269 [4] 262 144 174 475 0 238

Row

Column
[5] 130 128 201 269 238 0
24

6/23/2012

[5]

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

EXAMPLE -- Array for monthly high temperatures for all 50 states

const int NUM_STATES = 50; const int NUM_MONTHS = 12; int stateHighs[NUM_STATES][NUM_MONTHS];

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10][11]
[0] [1] [2] . row 2, . col 7 Arizonas . high for [48] August [49]
6/23/2012

66 64 72 78 85 90 99 105 98 90 88 80
stateHighs[2][7]

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

25

Summary
1.Array is collection of "same type" data 2.Indexed variables of array used just like any other variables 3.for-loop "natural" way to traverse arrays 4.Programmer is responsible for staying "in bounds" of array 5.Array parameter is "new" kind similar to call-by-reference

Another innovative way to check the quality of your coding!!!!

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

27

THANK YOU
6/23/2012 ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION

DONE BY:SHUBHAM TRIVEDI XII A 12033

6/23/2012

ARRAYS SEMINAR A SHUBHAM TRIVEDI PRODUCTION ALL RIGHTS RESERVED

29

Anda mungkin juga menyukai