Thursday, 12 October 2017

Write C++ program to store first year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using
a) Selection Sort b) Bubble sort and display top five scores
---------------------------------------------------------------------------------
#include<iostream>
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
using namespace std;
class fe{
int m[100];
int n;
public :
void get(){
cout<<"\nEnter no. of students :";
cin>>n;
cout<<"\nEnter the marks : ";
for(int i=0 ; i<n ; i++){
cin>>m[i];
}
}

void display(){
cout<<"\nMarks of the students are :\nStudent\tMarks";
for(int i=0 ; i<n ;i++){
cout<<"\nStudent "<<i+1<<" :\t"<<m[i];
}
}

void top_five(){

cout<<"\n\nThe top five scores are : ";
if(n>=5){
for(int i=n-1 ; i>=n-5 ; i--){
cout<<"\n"<<m[i];
}}
else{
      for(int i=n-1 ; i>=0 ; i--){
      cout<<"\n"<<m[i];
}
}
}
void bubble_sort(){
int temp;
cout<<"\n\nBubble Sort :";
for(int i=1 ; i<n ; i++){
temp=0;
for(int j=0 ; j<n ; j++){
if(m[j]>m[j+1]){
temp=m[j];
m[j]=m[j+1];
m[j+1]=temp;
}
}
}
cout<<"\nSorted marks after Bubble Sort : ";
display();
}

void selection_sort(){
int temp;
cout<<"\n\nSelection Sort : ";
for(int i=0 ; i<n ;i++){
temp=0;
for(int j=i+1 ; j<n ; j++){
if(m[i]>m[j]){
temp=m[i];
m[i]=m[j];
m[j]=temp;
}
}
}
cout<<"\nSorted marks after SElection Sort :";
display();
}

};
int main(){
fe a;
a.get();
cout<<"\nBefore sorting : ";
a.display();
int ch;
do{
      cout<<"\n\nEnter choice : \n1.Selection Sort \n2.Bubble Sort \n3.Top 5 Scores \n4.Exit";
      cin>>ch;
      switch(ch){
      case 1: a.selection_sort();break;
      case 2: a.bubble_sort();break;
      case 3: a.top_five();break;

      };
}while(ch!=4);
return 0;

}
--------------------------------------------------------------------------------------------------------------------------



**If some mistakes, plz let me know!-----Vivek S. Sharma

No comments:

Post a Comment