1 / 35

# ARRAYS - PowerPoint PPT Presentation

ARRAYS. Lecture 15 12.2.2002. Arrays as parameters of functions. An array passed as a parameter is not copied. An array name is a constant whose value serves as a reference to the first (index 0) item in the array. Arrays as parameters of functions.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' ARRAYS' - maia

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### ARRAYS

Lecture 15

12.2.2002.

• An array passed as a parameter is not copied.

• An array name is a constant whose value serves as a reference to the first (index 0) item in the array.

• Since constants cannot be changed, assignments to array variables are illegal.

• Only the array name is passed as the value of a parameter, but the name can be used to change the array’s contents.

• Empty brackets [] are used to indicate that the parameter is an array. The no of elements allocated for the storage associated with the array parameter does not need to be part of the array parameter.

int main () {

int numbers[MAXS];

int k, size = 10;

for (k=0; k<size; k++) {

numbers[k] = k+1;

}

printf (“Before : \n”) ;

printarray (numbers, size) ;

change (numbers, size) ;

printf (“After : \n”) ;

printarray (numbers, size) ;

}

int k;

for (k=1; k<num; k++) {

list[k] += list[k-1] ;

}

}

void printarray (int list[], int num) {

int k;

for (k=0; k<num; k++) {

printf (“%d “, list[k]) ;

}

printf (“\n”) ;

}

Before:

1 2 3 4 5 6 7 8 9 10

After:

1 3 6 10 15 21 28 36 45 55

#define MAXS 100

int insert (int[], int, int, int) ;

int delete (int[], int, int) ;

int getelement (int[], int, int) ;

main () {

int a[MAXS];

int size;

size = readarray (a, 10) ;

size = insert (a, size, 4, 7) ;

x = getelement (a, size, 3) ;

size = delete (a, size, 3) ;

}

int readarray (int x[], int size) {

int i;

for (i=0; i<size; i++)

scanf(“%d”, &x[i]) ;

return size;

}

int insert (int x[], int size, int pos. int val){

for (k=size; k>pos; k--)

x[k] = x[k-1] ;

x[pos] = val ;

return size+1;

}

int getelement (int x[], int size, int pos){

if (pos <size) return x[pos] ;

return -1;

}

int size) {

}

int findmax (int x[], int size) {

}

int i;

for (i=0; i< (size/2); i++)

temp = x[size-i-1] ;

x[size-1-1] = x[i] ;

x[i] = temp;

}

int findmax (int x[], int size) {

int i, max;

max = x[0];

for (i=1; i< size; i++)

if (x[i] > max)

max = x[i] ;

return max;

}

• Strings are 1-dimensional arrays of type char.

• By convention, a string in C is terminated by the end-of-string sentinel \0, or null character.

• String constant : “abc” is a character array of size 4, with the last element being the null chaaracter \0.

• char s[] = “abc” ;

a

b

c

\0

### Searching an Array:Linear and Binary Search

• Check if a given element (key) occurs in the array.

• If the array is unsorted :

• start at the beginning of the array

• inspect every element to see if it matches the key

/* If key appears in a[0..size-1], return its location, pos, s.t. a[pos] == key. If key is not found, return -1 */

int search (int a[], int size, int key) {

int pos = 0;

while (pos < size && a[pos] != key)

pos++;

if (pos<n)

return pos;

return -1;

}

int x= {12,-3, 78,67,6,50,19,10} ;

Trace the following calls :

search (x, 8,6) ;

search (x,8,5) ;

• Binary search works if the array is sorted

• Look for the target in the middle

• If you don’t find it, you can ignore half of the array, and repeat the process with the other half.

• What we want : Find split betwen values larger and smaller than x :

0

L

R

x:

n

<=key

>key

• Situation while searching :

0

L

R

n

x:

<=key

?

>key

• Step : Look at [(L+R)/2]. Move L or R to the middle depending on test.

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */

int binsearch (int x[], int size, int key) {

int L, R, mid;

______________________;

while (_________________) {

}

____________________ ;

}

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */

int binsearch (int x[], int size, int key) {

int L, R, mid;

______________________;

while (_________________) {

mid = (L+R)/2;

if (x[mid] <= key)

L = mid;

else R = mid;

}

____________________ ;

}

mid = (L+R)/2;

if (x[mid] <= key)

L = mid;

else R = mid;

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */

int binsearch (int x[], int size, int key) {

int L, R, mid;

______________________;

while (_________________) {

mid = (L+R)/2;

if (x[mid] <= key)

L = mid;

else R = mid;

}

____________________ ;

}

L+1 != R

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */

int binsearch (int x[], int size, int key) {

int L, R, mid;

______________________;

while ( L+1 != R) {

mid = (L+R)/2;

if (x[mid] <= key)

L = mid;

else R = mid;

}

}

if (L>=0 && x[L]==key) return L;

else return -1;

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */

int binsearch (int x[], int size, int key) {

int L, R, mid;

______________________;

while ( L+1 != R) {

mid = (L+R)/2;

if (x[mid] <= key)

L = mid;

else R = mid;

}

if (L>=0 && x[L]==key) return L;

else return -1;

}

L=-1; R=size;

-17 -5 3 6 12 21 45 63 50

Trace :

binsearch (x, 9, 3);

binsearch (x, 9, 145);

binsearch (x, 9, 45);

• Suppose you had 1000 elements

• Ordinary search (if key is a member of x) would require 500 comparisons on average.

• Binary search

• after 1st compare, left with 500 elements

• after 2nd compare, left with 250 elements

• After at most 10 steps, you are done.

• What if you had 1 million elements ?

• Given an array x[0], x[1], ... , x[size-1]

• reorder entries so that

x[0]<=x[1]<= . . . <=x[size-1]

• What we want : Data sorted in order

sorted : x[0]<=x[1]<= . . . <=x[size-1]

size

0

x:

• Initial conditions :

size

0

unsorted

x:

• General situation :

k

0

size

x:

smallest elements, sorted

remainder, unsorted

• Step :

• Find smallest element, mval, in x[k..size-1]

• Swap smallest element with x[k], then increase k.

mval

0

k

size

x:

smallest elements, sorted

/* Yield location of smallest element int[x] in x[0 .. size-1];*/

int min_loc (int x[], int , int size)

int j, pos; /* x[pos] is the smallest element found so far */

pos = k;

for (j=k+1; j<size; j++)

if (x[i] < x[pos])

pos = j;

return pos;

}

/* Sort x[0..size-1] in non-decreasing order */

int selsort (int x[], int size) {

int k, m;

for (k=0; k<size-1; k++) {

m = min_loc(x, k, size);

temp = a[k];

a[k] = a[m];

a[m] = temp;

}

}

x:

x:

3

12

-5

6

142

21

-17

45

-17

-5

3

6

12

21

142

45

x:

-17

12

-5

6

142

21

3

45

x:

-17

-5

3

6

12

21

45

142

x:

-17

-5

12

6

142

21

3

45

x:

-17

-5

3

6

142

21

12

45

x:

-17

-5

3

6

12

21

142

45

• How many steps are needed to sort n things ?

• Total number of steps proportional to n2

#define MAXN 100

void InsertSort (int list[MAXN], int size) ;

main () {

int index, size;

int numbers[MAXN];

/* Get Input */

printarray (numbers, size) ;

InsertSort (numbers, size) ;

printarray (numbers, size) ;

}

for (i=1; i<size; i++)

item = list[i] ;

for (j=i-1; (j>=0)&& (list[j] > i); j--)

list[j+1] = list[j];

list[j+1] = item ;

}

}

• Exceeding the array bounds

• int array[10];for (i=0; i<=10; i++) array[i] = 0;

• C does not support array declaratiions with variable expressions.

• void fun (int array, int size) { int temp[size] ; . . .}