0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Chandan

321

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
321
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
10
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Arrays Designed By:Chandan deep singh College Guru nanak engg college, ludhiana, Punjab
• 2. Definition
• Array is a collection of variables of same data type with a common name
• 3. USE
• Can store multiple values of same type
• Values are stored in adjacent memory locations
• [ ] is the operator used
• 4. Example
• Int students[5]
• Allocate following memory
Fifth element Fourth element Third element Second element First element
• 5. Types of Arrays
• One Dimensional Arrays
• Two Dimensional Arrays
• Multi Dimensional Arrays
• 3D Array, 4D Arrays etc.
A[0] A[1] A[2] A[3] A[4] A[5] A[0][0] A[0][1] A[0][2] A[0][3] A[0][4] A[1][0] A[1][1] A[1][2] A[1][3] A[1][4 A[2][1] A[2][2] A[2][3] A[2][3] A[2][4]
• 6. Declaration of Array
• One Dimensional Arrays
• int A[10];
• float B[20];
• char C[20];
Two Dimensional Arrays int X[5][5]; char Y[5][25];
• 7. Initialization of the Array int A[5]= {1,2,3,4,5}; int ARR[ ] = {1,2,3,4,5,6}; char B[20]=“Rudrapur”; float C[5]={2.4,3.5,1.5}; int D[2][3]={{1,2}, {3,4}, {5,6}};
• 8. Accessing Array Elements In C++ first element of the array is always at zero position. A[0] A[1] A[2] A[3] A[4] A[5]
• 9. Sample Program on 1-D Array #include<iostream.h> void main() { int A[10],i; for(i=0;i<10;i++) { cout<<“Enter a number: “; cin>A[i]; } cout<<“n Array Contentsn”; for(i=0;i<10;i++) { cout<<A[i]<<“ “; } }
• 10. Sample Program on 2-D Array #include<iostream.h> void main() { int A[3][4],i,j; for(i=0;i<20;i++) for(j=0j<4;j++) { cout<<“Enter a number: “; cin>A[i]; } cout<<“n Array Contentsn”; for(i=0;i<10;i++) { for(j=0j<4;j++) cout<<A[i]<<“ “; cout<<endl; } }
• 11. Address Calculation in 2D Arrays
• Row Major
• Column Major
0 1 2 3 4 0 1 2 3 4 5 Base Address : 1000 Address of Location X Address of Location Y Size of Each Element : 2 Bytes In Row Major : 1032 In Column Major : 1016 In Row Major : 1018 In Column Major : 1032 Y X
• 12. Address Calculation in Row Major A[i][j]= B + [i*n + j] Where B=Base address of the array n = Total column of the array i = Row number of desired row j = Column number of desired row
• 13. Address in Column Major A[i][j]= B + [i + j*m] Where B=Base address of the array m = Total rows of the array i = Row number of desired row j = Column number of desired row
• 14. THANKS