Chandan

462 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
462
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chandan

  1. 1. Arrays Designed By:Chandan deep singh College Guru nanak engg college, ludhiana, Punjab
  2. 2. Definition <ul><li>Array is a collection of variables of same data type with a common name </li></ul>
  3. 3. USE <ul><li>Can store multiple values of same type </li></ul><ul><li>Values are stored in adjacent memory locations </li></ul><ul><li>[ ] is the operator used </li></ul>
  4. 4. Example <ul><li>Int students[5] </li></ul><ul><li>Allocate following memory </li></ul>Fifth element Fourth element Third element Second element First element
  5. 5. Types of Arrays <ul><li>One Dimensional Arrays </li></ul><ul><li>Two Dimensional Arrays </li></ul><ul><li>Multi Dimensional Arrays </li></ul><ul><li>3D Array, 4D Arrays etc. </li></ul>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. 6. Declaration of Array <ul><li>One Dimensional Arrays </li></ul><ul><li>int A[10]; </li></ul><ul><li>float B[20]; </li></ul><ul><li>char C[20]; </li></ul>Two Dimensional Arrays int X[5][5]; char Y[5][25];
  7. 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. 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. 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. 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. 11. Address Calculation in 2D Arrays <ul><li>Row Major </li></ul><ul><li>Column Major </li></ul>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. 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. 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. 14. THANKS <ul><li>FOR YOUR TIME </li></ul>

×