Download presentation
Presentation is loading. Please wait.
Published byAshlynn West Modified over 9 years ago
2
Topics to be covered Introduction to array Introduction to array Types of array Types of array One dimensional array One dimensional array Declaration of an array Declaration of an array Initialization of an array Initialization of an array Reading and Writing an array Reading and Writing an array Programs of One dimensional Array Programs of One dimensional Array
3
What is Array An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, collection of doubles).
4
Array ◦ Group of consecutive memory locations ◦ Same name and type To refer to an element, specify ◦ Array name ◦ Position number Format: arrayname[ position number ] ◦ First element at position 0 ◦ n element array named A: A[ 0 ], A[ 1 ]...A[ n – 1 ] 4 6 3 2 A[0] A[1] A[2] A[3] Back
5
Types of Arrays 1. One dimensional Array 2. Two dimensional Array 3. Multidimensional Array Back
6
One Dimensional Array One dimensional Array: An array in which each individual element can be refer by one subscript is called one dimensional array. Back
7
Declaration of an array Syntax: type arrayname[array_size]; Example: int A[10]; The array elements are all values of the type type. The size of the array is indicated by array_size, the number of elements in the array. array_size: must be an int constant or a constant expression. Note that an array can have multiple dimensions.
8
Declare an array of 10 integers: int A[10]; To access an individual element we must apply a subscript to array named A. A subscript is a bracketed expression. The expression in the brackets is known as the index. First element of array has index 0. A[0] Second element of array has index 1, and so on. A[1], A[2], A[3],… Last element has an index one less than the size of the array. A[9] Back
9
Initialization of an array int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } ; All elements 0 The array size need not be specified explicitly. When initial values are include as a part of definition, the array will automatically set equal to the number of initial values included in the definition. int a[]={2,1,3,5,4}; Size=5; Back
10
Reading & Writing an Array Reading an array: An array can be read by reading the elements inside a loop that iterates as many times as there are elements in the array. For loop is generally used for this purpose for(i=0;i<n;i++) Scanf(“%d”,&a[i]); Writing an array: An array can be written by writing the elements inside a loop that iterate as many times as there are elementsin the array. For loop is generally used for this purpose. for(i=0;i<n;i++) printf(“%d”,a[i]); Back
11
Programs of one dimensional array 1)Searching Linear Search Binary Search 2) Sorting Bubble Sort Selection Sort Back
12
Program for Linear Search #include void main() { int a[20],i,n,d,c=0; clrscr(); printf("\n enter the value of n"); scanf("%d",&n); printf("\n enter the element"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\n enter the element to be searched"); scanf("%d",&d);
13
for(i=0;i<n;i++) if(a[i]==d) { printf("\n elements found at location%d",i+1); c++; } if(c==0) printf("Element not found"); getch(); } Back
14
Program for Binary Search #include void main() { int a[10],i,n,m,b,e,d; clrscr(); printf("Enter the size of an array"); scanf("%d",&n); printf("\nEnter the elements of the array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nThe elements of an array are"); for(i=0;i<n;i++) { printf(" %d",a[i]); }
15
printf("\nEnter the number to be search"); scanf("%d",&d); b=0; e=n-1; m=(b+e)/2; while((a[m]!=d)&&(b<=e)) { if(a[m]<d) b=m+1; else e=m-1; m=(b+e)/2; } if(a[m]==d) printf("Element is found at position%d",m+1); else printf("Element is not found"); getch(); } Back
16
Program for Bubble Sort #include main() { int nums[5]; int i,j,temp,n; clrscr(); printf("Enter size of an array"); scanf("%d",&n); printf("\n enter the array element"); for(i=0;i<n;i++) { scanf("%d",&nums[i]); }
17
//BUBBLE SORTING for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(nums[j]>nums[j+1]) { temp=nums[j]; nums[j]=nums[j+1]; nums[j+1]=temp; } printf("\n\n\t the sorted array is \n"); for(i=0;i<n;i++) { printf("\t%d",nums[i]); } getch(); } Back
18
Program for Selection Sort #include void main() { int nums[20]; int i,j,small,sp,t,n; clrscr(); printf("Enter value of n"); scanf("%d",&n); printf("\n enter the array element"); for(i=0;i<n;i++) { scanf("%d",&nums[i]); }
19
//SELECTION SORTING for(i=0;i<n-1;i++) {small=nums[i]; sp=i; for(j=i+1;j<n;j++) if(nums[j]<small) { small=nums[j]; sp=j; } t=nums[i]; nums[i]=nums[sp];//swapping nums[sp]=t; } printf("\n\n\t the sorted array is \n"); for(i=0;i<n;i++) {printf("\t%d",nums[i]);} getch();} Back Back
20
Thanks
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.