Skip to main content

Shyam has a lot of books messed on the floor. Therefore, he wants to pile up the books that still have some remaining exercises into a single pile. He will grab the books oneby-one and add the books that still have remaining exercises to the top of the pile. Whenever he wants to do a book exercise, he will pick the book with the minimum number of remaining exercises from the pile. In order to pick the book, he has to remove all the books above it. Therefore, if there are more than one books with the minimum number of remaining exercises, he will take the one which requires the least number of books to remove. The removed books are returned to the messy floor. After he picks the book, he will do all the remaining exercises and trash the book. Since number of books is rather large, he needs your help to tell him the number of books he must remove, for picking the book with the minimum number of exercises. Whenever he wants to do a book exercise, there is at least one book in the pile. Implement a program for this scenario

 #include<iostream>

using namespace std;

class Books

{

public: 

string subName[50];

int subEx[50], num;

void readData()

{

int i;

cout << "Enter number of books: ";

cin >> num;

for(i=0; i<num; i++)

{

cout << "Enter book name: ";

cin >> subName[i];

cout <<"Enter number of exercises in " << subName[i] << ": ";

cin >> subEx[i];

}

}

void sortStack()

{

string tempName;

int i, j, tempEx;

for(i=0; i<num-1; i++)

{

for (j=0; j<num-i-1; j++)

{

if(subEx[j] > subEx[j+1])

{

tempEx = subEx[j];

subEx[j] = subEx[j+1];

subEx[j+1] = tempEx;

tempName = subName[j];

subName[j] = subName[j+1];

subName[j+1] = tempName;

}

}

}

}

void displayStack()

{

int j;

for(j=0;j<num;j++)

cout<<"\nRemaining exercies in " << subName[j]<< " are: " << subEx[j];

cout << endl;

}

};

int main()

{

Books O1;

O1.readData();

O1.sortStack();

O1.displayStack();

}






Comments

Total Pageviews