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
Pizza parlor accepting maximum M orders. Orders are served in first come first served basis. Order once placed cannot be cancelled. Write C++ program to simulate the system using circular queue using array.
---------------------------------------------------------------------------------
#include<iostream>
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
using namespace std;

class pizza{
int order[10];
int max;
int f,r;

public :
pizza(){
f=-1,r=-1;

cout<<"\nEnter Maximum order : ";
cin>>max;
}

int full(){
  if(((f==0)&&(r==(max-1)))||(f==(r+1)%max))
         return 1;
     else
         return 0;
}
int qempty(){
        if(f==-1)
            return 1;
        else
            return 0;
    }

void add(int a){
if(full()){
cout<<"\nOrder is Full!!!";
}
else{
if(f==-1){
f=r=0;
}
else{
r=(r+1)%max;
}
order[r]=a;
}
}

void remove(){
int i;
i=order[f];
      if(f==r){
f=r=-1;
}
else{
f=(f+1)%max;
}
cout<<"\n Order deleted : "<<i;


}

void display(){
int temp;
temp=f;
if(qempty())
    {
        cout<<"\nNo orders currently\n";

    }
else{
cout<<"\nThe oders are : \n\n";
while(temp!=r){
cout<<"  "<<order[temp];
temp=(temp+1)%max;
}
cout<<"  "<<order[temp];
}}

};

int main(){

int ch;
pizza p;
do{
cout<<"\n1. Order \n2. Remove order \n3.Display orders \n4. Exit";
cin>>ch;
switch(ch){
case 1:int o;
      cout<<"\nEnter Order number : ";
      cin>>o;
      p.add(o);
      break;
case 2:p.remove();
      break;
case 3:p.display();
      break;

}

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

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



**If some mistakes, plz let me know!-----Vivek S. Sharma
A double-ended queue(deque) is a linear list in which additions and deletions may be made at either end. Obtain a data representation mapping a deque into a one-dimensional array. Write C++ program to simulate deque with functions to add and delete elements from either end of the deque.
--------------------------------------------------------------------------------------------------------------------------

#include<iostream>
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
using namespace std;
#define size 5
class dqueue{
      int q[size];
      int f,r;
public :
      dqueue(){
            f=r=-1;
            q[-1]=NULL;
      }

      void display(){
            for(int i=f ; i<=r ; i++){
                  cout<<"\n"<<q[i];
            }
      }
      void insert_end(int data){
            if(r==size-1){
                  cout<<"\nNotpossibel";
            }
            else{
            if(f==-1 && r==-1){
                              f=r=0;
                              q[r]=data;
                        }
                        else{
                              r=r+1;
                              q[r]=data;
                        }

                        display();
                  }}
      void delete_end()
      {
              if(f==-1)
              {
                  cout<<"deletion is not possible::dequeue is empty";
                  return;
              }
              else
              {
                  cout<<"the deleted element is"<<q[r];
                  if(f==r)
                  {
                      f=r=-1;
                  }
                  else
                      r=r-1;
              }
              display();
      }
      void delete_front(){
            if(f==-1)
                    {
                        cout<<"deletion is not possible::dequeue is empty";
                        return;
                    }
                    else
                    {
                        cout<<"the deleted element is"<<q[f];
                        if(f==r)
                        {
                            f=r=-1;
                            return;
                        }
                        else
                            f=f+1;
                    }
            display();

      }
      void insert_begin(int data){

            if(f==-1 && r==-1){
                  f=r=0;
                  q[f]=data;
            }

            else if(f!=0){
                  f=f-1;
                  q[f]=data;
            }
            else
                  cout<<"\nNot possible ";

            display();
      }
};
int main(){
      int ch;
      int data;
      dqueue q;
      do{
            cout<<"\n1.Add at begin \n2.Add at end \n3.Delete front \n4.Delete end\n5.Exit";
            cin>>ch;
            switch(ch){
            case 1 : cout<<"\nEnter data : ";
                        cin>>data;
                        q.insert_begin(data);
                        break;
            case 2 : cout<<"\nEnter data : ";
                        cin>>data;
                        q.insert_end(data);
                        break;
            case 3 :q.delete_front();break;
            case 4 :q.delete_end();break;
            }
      }while(ch!=5);
      return 0;

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



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