Upcoming SlideShare
×

# Arrays

342 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
342
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
25
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Arrays

1. 1. ARRAYS
2. 2. DEFINITION  Array is a data structure, which provides the facility to store a collection of data of same type under single variable name.  It is a group of consecutive memory locations having same name and type.
3. 3. Data types Fundamental integer character float Derived Array Structure Union
4. 4. Array One dimensional Multi dimensional
5. 5. DECLARATION  Syntax:  type array_name[array_size];  array_size is a positive integer constant.  type is a data type such as int, char, float, double, etc.  array_name is a valid identifier.  Example:  int num[5]; num[0] num[1] num[2] num[3] num[4]
6. 6. INITIALIZATION  int num[5]={10,20,30,40,50};  int num[]={10,20,30,40,50}; num[0] num[1] num[2] num[3] num[4] num[5] 10 20 30 40 50 NULL
7. 7. ACCESSING int num[5]={10,20,30,40,50}; num[2]++; num[4]--; Num[0] Num[1] Num[2] Num[3] Num[4] Num[5] 10 20 31 40 49 NULL
8. 8. PROGRAM USING ARRAY main() { int mark[5]={78,80,72,60,87}; int i,total=0; for(i=0;i<5;i++) { scanf(“%d”,&mark[i]); total=total+mark[i]; } printf(“%d”,total); }
9. 9. WHAT IF I DON’T USE ARRAY main() { int mark0,mark1,mark2,mark3,mark4,total; scanf(“%d%d%d%d%d”,&mark0,&mark1,&mark2,&mark3,&m ark4); total=mark0+mark1+mark2+mark3+mark4; printf(“%d”,total); }
10. 10. TWO DIMENSIONAL ARRAY  Declaration syntax:  type array_name[row_subscript][column_subscript];  type is data type such as int, char, float,etc.  array_name is a valid identifier.  row_subscript and column_subscript are positive integer constants.  Example:  int matrix[2][2]; matrix[0][0] matrix[0][1] matrix[1][0] matrix[1][1]
11. 11. INITIALIZATION  int matrix[2][2]={{5,0},{8,2}};  int matrix[][]={{5,0},{8,2}}; matrix[0][0] 5 matrix[0][1] 0 matrix[1][0] 8 matrix[1][1] 2
12. 12. main() { int matrix[3][3]={{1,2,3},{4,5,6},{7,8,9}}; int transpose[3][3],i,j; { for(i=0;i<3;i++) { for(j=0;j<3;j++) transpose[j][i]=matrix[i][j]; } 1 2 3 4 5 6 7 8 9 1 4 7 2 5 8 3 6 9