The document summarizes key concepts about arrays including:
1) Arrays can store homogeneous elements and are stored in consecutive memory locations. Common array types include one-dimensional, two-dimensional, and multi-dimensional arrays.
2) Operations on arrays include insertion, deletion, merging, traversing, and sorting elements. Common sorting algorithms discussed include selection sort and bubble sort.
3) Linear arrays have limitations such as the need to pre-determine the number of elements and static memory allocation.
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.
11/01/11 ARRAYS INTRODUCTION AND CHARACTERISTICS AN ARRAY CAN BE DEFINED AS FINITE COLLECTION OF HOMOGENOUS (SIMILAR TYPE) ELEMENTS AN ARRAY CAN STORE EITHER ALL INTEGERS,ALL FLOATING POINT NUMBERS,ALL CHARACTERS etc. ARRAYS ARE ALWAYS STORED IN CONSECUTIVE MEMORY LOCATIONS IT CAN BE INITIALIZED EITHER DURING DECLARATION TIME OR DURING RUN TIME TYPES ONE DIMENSIONAL ARRAY TWO DIMENSIONAL ARRAY . . MULTI DIMENSIONAL ARRAY
3.
11/01/11 DECLARATION OFONE 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.
11/01/11 ACCESSING ONEDIMENSIONAL 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.
11/01/11 OPERATIONS IN ARRAY INSERTION DELETION MERGING TWO ARRAYS TRAVERSING SORTING SEARCHING
6.
11/01/11 ALGORITHM 1. [INITIALIZE THE VALUE OF i] SET i =len 2 . REPEAT FOR i=len DOWN TO pos {SHIFT ELEMENTS DOWN BY 1 POSITION} SET a[i+1]=a[i] [END OF LOOP] 3 . [INSERT THE ELEMENT AT REQUIRED POSITION] SET a[pos]=num 4 . [RESET len] SET len =len +1 5 . DISPLAY THE NEW LIST OF ARRAYS 6. END INSERTION
7.
11/01/11 INSERTION Inti,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.
11/01/11 ALGORITHM 1.SET item=a[pos] 2. REPEAT FOR i=pos to (n-1) [SHIFTING ELEMENTS 1 POSITION DOWNWARDS ] SET a[i]=a[i+1] [END OF LOOP] 3. RESET n=n-1 4. DISPLAY ELEMENTS OF NEW ARRAY 5. END DELETION
9.
11/01/11 DELETION Inti,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.
11/01/11 ALGORITHM LetrLB 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/01/11 TRAVERSING ANARRAY #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.
11/01/11 MERGING OFTHE TWO ARRAYS MERGING MEANS COMBINING ELEMENTS OF TWO ARRAYS TO FORM A NEW ARRAY THIS CAN BE DONE IN 2 WAYS---- 1. FIRST COPY ALL ELEMENTS OF ARRAY 1 TO ARRAY3 THEN ARRAY2 TO ARRAY3 THEN SORT THE RESULTANT ARRAY3 2. SECOND WAY IS TO SORT THE ARRAY DURING MERGING. THIS TECHNIQUE IS CALLED SORTING WHILE MERGING AND IT IS MORE EFFICIENT ONE
13.
11/01/11 void main(){ int a[10],b[10],c[10],i,j,k=0,l,m,n; cout<<"Enter sizeof array1 and array2” ; cin>>m>>n; cout<<"Enter elements of 1st:\n"; for(i=0;i<m;i++) {cin>>a[i];} cout<<"Elements of 2nd list:\n"; 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<<"New merged array :"; for(i=0;i<(m+n);i++) {cout<<c[i]<<"\t";} } 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.
11/01/11 ALGORITHM SEARCHING[ Insert item at the end of data ] set data[n+1]=item. [ Initialize counter ] set loc = 1. [ Search for item ] Repeat while data[loc] =! Item Set loc = loc +1. [ Successful ] if loc=n+1 , then set loc = 0. Exit
15.
11/01/11 SEARCHING Inti,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.
11/01/11 SORTING SORTINGREFERS TO THE OPERATION OFARRANGING DATA IN SOME GIVEN SEQUENCE ie. ASCENDING OR DESCENDING ORDER . SORTING IS OF 2 TYPES---- 1. INTERNAL SORTING —MEANS ARRANGING THE NOs. WITHIN THE ARRAY ONLY WHICH IS IN CXOMPUTER PRIMARY MEMORY. 2. EXTERNAL SORTING ----IT IS THE SORTING OF NOs. FROM EXTERNAL FILE BY RADING IT FROM SECONFDARY MEMORY. TYPES 1.BUBBLE SORT 2.SELECTION SORT 3.INSERTION SORT 4.QUICK SORT 5.MEARGE SORT etc.
17.
11/01/11 SELECTION SORTINNG THIS TECHNIQUE IS BASED UPON THE EXTENSION OF MINIMUM / MAXIMUM TECHNIQUE. BY MEANS OF NESTED FOR LOOPS ,MINIMUM VALUE IS FOUND OUT. ONCE THIS IS FOUND ,IT IS PLACED IN 1 st POSITION OF ARRAY ie. Position 0 THEN WE’LL FIND THE NEXT SMALLEST ELEMENT FROM REMAINING ELEMENTS AND NOW THIS ELEMENT IS PLACED IN 2 nd POSITION ie. Position 1
18.
11/01/11 PROGRAM #include<stdio.h>void main() { int a[10],i,n,j,x; printf("enter the value of n \n"); scanf("%d",&n); printf("enter array \n"); for(i=0;i<n;i++) { scanf("%d",&a[i]);} printf(" ur unsorted array is \n"); for(i=0;i<n;i++) { printf("%d ",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(" ur sorted array is \n"); for(j=0;j<n;j++) {printf("%d ",a[j]);} } SELECTION SORTING
11/01/11 BUBBLE SORTINGIN BUBBLE SORT, EACH ELEMENT IS COMPARED WITH ITS ADJECENT ELEMENT.IF 1 st ELEMENT IS LARGER THAN THE 2 nd ONE THE POSITION GETS INTERCHANGED,OTHERWISE NOT. AND NEXTELEMENT IS COMPARED WITH ITS ADJECENT ELEMENT AND SAME PROCESS IS REPEATED EXAMPLE INITIAL ELEMENTS (without sorting) 11 15 2 13 6
11/01/11 LIMITATIONS OF LINEAR ARRAYS THE PRIOR KNOWLEDGE OF NO. OF ELEMENTS IN THE LINEAR ARRAY IS NECESSARY THESE ARE STATIC STRUCTURES STATIC IN THE SENSE THAT MEMORY IS ALLOCATED AT COMPILATION TIME, THEIR MEMORY USED BY THEM CAN’T BE REDUCED OR EXTENDED SINCE THE ELEMENTS OF THIS ARRAYS ARE STORED IN THESE ARRAYS ARE TIME CONSUMING THIS IS B’COZ OF MOVING DOWN OR UP TO CREATE A SPACE OF NEW ELEMENT OR TO OCCUPY THE SPACE VACATED BY DELETED ELEMENT