Docsity

Arrays in CS1123: Declaration, Initialization, and Manipulation, Slides of Computer Programming

Partial preview of the text

Download Arrays in CS1123: Declaration, Initialization, and Manipulation and more Slides Computer Programming in PDF only on Docsity!

CS 1123Lecture 10

Fall 2011

October 21-28, 2011

Ghufran Ahmed

Arrays

•^

An array is a collection of data elements of same typein contiguous memory e.g. list of names, list of scores

-^

Easier way to compare and use data than havingseparate variables for data elements

-^

data_type

identifier[

size

]

int arr[3]

  • declares a one-dimensional array named

arr

with space

for 3 integer variables

•^

Variables contiguous in memory; lowest address hasfirst value, next address has second variable value,and so on

Arrays

•^

An array is an indexed data structure

-^

An array stores a collection of variables

-^

All variables stored in an array are of the samedata type

-^

An individual variable within an array is called an element

of the array

•^

An element of an array is accessed using the arrayname and an index

-^

The name of the array is the address of the firstelement. The index is the offset

Declaring an array

data

_

type array

_

name

[ size

];

  • allocates memory for

size

variables

  • index of first element is 0– index of last element is size-1–

size

must be a constant

Arrays

Arrays offer convenient means of groupingtogether several related variablesOne- dimensional arraystype var_name[size]int sample[10];double d[30];char ch[100];

Initializing Arrays

  • Arrays can be initialized at the time they are

declared.

Examples:

double taxrate[3]

= {0.15, 0.25, 0.3};

char word[]

= “hello”;

//word has size 6

char list[5]

= {‘h’,’e’,’l’,’l’,’o’};

//list of characters, not a string

double vector[100] = {0.0};

//assigns zero to all 100 elements

Initializing Arrays

-^

for

loops used to initialize arrays

#include

int

main() {

int

t,

sample[10];

//

this

reserves

10

integer

elements//

load

the

array

for(t=0;

t<10;

++t)

sample[t]=t; //

display

the

array

for(t=0;

t<10;

++t)

cout

<<

sample[t]

<<

'

';

return

0;

}

Initializing Arrays

int a[5];

-^

Now an indexed variable like

a[1]

can be used anywhere that a variable of type

int

can be used

int a[5];cin >> a[4] >> a[2];cout << a[2] << “ “ << a[4] << endl;cin >> a[3] >> a[0];int max; max = a[3] + a[0];

cout << max << endl;

int next;

cin >> next;

a[1]= next; cout << a[1] << endl;a[3]= 42;

cout << a[3] << endl;

int student = 2;a[student]

= 99;

cout << a[student] << “ “ << a[2] << endl;

-^

Also:

a[get_index())]

=

3;

if

get_index()

returns an

int

Initializing Arrays

•^

Useful to give a

constant

name to the

size

of array

/*

const

type modifier*/

const int NUM_EMPLOYEES = 10;int salaries[NUM_EMPLOYEES];for (int i=0; i < NUM_EMPLOYEES; i++)

cin >> salaries[i];

•^

Advantage? Why not use a variable name?

-^

Another way:

define NUM_EMPLOYEES 10

•^

And yes,

sizeof(salaries);

gives what?

Arrays

•^

Total size of an array in bytes?

-^

Total bytes =

number of bytes in type * number of elements

•^

e.g. int sample[10];

-^

Has size in bytes = 10 * sizeof(int)

Assigning One Array to Another

int a[10], b[10];// ... a = b; // error -- illegal/*So how do we make the contents of one array

same as the other?*/

Assigning One Array to Another

-^

int a[5], b[5];// ...a = b; // error – illegal

-^

Cannot assign one array to another; each element must becopied individually

const

int

ARRAY_SIZE

=

5;

int

a[]

=

{10,

11,

12,

13,

14};

int

main() {

int

b[ARRAY_SIZE]; for

(int

i=0;

i

<

ARRAY_SIZE;

++i)

b[i]

=

a[i];

return

0;

}

Summarize Arrays

•^

In C++ all arrays are stored in contiguous memorylocations

-^

An individual element is accessed by use of an index.

-^

An index describes the position of an element withinan array.

-^

In C++ all arrays have zero as the index of their firstelement.

Arrays: Min Max

int main(){

const int SIZE = 10;int i, min_value, max_value;int list[SIZE];for(i=0; i