Upcoming SlideShare
×

# Arrays

2,325
-1

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
2,325
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
120
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Arrays

1. 1. C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
2. 2. t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS ARRAYS 2
3. 3. Introduction • Arrays – Group of consecutive memory locations. – Each memory location is known as element. – The name and type of each element is same. – Declare an array with the name and size. • data_type array_name[size]; int a[5]; char name[20]; 3
4. 4. Introduction… int a[5]; • Above statement creates an array of… • Data type: integer First element at 0th position • Array name: a • Array size : 5 Memory • Counting starts from 0. 0 1 2 3 4 4
5. 5. Introduction… • Format to access an element. – array_name[position]; int a[5]; Printf(“%d”,a[3]); 0 1 2 3 4 5
6. 6. Initialization of Array • Initialize at the time of declaration. int a[5] = { 10, 20, 30, 40, 50 }; • Initialization all elements with zero. int a[5] = { 0 }; • Initialize all element and define it’s size. int a[ ] = { 10, 20, 30, 40, 50 }; • 5 values means 5 elements in array a. 6
7. 7. Initialization of Array… • All the elements can be initialized as the normal variables. int a[5]; a[0] = 10; a[1] = 20; a[2] = 30; a[3] = 40; a[4] = 50; 7
8. 8. Initialization of Array… • Initialize all elements using loops. for (int i=0; i<=4; i++) { scanf(“%d”,&a[ i ]); } 8
9. 9. Character Arrays • Character arrays can be initialized using string literals (Double quotes). char c* + = “hello”; – “hello” is a string. – String terminates with the null character ‘0’. – String “hello” has 6 elements. – We can also initialize it with char c* + = ,‘h’ , ’e’ , ’l’ , ’l’ , ’o’, ‘0’-; 9
10. 10. More about arrays • Array name is the address of the first element of the array. printf(“%u”,&a[0]); printf(“%u”, a); • Both statements will produce the same result. 10
11. 11. More about arrays • In case of character type array we can scan the contents by using array name only. scanf(“%s”,c); – Reads characters until whitespace encountered. – May go beyond the size of an array. 11
12. 12. Multi-Dimensional Arrays • Two-Dimensional arrays – Assume it as a table of row and column. – First specify Row and then column. int a[2][3]; – Array a have 2 rows and 3 columns. 6 5 4 3 2 1 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 12
13. 13. Two-Dimentional Array… int a[2][3]; • Array a contains 2 rows and 3 columns, that means it has 2 * 3 = 6 elements. • These elements will store in consecutive memory locations. a[1][2] a[1][1] a[1][0] a[0][2] a[0][1] a[0][0] 13
14. 14. Initialization int a[2][3] = { {1,2,3} {10,20,30} }; 1 2 3 10 20 30 int a[2][3] = { {1,2} {10} }; 1 2 0 10 0 0 14
15. 15. Accessing Elements for(i=0; i<=1; i++) { for(j=0; j<=2; j++) { printf(“%d”,a[i][j]); } } printf(“%d”,a[1][2]); 15
16. 16. Mind Bend • Write a program which performs the following tasks: – Initialize an integer array of 10 elements. – Print array in a well formatted way. – Multiply each element of array by 3. – Print the array elements. • Write a program to pick up the largest and smallest number from any 3 x 3 array. 16
17. 17. Mind Bend • Write a program to add two 5 x 5 matrix. • Write a program to initialize an array of 6 x 6 by keyboard and copy elements of an array to the another 4 x 9 array. 1 5 2 6 3 7 4 8 1 2 3 4 5 6 9 10 11 12 7 8 9 10 11 12 13 14 15 16 17 18 19 20 13 14 15 16 17 18 21 22 23 24 19 20 21 22 23 24 25 26 27 28 25 26 27 28 29 30 29 30 31 32 31 32 33 34 35 36 33 34 35 36 17
18. 18. To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS POINTERS 18
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.