1d arrays classnotesmit.pptx

Post on 20-Jul-2016

234 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Information theory & coding (ECE) - SlideShare www.slideshare.net/nitmittal/information-theory-coding-eceMar 4, 2011 - This book offers a comprehensive review of the Information Theory and Coding. Themain text can be divided into four sections on Probability ...[PDF]An introduction to information theory and entropy www.fi.muni.cz/usr/staudek/infteo/info-lec.pdfby T Carter - ‎2011 - ‎Cited by 27 - ‎Related articlesBasics of information theory. 15. Some entropy theory ... This approach has been described as information theory. 7 ...... contemporary information, coding, and.Information and Coding Theory - AbeBooks www.abebooks.com/book-search/title/coding-theory/n/200000098/Information Theory And Coding by Anoop Singh Poonia and a great selection of similar Used, New and Collectible Books available now at AbeBooks.com.[PDF]Elements of Information Theory - FTP Directory Listing ftp://ftp.esat.kuleuven.ac.be/pub/sista/decock/voor_xander/.../0.pdfby TM Cover - ‎1991 - ‎Cited by 35479 - ‎Related articles

TRANSCRIPT

Arrays

• What is an array?

An array is a derived data type from fundamental data type. It is a collection of variables of the same type that are referenced by a

common name which are stored in a contiguous memory locations.

• Declaring Arrays

Array are declared by specifying its data type, name and the number

of elements the array holds between square brackets immediately following the array name.

For Example: int marks[20] or int student_marks[5][20];

•Types of arrays One dimensional array Multi dimensional array

Two dimensional Three dimensional …….. N dimensional

• One Dimensional Array A list of items can be given one variable name using one

subscript and such a variable is called single-subscript variable or one dimensional(1D) array.

• Declaration Syntax: Data_type Array_name[ Max_size];

Where Data_type one among built-in-data type or user defined data type.

Arra_name is a valid identifier. Max_size specifies how many elements the array has to contain.

8/2

3/2

01

4

De

pt.

ofC

SE

2

• One Dimensional Array For example: float marks[5];

8/2

3/2

01

4

De

pt.

ofC

SE

3

marks

[0] [1] [2] [3] [4]

The individual components are accessed by using the array name

together with an integral valued index in square brackets. The index

indicates the position of the component within the collection .

The subscript/index value ranges from 0 to one less than the

maximum size specified in [ ]. That is, the first array element always

has subscript 0, the second array element has subscript 1, the Third

array element has subscript 2 etc. In general the subscript is

assigned to an integer variable.( like I or j )

A subscript can be integer constants, Integer variables like i, or

expressions that yield integers.

By default all the locations are initialized with unknown value.

• Initialization of arrays

Syntax:Type array_name[ Max_size]={ val0, val1,…..,valMax_size-1};

Example 1: int marks[6]={11,21};

0 1 2 3 4 5

marks

Will initialize 11 to marks[0]

21 to marks[1] and 0 to remaining locations if the elements assigned are less than the size of the array.

Example 2: static int marks[6];

0 1 2 3 4 5

marks

Will initialize all locations of the array to zero because an array is declared as static.

8/2

3/2

01

4

De

pt.

ofC

SE

4

0 0 0 0 0 0

11 21 0 0 0 0

• Assigning elements to an array

The elements can be assigned to any positions of the array.

Example: int a[10];

a[0]=12; a[3]=13; … etc.

• Accessing the elements from an array

int s=a[2]; or cout<< a[3].

• Some of the operations carried using 1D array

To Read and display elements

To do arithmetic operations on two arrays

To search for the element

To sort the elements

To insert and delete an element …etc

8/2

3/2

01

4

De

pt.

ofC

SE

5

8/2

3/2

01

4

De

pt.

ofC

SE

6

#include<iostream.h> #include<conio.h> void main() { int n,i,a[10]; clrscr(); cout<<"enter no of elements and it should be less than or equal to 10"; cin>>n; cout<<"\nenter elements\n"; for(i=0;i<n;i++) cin>>a[i]; for(i=0;i<n;i++) cout<<a[i]<<endl; getch();}

WAP to read n elements to an array a and print it

8/2

3/2

01

4

De

pt.

ofC

SE

7

#include<iostream.h>

#include<conio.h>

void main()

{

int n,m,i,j,a[10],b[10],c[10];

clrscr();

cout<<"enter no of elements for the first array\n";

cin>>n;

for(i=0;i<n;i++)

{

cout<<"enter number\n";

cin>>a[i];

}

WAP to add two array elements and store the corresponding

elements sum in another array

8/2

3/2

01

4

De

pt.

ofC

SE

8

cout<<"enter no of elements for the second array\n"; cin>>m; for(i=0;i<m;i++) { cout<<"enter number\n"; cin>>b[i]; } if(m==n) { for(i=0;i<m;i++) c[i]=a[i]+b[i]; cout<<“the resultant array is\n”; for(i=0;i<n;i++) cout<<c[i]<<endl; } else cout<<"cannot add"; getch(); }

To display elements of an array in reverse order.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10],i,j,n; cout<<"enter values"; for(i=0;i<10;i++) cin>>a[i]; for(j=i-1;j>=0;--j) cout<<a[j]<<"\t"; getch(); }

8/2

3/2

01

4

De

pt.

ofC

SE

9

Write a program to reverse an array using only one array.

void main()

{

int a[20],i,j,n, temp;

cout<<"enter n \n";

cin>>n;

cout<<“enter values for an array";

for(i=0;i<n;i++)

cin>>a[i];

8/2

3/2

01

4

De

pt.

ofC

SE

10

j=n-1; for(i=0;i<n/2; i++) {temp=a[i]; a[ i]=a[ j]; a[ j]=temp; j--; } cout<<"\n reverse\n\n"; for(i=0;i<n;i++) cout<<a[i]<<"\t"; getch(); }

8/2

3/2

01

4

De

pt.

ofC

SE

11

#include<iostream.h> #include<conio.h> void main() { int n,i,j,a[10],pos; clrscr(); cout<<"enter no of numbers"; cin>>n; cout<<"enter n numbers \n"; for(i=0;i<n;i++) cin>>a[i]; cout<<"enter the position, i.e index at which the element to

be deleted";

cin>>pos;

for(i=pos; i<n; i++) a[i] =a[i+1]; for(i=0;i<n-1;i++) cout<<a[i]<<endl; getch();}

WAP to delete an element from a specified position in a given array

Sorting-Ascending Selection sort algorithm

1. Find the minimum value in the list

2. Swap it with the value in the first position

3. Repeat the steps 1 and 2 for the remainder of the list (starting at the second position and advancing each time)

Example:

64 25 12 22 11

11 25 12 22 64

11 12 25 22 64

11 12 22 25 64

11 12 22 25 64

8/2

3/2

01

4

De

pt.

ofC

SE

13

void main() { int i,n,j,min,temp,a[30],k,pos; cout<<“\nHow many elements you want to sort: “

cin>>n; cout<<\nEnter the values one by one: “; for (i = 0; i < n i++) cin>>a[i];

//display before sorting for (i = 0; i < n-1; i++) { min = a[i];

pos=i; for (j = i+1; j < n; j++) { if (a[j] < min) {min = a[j];

pos=j;} }

a[pos] = a[i]; a[i] = min;

} //display after sorting }

8/2

3/2

01

4

De

pt.

ofC

SE

14

Bubble sort algorithm(Ascending order)

1. Compare the first and the second elements. If the first element is greater than the second element then interchange them.

2. Compare the second and the third elements. If the second element is greater than the third element then interchange them.

3. The process is repeated till the last element is reached and largest element occupies the N location. This is called as the first pass.

4. Repeat steps 1, 2 and 3 for the elements between 1 to (N-1), because Nth element is all ready sorted. At the end of this the next largest element occupies (N-1) Location. This is called as second pass.

5. The process is repeated for N-1 pass. At the end of this all the elements will be in a sorted order.

8/2

3/2

01

4

De

pt.

ofC

SE

15

Bubble Sort Example

8/2

3/2

01

4

De

pt.

ofC

SE

16

9, 6, 2, 12, 11, 9, 3, 7

6, 9, 2, 12, 11, 9, 3, 7

6, 2, 9, 12, 11, 9, 3, 7

6, 2, 9, 12, 11, 9, 3, 7

6, 2, 9, 11, 12, 9, 3, 7

6, 2, 9, 11, 9, 12, 3, 7

6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12

Bubble Sort Example

8/2

3/2

01

4

De

pt.

ofC

SE

17

6, 2, 9, 11, 9, 3, 7, 2, 6, 9, 11, 9, 3, 7 2, 6, 9, 9, 11, 3, 7, 2, 6, 9, 9, 3, 11, 7, 2, 6, 9, 9, 3, 7, 11, 12

6, 2, 9, 11, 9, 3, 7, 12 First Pass

Second Pass

Bubble Sort Example

8/2

3/2

01

4

De

pt.

ofC

SE

18

2, 6, 9, 9, 3, 7, 11, 2, 6, 9, 3, 9, 7, 11, 2, 6, 9, 3, 7, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12 Second Pass

First Pass

Third Pass

Bubble Sort Example

8/2

3/2

01

4

De

pt.

ofC

SE

19

2, 6, 9, 3, 7, 9, 11, 2, 6, 3, 9, 7, 9, 11, 12 2, 6, 3, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12 Second Pass

First Pass

Third Pass

2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass

Bubble Sort Example

8/2

3/2

01

4

De

pt.

ofC

SE

20

2, 6, 3, 7, 9, 9, 11, 2, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12 Second Pass

First Pass

Third Pass

The list is now sorted but the algorithm does not know this until it

completes a pass with no exchanges.

2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass

2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass

Bubble Sort Example

8/2

3/2

01

4

De

pt.

ofC

SE

21 2, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12 Second Pass

First Pass

Third Pass

2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass

2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass

Sixth Pass 2, 3, 6, 7, 9, 9, 11, 12

Example Let the array contains "5 1 4 2 8“ elements. In each step, elements written in bold are being compared and swapped if required. First Pass:

( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Swap since 5>1

( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4

( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2

( 1 4 2 5 8 ) ( 1 4 2 5 8 ), No swap Second Pass:

( 1 4 2 5 8 ) ( 1 4 2 5 8 )

( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2

( 1 2 4 5 8 ) ( 1 2 4 5 8 )

( 1 2 4 5 8 ) ( 1 2 4 5 8 )

8/2

3/2

01

4

De

pt.

ofC

SE

22

Third Pass:

( 1 2 4 5 8 ) ( 1 2 4 5 8 )

( 1 2 4 5 8 ) ( 1 2 4 5 8 )

( 1 2 4 5 8 ) ( 1 2 4 5 8 )

( 1 2 4 5 8 ) ( 1 2 4 5 8 )

Finally, the array is sorted, and the algorithm can terminate.

8/2

3/2

01

4

De

pt.

ofC

SE

23

void main() { int i,j,a[50],n,temp; clrscr(); cout<<"how many numbers"; cin>>n; cout<<"\n enter terms "; for(i=0;i<n;i++) cin>>a[i];

Cout<<“\Elemets before sort”; cout<<"\n sorted array is"; for(i=0;i<n;i++) cout<<a[i]<<"\n";

8/2

3/2

01

4

De

pt.

ofC

SE

24

for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } cout<<"\n sorted array is"; for(i=0;i<n;i++) cout<<a[i]<<"\n"; getch(); }

Write a program to sort the elements in ascending order using bubble sort.

8/2

3/2

01

4

De

pt.

ofC

SE

25

8/2

3/2

01

4

De

pt.

ofC

SE

26

8/2

3/2

01

4

De

pt.

ofC

SE

27

8/2

3/2

01

4

De

pt.

ofC

SE

28

8/2

3/2

01

4

De

pt.

ofC

SE

29

#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { int n,i,j,a[10],key,pos,found=0; clrscr(); cout<<"enter no of numbers"; cin>>n; for(i=0;i<n;i++) { cout<<"enter number\n"; cin>>a[i]; } cout<<"enter the element to be searched"; cin>>key;

WAP to search an element in an array using linear search

for(i=0;i<n;i++) { if(a[i]==key) { found=1; pos=i+1; break; } } if(found==1) { cout<<"element is found in "<<pos<<"position"<<endl; } else cout<<"no is not found"; getch(); }

8/2

3/2

01

4

De

pt.

ofC

SE

30

8/2

3/2

01

4

De

pt.

ofC

SE

31

8/2

3/2

01

4

De

pt.

ofC

SE

32

8/2

3/2

01

4

De

pt.

ofC

SE

33

low=0;high=n-1; while(low<=high) { mid=(low high)/2; if(a[mid]==key) { flag=1;break; } else if(a[mid]<key) low=mid+1; else high=mid-1; } if(flag==1) cout<<"\n search success"; else cout<<"\n search unsuccessful";

BINARY SEARCH PROCEDURE

Downloaded

From www.classnotesmit.blogspot.com

8/2

3/2

014

D

ep

t o

f C

.S.E

34

top related