Date post: | 23-Dec-2015 |
Category: |
Documents |
Upload: | elvin-obrien |
View: | 217 times |
Download: | 0 times |
C programming---Arrays
scalar: capable of holding a single data itemaggregate variables: capable of holding a collections of values.
Two kinds of aggregates in C:ArraysStructures
An array is a data structure containing a number of data values, all of which have the same type.
Arrays
One-Dimensional ArraysThe simplest kind of arraysThe 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];
Arrays Subscripting
Array elements are always numbered starting from 0, so the elements of an array of length nare 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 LoopArrays 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 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;
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++];
Array InitializationAlways 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} */
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 Arraysint m[5][9]; // 5 rows 9 columns
0
1
2
3
4
0 1 2 3 4 5 6 7 8
M[i, j] != M[i][j]M[i, j] == M[j]
How Multidimensional Array Stored in Memory
#define N 10double 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}, {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}
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.
Defining and Calling Functionsdouble 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 Functionsreturn-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 **/
Function DeclarationsDeclare your functions before you use them
double average(double x, double y){ ………..}
int main(){ ……….. average(x, y);}
Functions Declarationsdouble 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 that’s been initialized to the value of the matching argument
Argumentsint 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 **/ …….}
Program TerminationUsing return in main is one way to exit the programCalling exit has the same meaning
exit belongs to <stdlib.h>exit(0);exit(EXIT_SUCCESS);exit(EXIT_FAILURE);
RecursionA function is recursive if it calls itself.int fact(int n){ if(n <= 1) return 1; else return n * fact(n-1);}