Take a look please

[code]#include <iostream>
#include <cstdlib> #include <stdlib.h>
1
2
3
4
5
6
7
#include <time.h>

using namespace std;

const int MLS = 50;
const int SENTINEL = -1;
typedef int element;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class AList {
        private:
                element items[MLS];
                int size;
                int Read_int();
                void Swap(int pos1, int pos2);
                element Read_element();
                void exit_method();
        public:
                void Menu();
                void Read();
                //void Ran_Gen();
                void BubbleSort();
                void SelectionSort();
                void InsertionSort();

1
2
3
4
5
6
7
8
9
void Print();
                void LinearSearch(element target,bool&found,int&position);
                void BinarySearch(element target,bool&found,int&position);
};

int main() {
        srand(int(time(0)));
        cout<< "\n\nSort and Search Demo Program, version 1.0\n"
            << "(c) 2012,\n\n";

1
2
3
4
5
6
7
8
9
10
11
12
13
AList B;
                B.Menu();
              //  B.LinearSearch();
				//B.BinarySearch();
				system("PAUSE");
}

void AList::Menu() {
        element menuchoice;

       element target_linear;
        bool found_linear;
        int position_linear;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
element target_binary;
        bool found_binary;
        int position_binary;

        const int keyboard=1,
                  random=2,
                  bubble=3,
                  insertion=4,
                  selection=5,
                  linear=6,
                  binary=7,
                  exit=8;
        do {
        cout<< "Current List: ";
        if (menuchoice>0)
                Print();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
else
                cout<< "(empty)\t";
        if ((menuchoice>2)&&(menuchoice<6))
                cout<<"(list is known to be ordered)\n\n";
        else
                cout<<"(list is not know to be ordered)\n\n";
        cout<< "Actions:\n";
        cout<< "  1.  Reset the current list from the keyboard\n";
        cout<< "  2.  Reset the current list using randomly generated "
            << "elements\n";
        cout<< "  3.  Perform Bubble Sort on the current list\n";
        cout<< "  4.  Perform Insertion Sort on the current list\n";
        cout<< "  5.  Perform Selection Sort on the current list\n";
        cout<< "  6.  Perform Linear Search on the current list\n";
        cout<< "  7.  Perform Binary Search on the current list\n";
        cout<< "  8.  Quit the program\n\n";


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 cout<< "Choose an action:  ";
        menuchoice=Read_int();

   cout<< endl;
        switch (menuchoice) {
                case 1:
                Read();
                break;
               /* case 2:
                Ran_Gen();
                break;*/
                case 3:
                BubbleSort();
                break;
                case 4:
                InsertionSort();
                break;
                case 5:
                SelectionSort();
                break;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* case 6:{
                LinearSearch(target_linear,found_linear,position_linear);
                }
                break;
                case 7: {
                BinarySearch(target_binary,found_binary,position_binary);
                cout<< "The target was "<<found_binary<< " on the"
                    << " current list in position "<<position_binary;
                cout<< endl<<endl;
                }
                break;*/
               /* case 8:
                exit_method();
                break;
                default: cout<< "Please enter a valid menu choice: ";
                menuchoice=Read_int();
                ;*/
                }
        }

        while (menuchoice !=8);

}

void AList::Print() {
        for (int i=0; i<size; i++)
                cout<< items[i]<<" ";
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*void AList::Ran_Gen() {
        int usersize;
        int max;
        int min;
        int x;
        
  cout<< "Enter low: ";
        cin>> min;
        cout<< "Enter high: ";
        cin>> max;
        cout<< "Enter size: ";
        cin>> usersize;



for (int i = 0; i <usersize; i++)
       x = rand()+(max-min+1)+min;

        size = x;
*/
void AList::InsertionSort()	{
int j;
bool done;
for (int i=1; i<size; size++)
j=i;
done = false;
while ((j>=1) && (!done))
if (items[j] < items[j-1])	{
Swap (j,j-1);
j--;
}
else 
done=true;
}

void AList::SelectionSort()	{
int maxpos;
for (int i = size-1; i<0; i--)	{
maxpos = 0;
for (int j=1; j<=i; j++)
if (items[j] < items[maxpos])
maxpos = j;
else;
Swap(maxpos, i);
}
}


element Read_element()	{
element userval;
cin>> userval;
while ( !cin.good());
cout<< "invalid choice please enter a whole number between 1 and"
<< " 1000: "; 
cin.clear();
cin.ignore(80,'\n');
cin>> userval;
return userval;
}


int AList::Read_int()	{
int val;
cin>> val;
while (!cin.good())	{
cout<< "invalid menu choice please try again: ";
cin.clear();
cin.ignore(80,'\n');
cin>>val;	}

return val;
}
void AList::BubbleSort()	{
for (int i=0; size-1; i++){
for (int j=0; j<size-1-i; j++)	
if (items[j]>items[j+1])
Swap (j, j+1);

else
;
}
}
void AList::Swap(int pos1, int pos2)	{
element temp;
temp = items[pos2];
items[pos2]=items[pos1];
items[pos1]=temp;
}

void AList::Read()	{
element userval;
cout<< "Enter elements : ";
while ((userval!=SENTINEL) && (size<MLS))	{
items [size] = userval;
size ++;
}
}

void LinearSearch( )
{
}
void BinarySearch ()
{
}

[/code]
Last edited on
Use code <> Tags
And what errors/problems are you having?
I put every thing but I have nothing on the screen to be displayed
Topic archived. No new replies allowed.