Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr Tripty Singh Arrays.

Similar presentations


Presentation on theme: "Dr Tripty Singh Arrays."— Presentation transcript:

1 Dr Tripty Singh Arrays

2

3 Arrays A collection of objects of the same type stored contiguously in memory under one name May be type of any kind of variable May even be collection of arrays! For ease of access to any member of array For passing to functions as a group

4 Definition of array An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list;  A two dimensional array is like a table;  The C language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

5 Declaring Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array. Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets. Dimensions used when declaring arrays in C must be positive integral constants or constant expressions. In C dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allocated only once, at the time the array is declared. The array does NOT change sizes later if the variable used to declare it changes. )

6 Arrays Arrays of structs, unions, pointers, etc., are also allowed
int A[10] An array of ten integers A[0], A[1], …, A[9] double B[20] An array of twenty long floating point numbers B[0], B[1], …, B[19] Arrays of structs, unions, pointers, etc., are also allowed Array indexes always start at zero in C

7 Arrays int C[] int D[10][20]
An array of an unknown number of integers (allowable in a parameter of a function) C[0], C[1], …, C[max-1] int D[10][20] An array of ten rows, each of which is an array of twenty integers D[0][0], D[0][1], …, D[1][0], D[1][1], …, D[9][19] Not used so often as arrays of pointers

8 "All students to receive arrays!”.
Declaring arrays Passing arrays as parameters Stepping through arrays Inspecting arrays scores :

9 Definition:– Array Index – the expression between the square brackets
Generic form:– ArrayName[integer-expression] ArrayName[integer-expression] [integer-expression] Same type as the underlying type of the array Definition:– Array Index – the expression between the square brackets

10 Examples: int i, j, intArray[ 10 ], number; float floatArray[ 1000 ]; int tableArray[ 3 ][ 5 ]; /* 3 rows by 5 columns */ const int NROWS = 100; // ( Old code would use #define NROWS 100 ) const int NCOLS = 200; // ( Old code would use #define NCOLS 200 ) float matrix[ NROWS ][ NCOLS ];

11

12 Example program for one dimensional array in C:
Output: value of arr[0] is 10 value of arr[1] is 20 value of arr[2] is 30 value of arr[3] is 40 value of arr[4] is 50

13

14

15 Enter total number of elements(1 to 100): 8 Enter Number 1: 23
Enter total number of elements(1 to 100): 8 Enter Number 1: 23.4 Enter Number 2: Enter Number 3: 50 Enter Number 4: 33.5 Enter Number 5: 55.5 Enter Number 6: 43.7 Enter Number 7: 5.7 Enter Number 8: -66.5

16

17

18

19 Example program for two dimensional array in C:
Output: value of arr[0] [0] is 10 value of arr[0] [1] is 20 value of arr[1] [0] is 30 value of arr[1] [1] is 40

20 Array Elements (End of Array)
CS-2301, B-Term 2009 Arrays in C Array Elements (End of Array) Array elements are commonly used in loops E.g., for(i=0; i < max; i++) A[i] = i*i; sum = 0; for(j=0; j < max; j++) sum += B[j]; for (count=0;rc!=EOF;count++) rc=scanf("%f", &A[count]);


Download ppt "Dr Tripty Singh Arrays."

Similar presentations


Ads by Google