Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.

Similar presentations


Presentation on theme: "Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming."— Presentation transcript:

1 Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming for different purposes such as sorting, searching and etc.  Arrays allow you to store a group of data of a single type.  Arrays are efficient and useful for performing operations. You can use them to store a set of high scores in a video game, a 2 dimensional map layout, or store the coordinates of a multi-dimensional matrix for linear algebra calculations.

2 Arrays  Arrays are of two types single dimension array and multi-dimension array.  Each of these array type can be of either static array or dynamic array.  Static arrays have their sizes declared from the start and the size cannot be changed after declaration.  Dynamic arrays that allow you to dynamically change their size at runtime, but they require more advanced techniques such as pointers and memory allocation

3 Arrays A single dimension array is represented be a single column. A multiple dimensional array would span out n columns by n rows. Single Dimension Arrays Declaring Single Dimension Arrays Arrays can be declared using any of the data types available in C. Array size must be declared using constant value before initialization. A single dimensional array will be useful for simple grouping of data that is relatively small in size. You can declare a single dimensional array as follows:

4 Arrays Array - a collection of a fixed number of components wherein all of the components have the same data type One-dimensional array - an array in which the components are arranged in a list form The general form of declaring a one-dimensional array is: dataType arrayName[intExp]; where intExp is any expression(width) that evaluates to a positive integer

5 Declaring an array The statement int num[5]; declares an array num of 5 components of the type int The components are num[0], num[1], num[2], num[3], and num[4]

6

7 Accessing Array Components The general form (syntax) of accessing an array component is: arrayName[indexExp] where indexExp, called index, is any expression whose value is a nonnegative integer Index value specifies the position of the component in the array The [] operator is called the array subscripting operator The array index always starts at 0

8

9

10

11 Arrays In C language the end of string is marked by the null character ''. Hence to store a group of 3 possible string data. We declare the array as char game_map[4]; This applies for char type array. One-dimensional string array containing 3 elements. Array Element game_map[0] game_map[1] game_map[2 ] game_map[3] DataSR D One-dimensional integer array containing 3 elements.

12 Initializing Single Dimension Arrays Array can be initialized in two ways, initializing on declaration or initialized by assignment. Initializing on Declaration If you know the values you want in the array at declaration time, you can initialize an array as follows: Syntax: Sample Code array_name[size_of_array] = {element 1, element 2,...}; Example: Sample Code char game_map[3] = {'S', 'R', 'D'}; This line of code creates an array of 3 chars containing the values 'S', 'R ', and 'D'

13 Initialized by Assignment Sample Code char game_map[3]; game_map[0] = 'S'; game_map[1] = 'R'; game_map[2] = 'D'; Accessing Single Dimension Array Elements Arrays are 0-indexed, so the first array element is at index = 0, and the highest is size_of_array – 1. To access an array element at a given index you would use the following syntax: array_name[index_of_element];

14 Example: #include void main(void) { int a[5]; int i; for(i = 0;i<5;i++) { a[i]=i; } for(i = 0;i<5;i++) { printf(“ value of I is = %d\n",i,a[i]); } } Output 1 value of I is = 0 2 value of I is = 1 3 value of I is = 2 4 value of I is = 3 5 value of I is = 4

15 Program to calculate sum of given 10 numbers Include Main() { int i, sum, m ; int s[100]; printf(“how many numbers”); scanf(“%d”,&m); printf(“enter the numbers\n”); for(i=0; i<=m; ++i) { scanf(“%d”, &s[i]); } Sum=0; for(i=0; i<=m; ++i) { sum = sum+s[i]; } printf(“the sum of the number is %d”, sum); getch(); }

16 Now we are going to learn how we can use two dimensional arrays (2D arrays) to store values. Because it is a 2D array so its structure will be different from one dimension array. The 2D array is also known as Matrix or Table, it is an array of array. See the below image, here each row is an array. Declaration of 2D array: Syntax: data_type array_name[row_size][column_size]; Example: int arr[3][3]; So the above example declares a 2D array of integer type. This integer array has been named arr and it can hold up to 9 elements (3 rows x 3 columns).

17 2D Array Memory Map of 2D Array

18 Program prints the elements of 2*2 matrix Main() { int a[20] [20],i,j; a[1] [1]=2;a[1] [2]=3; a[2] [1] =4; a[2] [2]=6; for (i=1;i<=2; ++i) { for(j=1;j<=2;++i) { Printf(“%d”,a[i][j]); } Printf(“\n”); }

19 Sum Main() { int a[20] [20],sum int i,j,m,n; printf(“enter the order m*n of the matrix\n”); scanf (“%d %d”, &m, &n); printf (“enter the elements of the matrix\n”); for(i=1;i<=m;++i) { for( j=1; j<=n; ++j) scanf(“%d”, &a[1][j]); } Sum = 0; For(i=1;i<=m; ++i) { for(j=1;j<=n,++j) sum += a [i] [j]; } Printf(“ sum of all the elements is %d”, sum); Getch(); }


Download ppt "Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming."

Similar presentations


Ads by Google