Have to code a selection sort.

So I am in class, and we are supposed to create a selection sort using a class List. He is very specific about all the functions he wants in the class, etc. I am having trouble with two things.

1. The formatted output should have only 10 numbers per line. The c variable in the Print function should be taking care of this, but seems to not be working.

2. My sort algorithm effectively seems to be broken, but I'm not sure what I missed. My input is:

-54 5000 -31 17 54 -17 41 201 16 -54 97 405 5 7 -11 13 -17 19 -23 31 61 -59 432 -201

and my output is:

-201 -31 17 54 -17 41 201 16 -54 97 405 5 7 -11 13 -17 19 -23 31 61 -59 432 -54 5000

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
 //#include "List01.h"
#include <iostream>
#include <cstdlib> 
#include <cstring> 
#include <fstream> 
#include <string> 
#include <iomanip>
using namespace std; 

class List 
{
	int size;
	int count;
	
	int* L;
	public:
		List(int sz=100):size(sz),count(0){L=new int[sz];}
		
		~List(){if(L) delete[]L;}
		
		struct ListException {
			ListException(const char* m) {
				cout << endl;
				cout << "The list is " << m << ".";
				cout << endl;
			}
		};
		
		bool isFull(void) {return count >= size - 1;}
		
		void Insert(int v)
		{
			if (isFull()) throw ListException("Full");
			L[count++] = v;
		}
		
		void Scan(istream& i) 
		{
			for( ; ; ) {
				int v;
				i >> v;
				if(i.eof()) break;
				Insert(v);
			}
		}
		
		void Print(ostream& o, const char* title){
			o << title << endl;
			for (int a = 0; a <= count-1;a++) {
				int c = 0;
				if (c >= 9) {
					cout << endl;
					c = 0;
				}
				o << setw(5) << right<< L[a];
				c++;
			}
			o << endl << endl;
		}
		void Sort(void){
			int eol = count -1;
			while (eol >= 1) {
				int iom = 0;
				int i = 1;
				while (L[i] >= L[iom]){
					iom = i;
					i++;
				}
				swap(L[iom], L[eol]);
				eol--;
			}
		}
};

void ListMgr(istream& i,ostream& o) {
	List L;
	L.Scan(i);
	L.Print(o, "Unsorted List");
	L.Sort();
	L.Print(o, "Sorted List");
}

int main()
{
	ListMgr(cin, cout);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
		void Sort(void){
			int eol = count -1;
			while (eol >= 1) {
				int iom = 0;
				int i = 1;
				//¿what is this loop supposed to do?
				while (L[i] >= L[iom]){
					iom = i;
					i++;
				}
				swap(L[iom], L[eol]);
				eol--;
			}
		}
Topic archived. No new replies allowed.