Array Declaration - Lecture Slides | ECE 206, Study notes of Electrical and Electronics Engineering

Material Type: Notes; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-ewq
koofers-user-ewq 🇺🇸

5

(1)

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
ECE206 - Programming
Lecture 7 – Array
09/20/07
2
What is an array?
An array is a collection of data elements or
objects of the same type.
Arrays are “static” entities because they
remain the same size throughout program
execution.
When declaring an array, a consecutive
group of memory is allocated
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Array Declaration - Lecture Slides | ECE 206 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ECE206 - Programming

Lecture 7 – Array

2

What is an array?

† An array is a collection of data elements or

objects of the same type.

† Arrays are “static” entities because they

remain the same size throughout program

execution.

† When declaring an array, a consecutive

group of memory is allocated

3

Array declaration

† Example: To declare a one-

dimensional array with 10 double

variables, the following statement

is used:

double vector[10];

where a contiguous space for 10

double variables is allocated in the

memory by compiler

† Array-size must be an integer

constant

type array-name[array-size];

vector[0] 5. vector[1] 11. vector[2] -5. vector[3] 0. vector[4] 3. vector[5] 5. vector[6] 5. vector[7] 100. vector[8] -0. vector[9] 35.

4

Array name

† Name of an array indicates the address of

the memory which the array is stored, it is a constant pointer // Printing the memory location of an array #include using namespace std;

int main() { int a[] = {11, 22, 33}; cout << "a = " << a << endl;

return 0; }

7

Multidimensional arrays

† More than one subscript can

be used to reference an array element. A 3x2 matrix with 3 rows and 2 columns can be declared as:

double matrix[3][2];

† Space is allocated

contiguously, with the rightmost subscript incrementing the fastest

matrix[0][0] 0. matrix[0][1] 1. matrix[1][0] 1. matrix[1][1] 2. matrix[2][0] 2. matrix[2][1] 3.

8

2D array initialization

† Array initialization is like:

double matrix[3][2] =

† Without enough initializer

int a[2][3] = {{1},{3,4,5}};

int b[2][3] = {1,2,3,4,5};

int c[2][3] = {{1,2},{3}};v

9

Example: uninitialized array

// initializing an array with a declaration #include using namespace std;

int main() { int a[ 7 ] = { 3, 7, 6, 1 }; int b[ 7 ]; // an uninitialized array static int c[7];

for ( int i = 0; i < 7; i++ ) cout << a[i] << " " << b[i] << " " << c[i] << endl;

return 0; }

† Static arrays are initialized when the program is loaded, and are initialized to zero by default. Auto arrays are not.

10

Example: array operation (1D)

// Compute the average of the array #include using namespace std;

int main() { int a[] = {13, 5, 16, 45, 66, 23}; double total = 0, average; size = sizeof(a) / sizeof(int);

for ( int i = 0; i < size; i++ ) total += a[i];

average = total/size; cout << "The average is " << average << endl;

return 0;

13

Char arrays and strings

† Char arrays can hold strings.

† A string is stored as an array of characters

terminated with a NULL character (value of zero).

† There are two ways to initialize char arrays with

strings

// the NULL character is automatically inserted char str1[] = “Hello!\n”; // an 8-element array

//explicit NULL (8-element array): char str2[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\n’, ‘\0’};

† Pay attention that string uses double quote “”, while

character uses single quote ‘’.

14

Char array input and output

† cin >> can be used to input a char

array from keyboard, stop at space

† cout << can be used to output a char

array to screen

† What about other data type?

15

Passing arrays to functions

† Arrays can be passed as arguments

to functions

† C++ automatically passes arrays to

functions using simulated call-by-

reference.

† How to prevent called functions from

changing array values?

16

Parameter passing

† Call by value

„ For small objects that should not be altered by the function

† Call by reference

„ For large objects that may be altered by the function and the copy would be too expensive

† Call by constant reference

„ For large objects that should not be altered by the function

19

Array cannot be assigned

int a[5] = {1, 2, 3}; int b[5]; b = a;

20

Sort an array - Bubble sort

† Bubble sort uses repeated comparison to exchange adjacent elements in the array if they are not in order

† In this process, small values move toward one end and large ones toward the other end, that’s why it’s also called sinking sort

† Simplest, but

† the most inefficient

21

Bubble sort thinking

Bubble Sort Thinking (excerpt)

There is a famous computer sort algorithm called the bubble sort. It is the most natural, most often tried sort in the history of computing. It is absolutely obvious.

And it is absolutely abysmal because it is the slowest sort known to science.

It isn't known why the bubble sort is so obvious to the human mentality. But every other sort known to computer science is faster. You name it: the insertion sort, shell sort, quicksort, heapsort are all better and faster. Press, Teukolsky et al. in their book Numerical Recipes say: "We will draw the line at the inefficient N -squared algorithm, beloved of elementary computer science texts called bubble sort. If you know what the bubble sort is, wipe it from your mind; if you don't know, make a point of never finding out!"

http://www.macopinion.com/columns/utopia/archive/up-14.html

22

Bubble sort - pseudocode

† Search for adjacent pairs that are out

of order.

† Switch the out-of-order keys.

† Repeat this n-1 times.

† After the first iteration, the last key is

guaranteed to be the largest.

† If no switches are done in an

iteration, we can stop.