Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

Shell sort won't work in this program (the algorithm itself works just fine)

$
0
0
Code:

#include <iostream>
using namespace std;

class CD
{
public:
        static const int num = 100;
        char publisher[num], title[num], location[num];
        int year;
public:
        void virtual input()=0;
        void virtual output()=0;
};

class Classical: public CD
{
protected:
        static const int num = 100;
        char composer[num], conductor[num];
public:
        void virtual input()=0;
        void virtual output()=0;
};

class Popular: public CD
{
private:
        static const int num=100;
        char name_of_the_band[num], composer[num], leading_performer[num];
public:       
        void input()
        {        cout << "\nPublisher        : ";
        cin >> publisher;
        cout << "Title            : ";
        cin >> title;
        cout << "Location        : ";
        cin >> location;
        cout << "Year            : ";
        cin >> year;
        cout << "Name of the band : ";
        cin >> name_of_the_band;
        cout << "Composer        : ";
        cin >> composer;
        cout << "Leading performer: ";
        cin >> leading_performer;       
}
        void output()
        {
                cout << "\nPublisher        : " << publisher;
                cout << "\nTitle            : " << title;
                cout << "\nLocation          : " << location;
                cout << "\nYear              : " << year;
                cout << "\nName of the band  : " << name_of_the_band;
                cout << "\nComposer          : " << composer;
                cout << "\nLeading performer : " << leading_performer;
        }
};

class Symphony: public Classical
{
private:        static const int num=100;
                        char orchestra[num], location[num];
public:                void input(){
                cout << "\nPublisher: ";
                cin >> publisher;
                cout << "Title    : ";
                cin >> title;
                cout << "Location : ";
                cin >> location;
                cout << "Year    : ";
                cin >> year;
                cout << "Composer : ";
                cin >> composer;
                cout << "Conductor: ";
                cin >> conductor;
                cout << "Orchestra: ";
                cin >> orchestra;
                cout << "Location : ";
                cin >> location;}
        void output()
        {        cout << "\nPublisher        :" << publisher;
                cout << "\nTitle            :" << title;
                cout << "\nLocation          :" << location;
                cout << "\nYear              :" << year;
                cout << "\nComposer          :" << composer;
                cout << "\nConductor        :" << conductor;
                cout << "\nOrchestra        :" << orchestra;
                cout << "\nLocation          :" << location;}};
void ShellSort(CD *arr[],int n); //function prototype for our Shell sort algorithm

void main()
{
        CD *cdptr[100]; //pointer declaration for each of our class
        Popular *popptr;
        Symphony *symptr;
        int n=0, choose, symphonyCD, popularCD, pop;
        char terminate;

        cout << "\t\tEnter 2 or more CD-s to sort!" << endl;
        do // do-while statement which allows the user to enter data and terminate the program when he wants
        {
                        cout << "\n1.Classical";
                        cout << "\n2.Popular";
                        cout << "\n3.Sort";
                        cout << "\n4.Display";
                        cout << "\n5.Terminate program";
                        cout << endl << endl;
                        cout << "\nChoose category: ";
                        cin>>choose;
                        switch(choose)                                                        //switch for the user to choose the type of input
                        {
                        case 1:
                                cout << "\nChoose category: ";
                                cout << "\n1.Symphony";
                            cout << endl << endl;
                                cin >> pop;
                                if(pop==1) //if-else statement to chose the type of classical CD
                                {
                                        cout << "\nEnter number of symphony cd: ";
                                        cin >> symphonyCD;
                                        int static b;
                                        for(int i=0; i<symphonyCD;i++)
                                        {
                                                cout << "\nNr. " << ++b << endl;
                                                symptr = new Symphony;
                                                symptr->input();
                                                cdptr[n++]=symptr;
                                        }
                                }
                                else
                                        cout << "\nNot recognized value!";
                                break;
                        case 2:
                                cout << "\nEnter number of popular cd: ";
                                cin >> popularCD;
                                int static a;
                                for(int i=0; i<popularCD;i++)
                                {
                                        cout << "\nNr. " << ++a << endl;
                                        popptr = new Popular;
                                        popptr->input();
                                        cdptr[n++]=popptr;
                                }
                                break;
                        case 3:
                                ShellSort(cdptr,100); //function call statement for our Shell Sort algorithm
                                break;
                        case 4:
                                if(n==0)
                                cout << "\nNo data entered!" << endl;
                                for(int i=0; i<n;i++)
                                {
                                        cdptr[i]->output();
                                        cout << endl;
                                }
                                break;
                        case 5:
                                cout << "\nDo you want to terminate the program? (y/n)";
                                cin >> terminate;
                                break;
                        default:
                                cout << "\nNot recognized value!" << endl;
                        }
        }while(terminate!='y');
       
        system("pause");
}
void ShellSort(CD *arr[],int n)
{
        int i,j,increment;
        CD *temp;
        for(increment=n/2; increment>0; increment /= 2)
                {
                        for(i=increment; i<n; i++)
                        {
                                temp=arr[i];
                                for(j=i; j>=increment; j -= increment)
                                {
                                        if(temp->title < arr[j-increment]->title)
                                        {
                                        arr[j] = arr[j-increment];
                                        }
                                        else break;}
                                arr[j] = temp;}}}


Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images