Faculty: Department: Course: Course Code: Assignment Number: Year: (Part 2) Semester 1
Faculty: Department: Course: Course Code: Assignment Number: Year: (Part 2) Semester 1
Question: Write a program to enter numbers on a double dimension array, reverse the elements and display them Write a program to display the biggest an the smallest element in an array
Sorting programs: Bubble sort Selection sort Quick sort Insertion sort Shell sort Linear search
Solution Program to display elements in reverse from an array #include <conio.h> #include <iostream>
int main(){ int i,j, arr[2][4]; cout<<"please enter integers"<<endl ; for( i=0;i<2;i++){ for( j=0;j<4;j++){ cin>>arr[i][j] ;}} cout<<"the reverse is\n\n"<<endl;
cout<<arr[i][j] <<endl; }} getch(); return 0; } Program to display the biggest and the smallest of the two elements in an array
#include <conio.h> #include <iostream.h> voidshellsort(int a[],int n){ inti,j,k,m,mid ; for(m=n/2;m>0;m/=2){ for(j=m;j<n;j++){ for(i=j-m; i>=0;i-=m){ if (a[i+m]> a[i]) break; else { mid = a[i]; a[i]= a[i+m]; a[i+m]= mid; } } }
} }
int main(){ int a[10],i,n; clrscr(); cout<< "enter the number of elements" <<endl; cin>> n; for (i=0; i<n;i++) { cout<<endl<<"element "<< i+1<< ":\t"; cin>> a[i] ; } shellsort(a,n); cout<< "after sorting the array is: " ; for (i=0; i<n; i++) cout<< a[i] << " ";
cout<<endl<<endl<< "the smallest element is:" << a[0]; cout<<endl<< "the biggest element is :" << a[n-1]; getch(); return 0; }
#include <conio.h>
intarr[5],i,j,k,temp; clrscr();
cout<< "please enter the values for the array" <<endl; for (k=0; k<5;k++) cin>>arr[k];
for (i=0; i<=3; i++){ for (j=0; j<=3-i;j++){ if(arr[j]>arr[j+1]){ temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<endl<< "after sorting the array is"<<endl; for (k=0;k<5;k++) cout<<arr[k]<<"\t"; getch(); return 0; }
b) Shell sort #include <conio.h> #include <iostream.h> voidshellsort(int a[],int n){ inti,j,k,m,mid ; for(m=n/2;m>0;m/=2){ for(j=m;j<n;j++){ for(i=j-m; i>=0;i-=m){ if (a[i+m]> a[i]) break; else { mid = a[i]; a[i]= a[i+m]; a[i+m]= mid; } } } } }
clrscr(); cout<< "enter the number of elements" <<endl; cin>> n; for (i=0; i<n;i++) { cout<<endl<<"element "<< i+1<< ":\t"; cin>> a[i] ; } shellsort(a,n); cout<< "after sorting the array is: " ; for (i=0; i<n; i++) cout<< a[i] << " "; getch(); return 0; }