Vous êtes sur la page 1sur 28

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.

Arrays
One-Dimensional Arrays
The simplest kind of arrays The elements, the values of the items, of a onedimensional array are conceptually arranged one after another in a single row.
int a[8];
a

#define N 10 int a[N];

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];

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];

Arrays and Its Indexing


C doesnt require that subscript bounds be checked. If a subscript goes out of range, the programs behavior is undefined. int a[10], i; for(i = 1; i <= 10; i++) a[i] = 0;

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++];

An Example
reverse.c

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} */

An Example
repdigit.c

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])))

Multidimensional Arrays
int m[5][9]; // 5 rows 9 columns
0 1 2 3 4 5 6 7 8

0
1 2 3 4

M[i, j] != M[i][j] M[i, j] == M[j]

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;

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,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}, {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}}

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 thats used more than once.

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));

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 doesnt return a value double average(double a, b) /** WRONG **/

An Example
prime.c

Function Declarations
Declare your functions before you use them

double average(double x, double y) { .. } int main() { .. average(x, y); }

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

double average(double x, double y) { .. }

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.

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 thats been initialized to the value of the matching argument

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; }

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

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

The return Statement


return expression; return 0; return status; return n >= 0 ? n : 0 return;

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

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

An Example --- Quicksort

Vous aimerez peut-être aussi