Arrays in C++
Arrays in C++
DATASTRUCTURES - ARRAYS
Searching methods in
array
• Linear Search In this method each element of the
displayed.
#include<iostream.h> #include<conio.h> int
Sort sorting”
<<endl; for(i=0; i<size; i++)
cout<<A[i]<<" "; getch(); return
#include<iostream.h> 0; }
#include<conio.h> void
Insertion_Sort(int A[], int size) {
int i,j,k,min; for(i=1; i<size; i++) {
Bubble sort
min = A[i]; j = i-1; while(j>=0 && #include<iostream.h>
A[j]>min) { #include<conio.h> void
A[j+1] = A[j]; j--; } A[j+1] = min; Bubble_Sort(int A[], int size) {
cout<<"\nArray after pass int temp,i,j,k; for(i=0; i<size; i++)
{ the array elements"<<endl;
for(j=0; j<(size-1)-i; j++) { for(i=0; i<size; i++)
if(A[j] > A[j+1]) { cin>>A[i];
temp = A[j]; A[j] = A[j+1]; A[j+1] cout<<endl<<"Original array
= temp; } } cout<<"\nArray after is"<<endl;
pass for(i=0; i<size; i++)
"<<i+1<<": "; for(k=0; k<size; cout<<A[i]<<" ";
k++) Bubble_Sort(A,size);
cout<<A[k]<<" "; } } cout<<endl<<"Array after
int main() { sorting "<<endl;
clrscr(); int A[10],size,i,item; for(i=0; i<size; i++)
cout<<"How many elements cout<<A[i]<<" "; getch(); return
dou you want to create array 0; }
with: ";
cin>>size; cout<<endl<<"Enter
Two-Dimensional Arrays
• A 2D array is an array which is stored in
the form of matrix.
• There are two ways of implementing 2-D
array in the memory ▫ Row Major
Implementation ▫ Column major implementation
Row major
implementation
Column major
implementation
Address of A[i][j]= B+ W [R(j-
lc) + (i- lr)] where B – Base address
of the array W – Width of the array
R- No. of rows of the original array
lr – Lowest row index lc – Lowest
column index
Practice Questions
Q:
Q: