array presentation (engineerbabu.com)

Post on 20-Jan-2015

2.542 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

SEMINAR ON

ARRAYS

UNDER THE GUIDENCE OF-

Ms. PINKI MA’M

PREPARED BY-

SAURABH SHUKLA&

SAURABH VYAS

B.E. 2nd Yr. 1Vth Sem

04/10/23 2

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

04/10/23 3

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

04/10/23 4

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 is123456789

10

04/10/23 5

OPERATIONS IN ARRAY

INSERTION

DELETION

MERGING TWO ARRAYS

TRAVERSING

SORTING

SEARCHING

04/10/23 6

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

04/10/23 7

INSERTIONINSERTIONInt 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 4ENTER 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 4th POSITION

04/10/23 8

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

04/10/23 9

DELETIONDELETIONInt 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 WORKSORIGINAL 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 4th POSITION

04/10/23 10

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

04/10/23 11

TRAVERSING AN ARRAYTRAVERSING 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

12345

TRAVERSING THE ARRAY

ENTER UR ARRAY1 2 3 4 5

ENTER LENGTH OF ARRAY 5

04/10/23 12

MERGING OF THE TWO ARRAYS

MERGING MEANS COMBINING ELEMENTS OF TWO ARRAYSTO 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

04/10/23 13

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

OUTPUTOUTPUT

Enter size of array1 and array2

5

6

Enter elements of 1st:

5 9 16 50 80

Elements of 2nd list:

11 32 49 58 75 98

New merged array :

5 9 11 16 32 49 50 58 75 80 98

04/10/23 14

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

04/10/23 15

SEARCHINGSEARCHING

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);Elseprintf(“\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

04/10/23 16

SORTINGSORTING

SORTING REFERS 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.

TYPESTYPES

1.BUBBLE SORT 2.SELECTION SORT3.INSERTION SORT 4.QUICK SORT 5.MEARGE SORT etc.

04/10/23 17

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 1st POSITION OF ARRAY ie. Position 0

THEN WE’LL FIND THE NEXT SMALLEST ELEMENT FROM REMAINING ELEMENTS

AND NOW THIS ELEMENT IS PLACED IN 2nd POSITIONie. Position 1

04/10/23 18

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

04/10/23 19

interchange

16 1315

EXAMPLE

ENTERED ARRAY a[0] a[1] a[2] a[3] a[4]

6PASS 1

1316 62 15PASS 2

1316 152 6PASS 3

1613 152 6PASS 4

1513 162 6PASS 5

132 616 15

2

04/10/23 20

BUBBLE SORTING

IN BUBBLE SORT, EACH ELEMENT IS COMPARED WITH ITS ADJECENT ELEMENT.IF 1st ELEMENT IS LARGER THAN THE 2nd 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

04/10/23 21

EXAMPLE

11 13

ENTERED ARRAY a[0] a[1] a[2] a[3] a[4]

6PASS 1

613 1511 2PASS 2

136 152 11PASS 3

1113 152 6PASS 4

1311 152 6RESULT

132 611 15

215

04/10/23 22

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

04/10/23 23FROM SAURABH SHUKLA & SAURABH VYAS

top related