240-222 cpt: arrays/51 240-222 computer programming techniques semester 1, 1998 objective of these...

41
240-222 CPT: Arrays/5 240-222 Computer Programming 240-222 Computer Programming Techniques Techniques Semester 1, 1998 Semester 1, 1998 Objective of these slides: to introduce arrays 5. Arrays ch. 6

Upload: cornelia-maria-hart

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 1

240-222 Computer Programming Techniques240-222 Computer Programming TechniquesSemester 1, 1998Semester 1, 1998

Objective of these slides:– to introduce arrays

5. Arraysch. 6

Page 2: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 2

OutlineOutline1. What is an Array?

2. Declaring an Array

3. Accessing an Array

4. An Array Program

5. Initialising Arrays

6. Avoid 'Magic' Numbers

7. Student Polls Example

8. Functions Using Arrays

9. Two Examples

10. 2D Arrays

11. 3D Arrays

Page 3: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 3

1. What is an Array?1. What is an Array?

An array is a data structure which can hold a sequence of values.

The array c[] holds 3 integer values:

c[0]

c[1]

c[2]

-45

6

72

Page 4: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 4

2. Declaring an Array2. Declaring an Array

int c[3];

The array is comprised of 3 elements:

c[0] c[1] c[2]

Page 5: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 5

int b[100], x[27];char sent[125];

b[] holds 100 integers (in b[0] to b[99])

x[] holds 27 integers (in x[0] to x[26])

sent[] holds 125 characters (in sent[0] to sent[124])

Page 6: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 6

3. Accessing an Array3. Accessing an Array

int c[3], s, t, v;

c[1] = 3;s = 0; c[s] = 3;c[s + 1] = 2;t = 2; c[s + t] += 2;printf("%d", c[0] + c[1]);v = c[2] / 3;

Page 7: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 7

4. An Array Program 4. An Array Program Fig. 6.3Fig. 6.3

#include <stdio.h>

int main(){ int n[10], i;

for (i = 0; i < 10; i++) n[i] = 0;

printf("%s%13s\n", "Element", "Value"); for(i = 0; i < 10; i++) printf("%7d%13d\n", i, n[i]); return 0;}

notice the range

Page 8: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 8

Output:Output:

Element Value0 01 02 0: :9 0

Page 9: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 9

5. Initialising Arrays5. Initialising Arrays

int n[5] = {32, 23, 34, 1, 4};

int p[5] = {3};

int q[] = {32, 23, 34, 1, 4};

int r[3] = {1, 2, 3, 4, 5};/* WRONG */

Page 10: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 10

6. Avoid 'Magic' Numbers6. Avoid 'Magic' Numbers

/* Initialize array s to the even integers from 2 to 20 */#include <stdio.h>#define SIZE 10

int main(){ int s[SIZE], j; for (j = 0; j < SIZE; j++) s[j] = 2 + 2 * j; printf("%s%13s\n", "Element", "Value"); for (j = 0; j < SIZE; j++) printf("%7d%13d\n", j, s[j]); return 0;}

Typical rangeexpression

Page 11: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 11

Output:Output:

Element Value0 21 42 6: :9 20

Page 12: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 12

7. Student Polls Example fig. 6.77. Student Polls Example fig. 6.7

/* How many students got each mark? */#include <stdio.h>#define SIZE1 40 /* no of students */#define SIZE2 11 /* ans range (0 - 10) */

int main(){ int answer, rating; int responses[SIZE1] = {1, 2, 6, 4, 8, 5,

9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3,8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10};

int frequency[SIZE2] = {0};:

continued

Page 13: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 13

for(ans = 0; ans < SIZE1; ans++) frequency[responses[ans]]++;

printf("%s%17s\n", "Rating", "Freq");for(rating = 0; rating < SIZE2; rating++) printf("%6d%17d\n", rating,

frequency[rating]);

return 0;

}

Page 14: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 14

Consider:Consider:

for(ans = 0; ans < SIZE1; ans++) frequency[responses[ans]]++;

When responses[ans] is 1 then:frequency[1]++;

When responses[ans] is 7 then:frequency[7]++;

What happens if responses[ans] has a value >= 11 or < 0?

Page 15: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 15

8. Functions using Arrays Fig 6.138. Functions using Arrays Fig 6.13

/* A program which illustrates the differences between passing an array to a function and passing an array element */

#include <stdio.h>#define SIZE 5

void modifyarray(int b[], int size);void modifyelement(int e);

:

continued

Page 16: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 16

int main(){ int a[SIZE] = {0, 1, 2, 3, 4}; int i;

printf("The values of the originalarray are:\n");

for (i = 0; i < SIZE; i++) printf("%3d", a[i]); printf("\n");

::

continued

Page 17: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 17

modifyarray(a, SIZE);

printf("The values of the modified array are:\n");

for (i = 0; i < SIZE; i++) printf("%3d", a[i]); printf("\n");

printf("The value of a[3] is %d\n",a[3]); modifyelement(a[3]); printf("The value of a[3] is %d\n",a[3]);

return 0;}

continued

Page 18: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 18

void modifyarray(int b[], int size){ int j;

for (j = 0; j < size; j++) b[j] *= 2;}

void modifyelement(int e){ printf("Value in modifyelement

is %d\n", e *= 2);}

Page 19: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 19

Passing Arrays to FunctionsPassing Arrays to Functions

The call to the function modifyarray() is:modifyarray(a, SIZE);

C treats array arguments differently .

Any changes to the elements of a[]will be retained when the function returns.

This is call by reference.

Page 20: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 20

Output:Output:

The values of the original array are:0 1 2 3 4

The values of the modified array are:0 2 4 6 8

The value of a[3] is 6Value in modifyelement is 12The value of a[3] is 6

Page 21: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 21

9. Two Examples9. Two Examples

9.1 Reverse Input Text

9.2 Capitalise Input Text

9.3 Reverse & Capitalise

Page 22: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 22

9.1. Reverse Input Text9.1. Reverse Input Text/* Reverse lines */#include <stdio.h>#include <string.h>#define SIZE 200

void rev_print(char ln[]);

int main(){ char line[SIZE];

while (gets(line) != NULL) rev_print(line); return 0;}

continued

Page 23: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 23

void rev_print(char ln[])/* Find the length of the line and then use a for-loop to print it backwards */{ int i;

for(i=strlen(ln)-1; i >=0; i--) putchar(ln[i]); putchar('\n');}

Page 24: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 24

Use:Use:

$ revhello worlddlrow ollehabbaabba$

$ rev < test > rev_test

output fromprogram

Page 25: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 25

9.2. Capitalise Input Text9.2. Capitalise Input Text/* Make lines uppercase */#include <stdio.h>#include <ctype.h>#define SIZE 200

void upper(char ln[]);

int main(){ char line[SIZE]; while (gets(line) != NULL) { upper(line); puts(line); } return 0;}

continued

Page 26: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 26

void upper(char ln[])/* Convert every lowercase char to uppercase */{ int count = 0;

while (ln[count] != '\0') { if (islower(ln[count])) ln[count] = toupper(ln[count]); count++; }}

Page 27: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 27

Use:Use:

$ uphello worldHELLO WORLD4 abbas4 ABBAS$

$ up < test > up_test

output fromprogram

Page 28: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 28

9.3. Reverse & Capitalise9.3. Reverse & Capitalise

$ rev < rev.c | up > rev-up

$ cat rev-up/* DESREVER TI TNIRP DNA ENIL A DAER YLDETAEPER */

>H.OIDTS< EDULCNI#>H.GNIRTS< EDULCNI#

4201 EZIS ENIFED#

;)][NL RAHC(TNIRP_VER DIOV::}

Page 29: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 29

10. Two-dimensional Arrays10. Two-dimensional Arrays

10.1. Declaring 2D Arrays

10.2. 2D Array Initialisation

10.3. Using a 2D Array

Page 30: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 30

10.1. Declaring 2D Arrays10.1. Declaring 2D Arrays

int b[3][4]

The first subscript is the number of rows (3), the second subscript is the number of columns (4).

Row 0

Row 1

Row 2

Col 0 Col 1 Col 2 Col 3

b[0][0] b[0][1] b[0][2] b[0][3]

b[1][0] b[1][1] b[1][2] b[1][3]

b[2][0] b[2][1] b[2][2] b[2][3]

Page 31: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 31

10.2. 2D Array Initialisation10.2. 2D Array Initialisation

Many alternatives:

int a[2][3] = {{1,2,3}, {4,5,6}}; – Indexing is by rows,

so a[0] row initialised with {1,2,3}.

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

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

Page 32: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 32

Initialisation of partsInitialisation of parts

int b[2][2] = {{1}, {3,4}};– b[0][1] is assigned 0

b[0][0] = 1

b[1][0] = 3, b[1][1] = 4

Page 33: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 33

Initialisation with codeInitialisation with code

int a[3][4];:

for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) a[row][col] = row * col;

Page 34: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 34

10.3. Using a 2D Array10.3. Using a 2D Array

#include <stdio.h>

int sum(int a[][]);

int main(){ int scores[3][5] = {0}; int tot;

: /* scores gets some values */ tot = sum(scores); printf("Total score is %d\n", tot); return 0;}

continued

Page 35: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 35

int sum(int a[][5])/* sum all the elements of the array */{ int i, j, sum = 0;

for (i = 0; i < 3; ++i) for (j = 0; j < 5; ++j) sum += a[i][j]; return sum;}

Page 36: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 36

11. Three-dimensional Arrays11. Three-dimensional Arrays

11.1. Declaring 3D Arrays

11.2. 3D Array Initialisation

11.3. Using a 3D Array

Page 37: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 37

11.1. Declaraing 3D Arrays11.1. Declaraing 3D Arrays

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

The first inner brackets refer to the first subscript. The next inner brackets refer to the second

subscript. The three values inside each of the inner brackets

refer to final subscript.

Page 38: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 38

a[0][1][2] = 2a[0][1][2] = 2

X

0

1

Y0 1

Z

0

1

2

Page 39: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 39

11.2. 3D Array Initialisation11.2. 3D Array Initialisation

int a[2][2][3] = {0};/* everything to 0 */

Using code:int a[3][4][5];

:for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) for (dep = 0; dep < 5; dep++) a[row][col][dep] = row*col*dep;

Page 40: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 40

11.3. Using a 3D Array11.3. Using a 3D Array

#include <stdio.h>

int sum_coords(int a[][][]);

int main(){ int orbit[10][20][30] = {0}; int points; : /* initialise orbit array */ points = sum_coords(orbit); printf(" Orbit points sum: %d\n",

points); return 0;}

continued

Page 41: 240-222 CPT: Arrays/51 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce arrays 5. Arrays ch. 6

240-222 CPT: Arrays/5 41

int sum_coords(int a[][20][30])/* sum the number of points in the array */{ int i, j, k, sum = 0; for (i = 0; i < 10; ++i) for (j = 0; j < 20; ++j) for (k = 0; k < 30; ++k) sum += a[i][j][k]; return sum;}