Presentation is loading. Please wait.

Presentation is loading. Please wait.

C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.

Similar presentations


Presentation on theme: "C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates."— Presentation transcript:

1 C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates in C: Arrays Structures An array is a data structure containing a number of data values, all of which have the same type.

2 Arrays One-Dimensional Arrays The simplest kind of arrays The elements, the values of the items, of a one- dimensional array are conceptually arranged one after another in a single row. int a[8]; a #define N 10 …… int a[N];

3 Arrays Subscripting Array elements are always numbered starting from 0, so the elements of an array of length n are indexed from 0 to n-1 a[0]a[1] a[8-1] a[1] = 9; printf(“%d\n”, a[5]); ++a[i];

4 Arrays and For Loop Arrays and for loops go hand-in-hand Idioms: for(i = 0; i < N; i++) a[i] = 0; for(i = 0; i < N; i++) scanf(“%d”, &a[i]); for(i = 0; i < N; i++) sum += a[i];

5 Arrays and Its Indexing C doesn’t require that subscript bounds be checked. If a subscript goes out of range, the program’s behavior is undefined. int a[10], i; for(i = 1; i <= 10; i++) a[i] = 0;

6 Array subscript may be any integer expression a[i+j*10] i = 0; while(i < N) a[i++] = 0; i = 0; while(i < N) a[i] = b[i++];

7 An Example reverse.c

8 Array Initialization Always initialize array when you declare it int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int a[10] = {1, 2, 3, 4, 5, 6}; /* {1, 2, 3, 4, 5, 0, 0, 0, 0, 0} */ int a[10] = {0}; /* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} */ int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; /* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} */

9 An Example repdigit.c

10 Using the sizeof Operater with arrays The sizeof operator can determine the size of an array (in bytes). int a[10]; sizeof(a) = 40 (assuming each integer requires 4bytes) Measure the size of an array: Sizeof(a) / sizeof(a[0]); #define SIZE ((int) (sizeof(a) / sizeof(a[0])))

11 Multidimensional Arrays int m[5][9]; // 5 rows 9 columns 0 1 2 3 4 012345678 M[i, j] != M[i][j] M[i, j] == M[j]

12 How Multidimensional Array Stored in Memory #define N 10 double ident[N][N]; int row, col; for(row = 0; row < N; row++) for(col = 0; col < N; col++) if(row == col) ident[row][col] = 1.0; else ident[row][col] = 0.0;

13 Initializing a Multidimensional Array int m[5][9] = { {1,1,1,1,1,1,1,1,1}, {0,1,0,0,0,1,1,0,1}, {0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1}, {1,0,1,0,1,0,1,0,1}} int m[5][9] = { {1,1,1,1,1,1,1,1,1}, {0,1,0,0,0,1,1,0,1}, {1,0,1,0,1,0,1,0,1}} int m[5][9] = { {1,1,1,1,1,1,1,1,1}, {0,1,0,0,0,1,1,0}, {0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1}, {1,0,1,0,1,0,1,0,1}} int m[5][9] = { 1,1,1,1,1,1,1,1,1, 0,1,0,0,0,1,1,0,1, 0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1, 1,0,1,0,1,0,1,0,1}

14 Functions Functions are the building blocks of C programs. Each function is essentially a small program, with its own declarations and statements. Using functions, programs are divided in to small pieces that are easier for us as well as others to understand and modify. Functions can take some of the tedium out of programming by allowing us to avoid duplicating code that’s used more than once.

15 Defining and Calling Functions double average(double a, double b) { return (a + b) / 2; } avg = average(x/2, y*2); printf(“Average: %g\n”, average(x, y));

16 Defining and Calling Functions return-type function-name ( parameters ) { declarations statements } Functions may not return arrays; Specifying that the return type is void indicates that the function doesn’t return a value double average(double a, b) /** WRONG **/

17 An Example prime.c

18 Function Declarations Declare your functions before you use them double average(double x, double y) { ……….. } int main() { ……….. average(x, y); }

19 Functions Declarations double average(double x, double y); int main() { ……….. average(x, y); } double average(double x, double y) { ……….. }

20 Arguments Parameters appear in function definitions: they are dummy names that represent values to be supplied when the function is called Arguments are expressions that appear in function calls.

21 Arguments --- passed by value When a function is called, each argument is evaluated and its value assigned to the corresponding parameter. Each parameter behaves like a variable that’s been initialized to the value of the matching argument

22 Arguments int power(int x, int n) { int i, result = 1; for (i = 1; i <= n; i++) result = result * x; return result; } int power(int x, int n) { int result = 1; while(n-- > 0) result *= x; return result; }

23 Array Arguments int f(int a[]) { …… } int f(int a[]) { int len = sizeof(a) / sizeof(a[0]); /** WRONG **/ ……. }

24 Array Arguments int f(int a[], int n) { …… }

25 The return Statement return expression; return 0; return status; return n >= 0 ? n : 0 return;

26 Program Termination Using return in main is one way to exit the program Calling exit has the same meaning exit belongs to exit(0); exit(EXIT_SUCCESS); exit(EXIT_FAILURE);

27 Recursion A function is recursive if it calls itself. int fact(int n) { if(n <= 1) return 1; else return n * fact(n-1); }

28 An Example --- Quicksort


Download ppt "C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates."

Similar presentations


Ads by Google