07slide (SingleDimensionalArray)
07slide (SingleDimensionalArray)
myList[0] 5.6
myList[1] 4.5
myList[2] 3.3
myList[3] 13.2
myList[4] 4.0
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34.0
myList[7] 45.45
myList[8] 99.993
myList[9] 111.23
C++ requires that the array size used to declare an array must be a
constant expression. For example, the following code is illegal:
int size = 4;
double myList[size]; // Wrong
But it would be OK, if size is a constant as follow:
const int size = 4;
double myList[size]; // Correct
arrayName[index];
double myList[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
int main()
{ After the array is created
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 0
{ 2 0
} 4 0
int main()
{
After the array is created
int values[5];
for (int i = 1; i < 5; i++) 0 0
{ 1 0
0
values[i] = values[i] + values[i-1]; 2
3 0
} 0
4
values[0] = values[1] + values[4];
}
int main()
{
After the array is created
int values[5];
for (int i = 1; i < 5; i++) 0 0
{ 1 0
} 3 0
0
values[0] = values[1] + values[4]; 4
int main()
{ After the first iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 0
} 4 0
int main()
{
int values[5]; After the first iteration
{ 1 1
0
2
values[i] = values[i] + 3 0
values[i-1]; 4 0
}
values[0] = values[1] +
values[4];
}
{ 0 0
values[i] = values[i] + 1 1
values[i-1]; 2 0
3 0
} 0
4
values[0] = values[1] +
values[4];
}
int main()
{ After the second iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
} 4 0
int main()
{ After the second iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
} 4 0
int main()
{ After the second iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
} 4 0
int main()
{ After the third iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
} 4 0
int main()
{ After the third iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
} 4 0
int main()
{ After the third iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
} 4 0
int main()
{ After the fourth iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
} 4 10
int main()
{
int values[5];
for (int i = 1; i < 5; i++)
{ After the fourth iteration
values[i] = values[i] + values[i-1];
} 0 0
values[0] = values[1] + values[4]; 1 1
} 2 3
3 6
4 10
int main()
{
int values[5];
for (int i = 1; i < 5; i++) After the fourth iteration
{
0
values[i] = values[i] + values[i-1]; 0
1 1
} 2 3
} 4 10
int main()
{
int values[5];
for (int i = 1; i < 5; i++) 0 11
{ 1 1
} 3 6
double total = 0;
for (int i = 0; i < ARRAY_SIZE; i++)
{
total += myList[i];
}
srand(time(0));
for (int i = 0; i < ARRAY_SIZE; i++)
{
// Generate an index randomly
int index = rand() % ARRAY_SIZE;
double temp = myList[i];
myList[i] = myList[index];
myList[index] = temp;
}
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
37
Shifting Elements
AnalyzeNumbers
AnalyzeNumbers
LottoNumbers
int deck[52];
// Initialize cards
for (int i = 0; i < NUMBER_OF_CARDS; i++)
deck[i] = i;
DeckOfCards
PassArrayDemo
PassArrayDemo
EffectOfPassArrayDemo
EffectOfPassArrayDemo
ConstArrayDemo
list
newList
ReverseArray
ReverseArray
list 1 2 3 4 5 6
newList 0 0 0 0 0 0
list 1 2 3 4 5 6
newList 0 0 0 0 0 0
list 1 2 3 4 5 6
newList 0 0 0 0 0 0
list 1 2 3 4 5 6
newList 0 0 0 0 0 1
list 1 2 3 4 5 6
newList 0 0 0 0 0 1
list 1 2 3 4 5 6
newList 0 0 0 0 0 1
newList 0 0 0 0 2 1
list 1 2 3 4 5 6
newList 0 0 0 0 2 1
list 1 2 3 4 5 6
newList 0 0 0 0 2 1
list 1 2 3 4 5 6
newList 0 0 0 3 2 1
list 1 2 3 4 5 6
newList 0 0 0 3 2 1
list 1 2 3 4 5 6
newList 0 0 0 3 2 1
list 1 2 3 4 5 6
newList 0 0 4 3 2 1
list 1 2 3 4 5 6
newList 0 0 4 3 2 1
list 1 2 3 4 5 6
newList 0 0 4 3 2 1
list 1 2 3 4 5 6
newList 0 5 4 3 2 1
list 1 2 3 4 5 6
newList 0 5 4 3 2 1
list 1 2 3 4 5 6
newList 0 5 4 3 2 1
list 1 2 3 4 5 6
newList 6 5 4 3 2 1
list 1 2 3 4 5 6
newList 6 5 4 3 2 1
list 1 2 3 4 5 6
newList 6 5 4 3 2 1
to an array of characters. … … … …
… … … …
Count the occurrence of each chars[98] counts[24]
letter in the array. chars[99] counts[25]
CountLetterInArray
CountLetterInArray
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
74
animation
Linear Search Animation
liveexample.pearsoncmg.com/dsanimation/LinearSearche
Book.html
BinarySearch
Binary Search
Key List
8 1 2 3 4 6 7 8 9
8 1 2 3 4 6 7 8 9
8 1 2 3 4 6 7 8 9
key < 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high
key > 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
key < 66 list 59 60 66 69 70 79
[7] [8]
key < 59 list 59 60
low high
return –low - 1;
}
SelectionSort
SelectionSort
...
The insertion sort [0] [1] [2] [3] [4] [5] [6]
algorithm sorts a list list 2 5 9 4 Step 1: Save 4 to a temporary variable currentElement
char city[10];
cout << "Enter a city: ";
cin >> city; // read to array city
cout << "You entered " << city << endl;
CopyString
CombineString
CombineString
CompareString
CompareString
StringConversion
StringConversion