Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Notes

469 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Notes

  1. 1. By : Rakesh Kumar D.A.V. Centenary Public School ArrayArray is a collection of similar data types sharing a common name and one element can be distinguished using theirindex.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 ornegative sign.When an array is declared using it’s syntax. The compiler creates the defined number of blocks in continuation. Eachblock 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 50Initialization Method Declaration andMethod-I initialization simultaneouly int x[5] = {10,20,30,40,50 };Method –II Only when declaration + initialization simultaneously takesh place 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 SolutionWrite a program in C++ to read an array of integer of Write a program in C++ to read an array of integersize 10 . Also display the same on the screen. of size 10 and give an increment of 20 to each element. Also display this modified list on the screen#include<iostream> #include<iostream>#include<iomanip> #include<iomanip>#include<conio.h> #include<conio.h>using namespace std; using namespace std;int main() int main(){ { int x[10],i; int x[10],i; for(i=0;i<10;i++) // input phase { for(i=0;i<10;i++) cout<<"Enter x["<<i<<"] element {:"; cout<<"Enter x["<<i<<"] element cin>>x[i]; :"; } cin>>x[i]; cout<<"n Entered array :"; } 8
  2. 2. By : Rakesh Kumar D.A.V. Centenary Public School for(i=0;i<10;i++) // processing phase cout<<setw(6)<<x[i]; for(i=0;i<10;i++) getch(); x[i] = x[i]+20; return 0;} 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 Write a program in C++ to read an array of integerof size 10 and find out the sum of even element and of size 10 and swap it’s first element with the last halfthe sum of odd element. Also display this entered of it’s element and display the result on the screen.array and sums on the screen.#include<iostream> #include<iostream>#include<iomanip> #include<iomanip>#include<conio.h> #include<conio.h>using namespace std; using namespace std;int main() int main(){ { int x[10],i,seven,sodd; int x[10],i,temp,j; // input phase // input phase for(i=0;i<10;i++) for(i=0;i<10;i++) { { cout<<"Enter x["<<i<<"] element cout<<"Enter x["<<i<<"] element :";:"; cin>>x[i]; cin>>x[i]; } } // processing phase // processing phase seven=sodd=0; for(i=0,j=9;i<5;i++,j--) for(i=0;i<10;i++) { if(x[i]%2==0) temp = x[i]; seven +=x[i]; x[i] = x[j]; else x[j] = temp; sodd += x[i]; } // output phase // output phase cout<<"n Entered array :"; cout<<"n Swaped array :"; for(i=0;i<10;i++) for(i=0;i<10;i++) cout<<setw(6)<<x[i]; cout<<setw(6)<<x[i]; cout<<"n Sum of Even Element :"<<seven; getch(); cout<<"n Sum of Odd element :"<<sodd; return 0; 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; } 8
  3. 3. By : Rakesh Kumar D.A.V. Centenary Public School// function to display an arry on the screenvoid 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 methodvoid 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 keyboardvoid 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 screenvoid 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 sortvoid selection_sort(int x[],int n){ int i,j,pos,low, temp; for(i=0;i<n-1;i++) { pos = i; 8
  4. 4. By : Rakesh Kumar D.A.V. Centenary Public School 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 keyboardvoid 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 screenvoid 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 sortvoid 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; } 8
  5. 5. By : Rakesh Kumar D.A.V. Centenary Public School 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 keyboardvoid 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 screenvoid output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return;}// search an element using binary searchvoid 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 Xn"; input(x,5); cout<<"n ARRAY Yn"; 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;} 8
  6. 6. By : Rakesh Kumar D.A.V. Centenary Public School Merging#include<iostream>#include<iomanip>#include<conio.h>using namespace std;// function to read an array from the keyboardvoid 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 screenvoid output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return;}// merge two array to produce third arrayvoid 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 Xn"; input(x,5); cout<<"n ARRAY Yn"; 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; 8
  7. 7. By : Rakesh Kumar D.A.V. Centenary Public School// function to read an array from the keyboardvoid 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 screenvoid output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return;}// search an element using linear searchint 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 keyboardvoid 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 screenvoid output(int x[],int n) { 8
  8. 8. By : Rakesh Kumar D.A.V. Centenary Public School for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return;}// search an element using binary searchint 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;} 8

×