8

Click here to load reader

Array notes

Embed Size (px)

Citation preview

Page 1: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

Array Array is a collection of similar data types sharing a common name and one element can be distinguished using their index. Syntax to declare Array datatype identifier[size]; Example int x[10]; float f[20] The size of array can not be variable, it is recommended as a integer constant values without any type of positive or negative sign. When an array is declared using it’s syntax. The compiler creates the defined number of blocks in continuation. Each block is assigned a unique index number which start from 0(Zero). Example x[0] x[1] x[2] x[3] x[4]

10 20 30 40 50 Initialization Method Method-I int x[5] = {10,20,30,40,50 }; Method –II int x[] = { 10,20,30,40,50 }; Method –III int x[5]; x[0] =10; x[1]=20; x[2]=30; x[3]=40; x[4]=50; Method –IV int x[5]; cin>>x[0]>>x[1]>>x[2]>>x[3]>>x[4]; Method V int x[5]; For(i=0;I,5;i++) cin>>x[i]; Some Sample Question and Their Solution Write a program in C++ to read an array of integer of size 10 . Also display the same on the screen.

Write a program in C++ to read an array of integer of size 10 and give an increment of 20 to each element. Also display this modified list on the screen

#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x[10],i; for(i=0;i<10;i++) { cout<<"Enter x["<<i<<"] element :"; cin>>x[i]; } cout<<"\n Entered array :";

#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x[10],i; // input phase for(i=0;i<10;i++) { cout<<"Enter x["<<i<<"] element :"; cin>>x[i]; }

Declaration and initialization simultaneouly

Only when declaration + initialization simultaneously takesh place

Page 2: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

for(i=0;i<10;i++) cout<<setw(6)<<x[i]; getch(); return 0; }

// processing phase for(i=0;i<10;i++) x[i] = x[i]+20; cout<<"\n Entered array :"; for(i=0;i<10;i++) cout<<setw(6)<<x[i]; getch(); return 0; }

Write a program in C++ to read an array of integer of size 10 and find out the sum of even element and the sum of odd element. Also display this entered array and sums on the screen.

Write a program in C++ to read an array of integer of size 10 and swap it’s first element with the last half of it’s element and display the result on the screen.

#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x[10],i,seven,sodd; // input phase for(i=0;i<10;i++) { cout<<"Enter x["<<i<<"] element :"; cin>>x[i]; } // processing phase seven=sodd=0; for(i=0;i<10;i++) if(x[i]%2==0) seven +=x[i]; else sodd += x[i]; // output phase cout<<"\n Entered array :"; for(i=0;i<10;i++) cout<<setw(6)<<x[i]; cout<<"\n Sum of Even Element :"<<seven; cout<<"\n Sum of Odd element :"<<sodd; getch(); return 0; }

#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x[10],i,temp,j; // input phase for(i=0;i<10;i++) { cout<<"Enter x["<<i<<"] element :"; cin>>x[i]; } // processing phase for(i=0,j=9;i<5;i++,j--) { temp = x[i]; x[i] = x[j]; x[j] = temp; } // output phase cout<<"\n Swaped array :"; for(i=0;i<10;i++) cout<<setw(6)<<x[i]; getch(); return 0; }

Bubble Sort ( Ascending Order) #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; }

Page 3: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

// function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // function to sort an array using bubble sort method void bubble_sort(int x[],int n) { int i,j,temp; for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) { if(x[j]>x[j+1]) { temp = x[j]; x[j] = x[j+1]; x[j+1] = temp; } } return; } int main() { int x[10]; input(x,10); bubble_sort(x,10); cout<<"\n Sorted Array :"; output(x,10); getch(); return 0; }

Selection Sort

#include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // function to sort an array using selection sort void selection_sort(int x[],int n) { int i,j,pos,low, temp; for(i=0;i<n-1;i++) { pos = i;

Page 4: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

low = x[i]; for(j=i+1;j<n-1;j++) { if(low>x[j]) { low = x[j]; pos=j; } } temp = x[i]; x[i] = x[pos]; x[pos] = temp; } } int main() { int x[10]; input(x,10); selection_sort(x,10); cout<<"\n Sorted Array :"; output(x,10); getch(); return 0; }

INSERTION SORT

#include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // function to sort an array using insertion sort void insertion_sort(int x[],int n) { int i,j,temp; for(i=1;i<n;i++) { temp = x[i]; j = i-1 ; while(temp<x[j] && j>=0) { x[j+1] = x[j]; j = j-1; } x[j+1]= temp; }

Page 5: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

return; } int main() { int x[10]; input(x,10); insertion_sort(x,10); cout<<"\n Sorted Array :"; output(x,10); getch(); return 0; }

Contatenation #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // search an element using binary search void concate(int x[],int m,int y[],int n, int z[]) { for(int i=0;i<m;i++) z[i] = x[i]; for(int i=0;i<n;i++) z[m+i] = y[i]; return; } int main() { int x[5],y[10],z[15]; cout<<"\n ARRAY X\n"; input(x,5); cout<<"\n ARRAY Y\n"; input(y,10); concate(x,5,y,10,z); system("cls"); cout<<"\n Array A : "; output(x,5); cout<<"\n Array B : "; output(y,10); cout<<"\n Concatenated Array : "; output(z,15); getch();

return 0;}

Page 6: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

Merging #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // merge two array to produce third array void concate(int a[],int m,int b[],int n, int c[]) { int i,j,k; i=j=k=0; while(i<m && j<n ) if(a[i]<b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; while(i<m) c[k++]= a[i++]; while(j<n) c[k++]= b[j++]; return; } int main() { int x[5],y[10],z[15]; cout<<"\n ARRAY X\n"; input(x,5); cout<<"\n ARRAY Y\n"; input(y,10); concate(x,5,y,10,z); system("cls"); cout<<"\n Array A : "; output(x,5); cout<<"\n Array B : "; output(y,10); cout<<"\n Merged Array : "; output(z,15); getch(); return 0;}

Linear Search #include<iostream> #include<iomanip> #include<conio.h> using namespace std;

Page 7: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

// function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // search an element using linear search int linear_search(int x[],int n,int data) { int i,found =0; for(i=0;i<n;i++) { if(x[i]==data) found =1; } return found; } int main() { int x[10],data; input(x,10); cout<<"\n Enter element to search :"; cin>>data; int res = linear_search(x,10,data); cout<<"\n Entered Array :"; output(x,10); if(res ==1) cout<<"\n Given data available in given array "; else cout<<"\n Given data not available in given array "; getch(); return 0; }

Binary Search #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) {

Page 8: Array notes

By : Rakesh Kumar D.A.V. Centenary Public School

8

for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // search an element using binary search int binary_search(int x[],int n,int data) { int first,last,mid ,found =0; first =0; last = n-1; while(first<=last && found ==0) { mid = (first+last)/2; if(x[mid] == data) found =1; else if(x[mid]>data) last = mid-1; else first = mid+1; } return found; } int main() { int x[10],data; input(x,10); cout<<"\n Enter element to search :"; cin>>data; int res = binary_search(x,10,data); cout<<"\n Entered Array :"; output(x,10); if(res ==1) cout<<"\n Given data available in given array "; else cout<<"\n Given data not available in given array "; getch(); return 0; }