- Prof. Amr Goneid, AUC
1
CSCE 110 PROGRAMMING FUNDAMENTALS
WITH C++
- Prof. Amr Goneid
WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. - - PowerPoint PPT Presentation
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1 Arrays Prof. Amr Goneid, AUC 2 1-D Arrays Data Structures The Array Data Type How to Declare an Array
1
2
3
4
5
6
The array as a whole is referenced through the
7
8
9
Size = No. of Elements = n UB = n - 1 Number of Bytes = size * Element size. All
Maximum size of 64 Kbytes.
250 elements = 500 bytes 26 elements = 104 bytes
10
11
12
13
14
15 12
16 12
C++ will not flag an error if the array subscript
Ex:
Beware!
17
Retrieve an element:
Update an element:
Input an element of an array:
Output an element of an array:
18
19
20
A function to input n elements of an integer array and
21
22
A function to receive an integer array and return the
23
A function to receive a real array and return the
24
Function1:
Computing the Average and Standard Deviation of a list of numbers.
Function2:
Linear (Sequential) Search in an array.
Function3:
Sorting an array using Selection Sort.
Function4:
Sorting an array using Bubble Sort
Function5:
Searching an array using Binary Search
25
The average (av) of array x[0..n-1] is computed as the sum of all elements divided by n. The variance is defined as the average of the squared deviations from the average value. The Standard Deviation is the square root of the variance.
n i i n i i
− = − =
2 1 1
26
27
28
29
30
31
4 3 1 6 2 5 1 3 4 6 2 5 1 2 4 6 3 5 1 2 3 4 6 5 1 2 3 6 4 5 1 2 3 4 5 6
32
33
The general idea is to compare adjacent elements
Assume elements to be in locations 0..n-1 Let (i) be the index of the last element in a sub-
34
for each i = n-1…1
35
4 3 1 2 3 4 1 2 3 1 4 2 1 3 2 4 3 1 2 4 1 2 3 4
36
void bubbleSort (itemType a[ ], int n) { int i , j; bool swapped; for (i = n; --i >= 0; ) { swapped = false; for (j = 0; j < i; j++) { if (a[j] > a[j+1] ) { swap(a[j] , a[j+1]); swapped = true; } } if (!swapped) return; } }
37
38
Assume elements in locations a[1]..a[n] to be already sorted in ascending order
put the key (x) we are searching for in location a[0]. Set Low index L=1 and High index H=n Let (m) be the index of the approximate middle between L and H do
compute middle location (m) if (L > H) there is no hope to find (x) in the array, so set m = 0 else if x < a[m] discard a[m]..a[H] by setting H = m-1, else if x > a[m] discard a[1]..a[m] by setting L = m+1, else, x is found at a[m] while x is not yet found at a[m]
return the location (m), if zero it is not found
39
int binsearch (itemType a[ ], int n, itemType x) { int L , m , H; a[0] = x; L = 1; H = n; do { m = (L + H)/2; if (L > H) m = 0; else if (x < a[m]) H = m-1; else L = m+1; } while (a[m] != x); return m; }
40
41
42
City
Cairo Tanta Alex Day Fri Sat Sun
i j
43
Zoomed showing Pixels
44
Row# 0 .. NROWS Column# 0 .. NCOLS
45
46
47
In 1-D arrays, it is not necessary to specify the size:
This will not work, because 2-D arrays are stored as
Hence, beside the base address (name) we must
48
// FILE: SumArray.cpp // CALCULATES THE SUM OF THE ELEMENTS IN THE // FIRST rows ROWS OF AN ARRAY OF FLOATING POINT // VALUES WITH NCOLS (A CONSTANT) COLUMNS. // Pre: The type int constant NCOLS is // defined (NCOLS > 0) and the // array element values are defined and rows > 0. // Post: sum is the sum of all array element // values. // Returns: Sum of all values stored in the // array.
49
float sumArray (float a[ ][NCOLS], int rows) { float sum = 0.0; // Add each array element value to sum. for (int r = 0; r < rows; r++) for (int c = 0; c < NCOLS; c++) sum += a[r][c]; return sum; }
50
51
52
53
54
55
56
57
Image A has random gray levels between 0(Black) and
255(White). What is the algorithm used to convert Image A to the binary image B? Image A Image B
58
59
60
61