Upcoming SlideShare
×

# Array Presentation (EngineerBaBu.com)

6,403 views
6,094 views

Published on

This is Array Presentation For All Engineers
Visit http://www.engineerbabu.com/ for More

1 Comment
6 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• i think this is best to tell about array

Are you sure you want to  Yes  No
Views
Total views
6,403
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
554
1
Likes
6
Embeds 0
No embeds

No notes for slide

### Array Presentation (EngineerBaBu.com)

1. 1. SEMINAR ON ARRAYS UNDER THE GUIDENCE OF- Ms. PINKI MA’M PREPARED BY- SAURABH SHUKLA & SAURABH VYAS B.E. 2 nd Yr. 1Vth Sem
2. 2. 11/01/11 ARRAYS INTRODUCTION AND CHARACTERISTICS <ul><li>AN ARRAY CAN BE DEFINED AS FINITE COLLECTION </li></ul><ul><li>OF HOMOGENOUS (SIMILAR TYPE) ELEMENTS </li></ul><ul><li>AN ARRAY CAN STORE EITHER ALL INTEGERS,ALL FLOATING </li></ul><ul><li>POINT NUMBERS,ALL CHARACTERS etc. </li></ul><ul><li>ARRAYS ARE ALWAYS STORED IN CONSECUTIVE MEMORY </li></ul><ul><li>LOCATIONS IT CAN BE INITIALIZED EITHER DURING </li></ul><ul><li>DECLARATION TIME OR DURING RUN TIME </li></ul>TYPES <ul><li>ONE DIMENSIONAL ARRAY </li></ul><ul><li>TWO DIMENSIONAL ARRAY </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>MULTI DIMENSIONAL ARRAY </li></ul>
3. 3. 11/01/11 DECLARATION OF ONE DIMENSIONAL ARRAY Data_type var_name[Expression] Data Type is the type of elements to be stored in the array Var_name is the name of the array like any Other variables Expression specifies the number of elements to be stored In array Example int num[10]; Num[0] =data1 Num[1] =data2 Num[2] =data3 Num[3] =data4 Num[4] =data5 Num[5] =data6 Num[6] =data7 Num[7] =data8 Num[8] =data9 Num[9] =data10
4. 4. 11/01/11 ACCESSING ONE DIMENSIONAL ARRAY ELEMENTS #<Include<stdio.h> #include<conio.h> Void main() { Int a[10]; Clrscr(); Printf (“enter the array”); For (i=0;i<10;i++) { Scanf(“%d”,&a[i]); } Printf(“the entered array is”); For(i=0;i<10;i++) { printf(“%d”,a[i]);} } getch(); } Arrray declaration Taking values from user Printing the values OUTPUT Enter the array 1 2 3 4 5 6 7 8 9 10 Entered array is 1 2 3 4 5 6 7 8 9 10
5. 5. 11/01/11 OPERATIONS IN ARRAY <ul><li>INSERTION </li></ul><ul><li>DELETION </li></ul><ul><li>MERGING TWO ARRAYS </li></ul><ul><li>TRAVERSING </li></ul><ul><li>SORTING </li></ul><ul><li>SEARCHING </li></ul>
6. 6. 11/01/11 ALGORITHM <ul><li>1 . [INITIALIZE THE VALUE OF i] SET i =len </li></ul><ul><li>2 . REPEAT FOR i=len DOWN TO pos </li></ul><ul><li>{SHIFT ELEMENTS DOWN BY 1 POSITION} </li></ul><ul><li>SET a[i+1]=a[i] </li></ul><ul><li>[END OF LOOP] </li></ul><ul><li>3 . [INSERT THE ELEMENT AT REQUIRED POSITION] </li></ul><ul><li>SET a[pos]=num </li></ul><ul><li>4 . [RESET len] SET len =len +1 </li></ul><ul><li>5 . DISPLAY THE NEW LIST OF ARRAYS </li></ul><ul><li>6. END </li></ul>INSERTION
7. 7. 11/01/11 INSERTION Int i,len,pos,num; Void main() { int a[10]; Void insert (int a[ ],int,int,int); Printf(“enter integers to be read”); Scanf(“%d”,&len); Printf(:enter ur array”); For(i=0;1<len;i++) { scanf(“%d”,&a[i]); } Printf(:enter position”); Scanf(“%d”,&pos); --pos; Printf(:enter integer to be inserted”); Scanf(“%d”,&num); Insert(a,len,pos,num);} For(i=len;1pos;i--) {a[i+1])=a[i]; } A{pos]=num; Len ++; Printf(“new array is ”); For(i=0;i<len;i++) { printf(“%d”,&a[i]); } } ORIGINAL ARRAY 1 2 3 4 5 6 7 8 HOW IT WORKS ENTER POSITION 4 ENTER NUM 9 DURING PROCESSING 1 2 3 …….. 4 5 6 7 8 NEW ARRAY 1 2 3 …9…. 4 5 6 7 8 THE ACTUAL 4 th POSITION
8. 8. 11/01/11 ALGORITHM <ul><li>1. SET item=a[pos] </li></ul><ul><li>2. REPEAT FOR i=pos to (n-1) </li></ul><ul><li>[SHIFTING ELEMENTS 1 POSITION DOWNWARDS ] </li></ul><ul><li>SET a[i]=a[i+1] </li></ul><ul><li>[END OF LOOP] </li></ul><ul><li>3. RESET n=n-1 </li></ul><ul><li>4. DISPLAY ELEMENTS OF NEW ARRAY </li></ul><ul><li>5. END </li></ul>DELETION
9. 9. 11/01/11 DELETION Int i,n; Void main() { Int a[10],pos; Void delete(int a[ ],int,int); Printf(“enter no of elements in array”); Scanf(“%d”,&n); Printf(“enter ur array”); For(i=0;i<n;i++) Scanf(“%d”,&a[i]); Printf(:enter position”); Scanf(“%d”,&pos); --pos; delete(a[ ],pos,n); Void delete(int a [ ],int pos,int n) { int j item; Item =a[pos]; For(j=pos;j<n;j++) a[j]=a[j+1]; n=n-1; } For(i=0;i<n;i++) printf(“%d”,&a[i]); } HOW IT WORKS ORIGINAL ARRAY 1 2 3 4 5 6 7 8 ENTER POSITION 4 DURING PROCESSING 1 2 3 4 5 6 7 8 NEW ARRAY 1 2 3 5 6 7 8 THE ACTUAL 4 th POSITION
10. 10. 11/01/11 ALGORITHM Letr LB be the lower bound andUB be the upper bound of linear array a 1. [initialize the counter] Set I at lower bound LB 2. Repeat for i=LB to UB [visit element]Display element a[i] [end of the loop] 3. EXIT TRAVERSING
11. 11. 11/01/11 TRAVERSING AN ARRAY #include<stdio.h> Void main ( ) { Int i,n,a[10]; printf)(“enter length of the array”); Scanf(“%d”,&n); Printf(“enter ur array”); For(i=0;i<n;i++) Scanf(“%d”,&a [ i ]); Printf(“traversing the array”); For(i=0;i<n;i++) printf(“%d”,&a [ i ]); } OUTPUT 1 2 3 4 5 TRAVERSING THE ARRAY ENTER UR ARRAY 1 2 3 4 5 ENTER LENGTH OF ARRAY 5
12. 12. 11/01/11 MERGING OF THE TWO ARRAYS <ul><li>MERGING MEANS COMBINING ELEMENTS OF TWO ARRAYS </li></ul><ul><li>TO FORM A NEW ARRAY </li></ul><ul><li>THIS CAN BE DONE IN 2 WAYS---- </li></ul><ul><li>1. FIRST COPY ALL ELEMENTS OF ARRAY 1 TO ARRAY3 </li></ul><ul><li>THEN ARRAY2 TO ARRAY3 </li></ul><ul><li>THEN SORT THE RESULTANT ARRAY3 </li></ul><ul><li>2. SECOND WAY IS TO SORT THE ARRAY DURING MERGING. </li></ul><ul><li>THIS TECHNIQUE IS CALLED SORTING WHILE MERGING </li></ul><ul><li>AND IT IS MORE EFFICIENT ONE </li></ul>
13. 13. 11/01/11 void main() { int a[10],b[10],c[10],i,j,k=0,l,m,n; cout<<&quot;Enter sizeof array1 and array2” ; cin>>m>>n; cout<<&quot;Enter elements of 1st:n&quot;; for(i=0;i<m;i++) {cin>>a[i];} cout<<&quot;Elements of 2nd list:n&quot;; for(i=0;i<n;i++) {cin>>b[i];} for(i=0;i<m;i++) {c[i]=a[i];} for(j=0;j<n;j++) {c[j+m]=b[j];} for(i=0;i<(m+n);i++) {for(j=0;j<(m+n);j++) {if(c[i]<c[j]) {k=c[j]; c[j]=c[i]; c[i]=k; }}} cout<<&quot;New merged array :&quot;; for(i=0;i<(m+n);i++) {cout<<c[i]<<&quot;t&quot;;} } PROGRAM OUTPUT Enter size of array1 and array2 5 6 Enter elements of 1 st: 5 9 16 50 80 Elements of 2 nd list: 11 32 49 58 75 98 New merged array : 5 9 11 16 32 49 50 58 75 80 98
14. 14. 11/01/11 ALGORITHM SEARCHING <ul><li>[ Insert item at the end of data ] set data[n+1]=item. </li></ul><ul><li>[ Initialize counter ] set loc = 1. </li></ul><ul><li>[ Search for item ] </li></ul><ul><li>Repeat while data[loc] =! Item </li></ul><ul><li>Set loc = loc +1. </li></ul><ul><li>[ Successful ] if loc=n+1 , then set loc = 0. </li></ul><ul><li>Exit </li></ul>
15. 15. 11/01/11 SEARCHING Int i,n; Void main() { Int a[10],pos=-1,k; Printf(“enter no of elements in array”); Scanf(“%d”,&n); Printf(“enter ur array”); For(i=0;i<n;i++) Scanf(“%d”,&a[i]); Printf(:enter no. to be searched :”); Scanf(“%d”,&k); For(j=0;j<n;j++) {if (k==a[j]) {pos=i; break;} } If (pos>=0) printf(“n%d is found in position %d”,k,pos+1); Else printf(“nelement does not exist”); getch(); } OUTPUT Enter no of elements in array: 6 Enter ur array: 10 20 30 40 50 60 Enter no to be searched : 50 50 is found in position 5 OUTPUT Enter no of elements in array: 6 Enter ur array: 10 20 30 40 50 60 Enter no to be searched : 45 Element does not exist
16. 16. 11/01/11 SORTING <ul><li>SORTING REFERS TO THE OPERATION OFARRANGING DATA IN </li></ul><ul><li>SOME GIVEN SEQUENCE ie. ASCENDING OR DESCENDING ORDER . </li></ul><ul><li>SORTING IS OF 2 TYPES---- </li></ul><ul><li>1. INTERNAL SORTING —MEANS ARRANGING THE NOs. WITHIN </li></ul><ul><li>THE ARRAY ONLY WHICH IS IN CXOMPUTER PRIMARY </li></ul><ul><li>MEMORY. </li></ul><ul><li>2. EXTERNAL SORTING ----IT IS THE SORTING OF NOs. FROM </li></ul><ul><li>EXTERNAL FILE BY RADING IT FROM SECONFDARY </li></ul><ul><li>MEMORY. </li></ul>TYPES 1.BUBBLE SORT 2.SELECTION SORT 3.INSERTION SORT 4.QUICK SORT 5.MEARGE SORT etc.
17. 17. 11/01/11 SELECTION SORTINNG <ul><li>THIS TECHNIQUE IS BASED UPON THE EXTENSION </li></ul><ul><li>OF MINIMUM / MAXIMUM TECHNIQUE. </li></ul><ul><li>BY MEANS OF NESTED FOR LOOPS ,MINIMUM VALUE </li></ul><ul><li>IS FOUND OUT. </li></ul><ul><li>ONCE THIS IS FOUND ,IT IS PLACED IN 1 st POSITION </li></ul><ul><li>OF ARRAY ie. Position 0 </li></ul><ul><li>THEN WE’LL FIND THE NEXT SMALLEST ELEMENT </li></ul><ul><li>FROM REMAINING ELEMENTS </li></ul><ul><li>AND NOW THIS ELEMENT IS PLACED IN 2 nd POSITION </li></ul><ul><li>ie. Position 1 </li></ul>
18. 18. 11/01/11 PROGRAM #include<stdio.h> void main() { int a[10],i,n,j,x; printf(&quot;enter the value of n n&quot;); scanf(&quot;%d&quot;,&n); printf(&quot;enter array n&quot;); for(i=0;i<n;i++) { scanf(&quot;%d&quot;,&a[i]);} printf(&quot; ur unsorted array is n&quot;); for(i=0;i<n;i++) { printf(&quot;%d &quot;,a[i]);} for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[j]>a[i]) { x=a[i]; a[i]=a[j]; a[j]=x; } } } printf(&quot; ur sorted array is n&quot;); for(j=0;j<n;j++) {printf(&quot;%d &quot;,a[j]);} } SELECTION SORTING
19. 19. 11/01/11 interchange 16 13 15 EXAMPLE ENTERED ARRAY a[0] a[1] a[2] a[3] a[4] 6 PASS 1 13 16 6 2 15 PASS 2 13 16 15 2 6 PASS 3 16 13 15 2 6 PASS 4 15 13 16 2 6 PASS 5 13 2 6 16 15 2
20. 20. 11/01/11 BUBBLE SORTING <ul><li>IN BUBBLE SORT, EACH ELEMENT IS COMPARED WITH ITS ADJECENT </li></ul><ul><li>ELEMENT.IF 1 st ELEMENT IS LARGER THAN THE 2 nd ONE THE </li></ul><ul><li>POSITION GETS INTERCHANGED,OTHERWISE NOT. </li></ul><ul><li>AND NEXTELEMENT IS COMPARED WITH ITS ADJECENT ELEMENT AND </li></ul><ul><li>SAME PROCESS IS REPEATED </li></ul>EXAMPLE INITIAL ELEMENTS (without sorting) 11 15 2 13 6
21. 21. 11/01/11 EXAMPLE > 11 13 ENTERED ARRAY a[0] a[1] a[2] a[3] a[4] 6 PASS 1 6 13 15 11 2 PASS 2 13 6 15 2 11 PASS 3 11 13 15 2 6 PASS 4 13 11 15 2 6 RESULT 13 2 6 11 15 2 < > > > < > < < > < < < < < < 15
22. 22. 11/01/11 LIMITATIONS OF LINEAR ARRAYS <ul><li>THE PRIOR KNOWLEDGE OF NO. OF ELEMENTS </li></ul><ul><li>IN THE LINEAR ARRAY IS NECESSARY </li></ul><ul><li>THESE ARE STATIC STRUCTURES STATIC IN THE </li></ul><ul><li>SENSE THAT MEMORY IS ALLOCATED AT </li></ul><ul><li>COMPILATION TIME, THEIR MEMORY USED BY </li></ul><ul><li>THEM CAN’T BE REDUCED OR EXTENDED </li></ul><ul><li>SINCE THE ELEMENTS OF THIS ARRAYS </li></ul><ul><li>ARE STORED IN THESE ARRAYS ARE </li></ul><ul><li>TIME CONSUMING THIS IS B’COZ OF MOVING </li></ul><ul><li>DOWN OR UP TO CREATE A SPACE OF NEW </li></ul><ul><li>ELEMENT OR TO OCCUPY THE SPACE </li></ul><ul><li>VACATED BY DELETED ELEMENT </li></ul>
23. 23. 11/01/11 FROM SAURABH SHUKLA & SAURABH VYAS