






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Fall 2007;
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







2
3
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
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
int main() { int a[] = {11, 22, 33}; cout << "a = " << a << endl;
return 0; }
7
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:
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
Array initialization is like:
Without enough initializer
9
// initializing an array with a declaration #include
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
// Compute the average of the array #include
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
// 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’};
14
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
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
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
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
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
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.