Issues with class/polymorphism

I have written a program with a class abstractSort that is to sort an array and count the number of times through the counter. Program 1 runs and counts, but does not sort the random list from low to high. Program 2 does better at the sort but does not count the times through the loop to sort it.

PROGRAM !

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
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>	
#include <cstdlib> 		
using namespace std;
	
class AbstractSort					
{
public:
	virtual void sort(int arr[], int size) = 0;
	int getComparisonCount()
	{
		return comparisonCount;
	}
		void resetComparisonCount()
	{
			comparisonCount= 0;
	}

protected:
			int compare(int x, int y);
private:
			int comparisonCount;
};

//	AbstractSort::compare
//		Returns  -1, 0, or 1 a la strcmp
//		This also keeps track of the number of comparisons performed

int	AbstractSort:: compare(int x, int y)	//compare func
		{
			comparisonCount++;
			return x - y;
		}
// derived class
		class MaxSort : public AbstractSort
		{
		public:
			void sort(int arr[], int size);
		};
		//MaxSort::sort  sort the given array with the given number of elements

		void MaxSort::sort (int arr[], int size)
		{
			resetComparisonCount();
			    int indexOfMin; 
				int pass; 
				int j; 

			for ( pass = 0; pass < size - 1; pass++ ) 
			{ 
				indexOfMin = pass; 

				 for ( j = pass + 1; j < size; j++ ) 
					if ( arr[j] < arr[pass] ) 
						indexOfMin = j; 
                                                                                compare (arr[j], arr[pass]);
					swap ( arr[pass], arr[indexOfMin] ); 
			} 
		} 

// swap function for integers 
void swap ( int& x, int& y ) 
{ 
   int temp; 
   temp = x; 
   x = y; 
   y = temp; 
} 

int main()
{
    const int MAX_SIZE = 100;
    int arr[MAX_SIZE];
    int size;
    
    // Explain the program
    cout << "This program keeps track of the number of comparisons required to\n"; 
    cout << "to sort a randomly generated array.\n";
    cout << "How large do you want the array to be (max size=100)? ";
    
    // Get the size of the array
    cin >> size;    
    if (size > MAX_SIZE) 
    {
        cout << "The size of the array must be no greater than 100.";
        exit(1);
    }
    
    // Initialize random number generator
    srand(time(0));
    
    // Fill the array with random numbers
    for (int k = 0; k < size; k++)
      { 
		  arr[k] = rand() % 1000;
	  }
    // Output array to be sorted
    cout << "Array to be sorted is: \n";
    for (int k = 0; k < size; k++)
	{
        cout << arr[k] << "  ";
	}
    // Sort and output results
    MaxSort maxSort1;
    maxSort1.sort(arr, size);
    cout << "\nThe sorted array is: \n";
    for (int k = 0; k < size; k++)
	{
        cout << arr[k] << "  ";
	}
    cout << "\nNumber of comparisons performed is: " << maxSort1.getComparisonCount() << endl;
 
	system("pause");
	return 0;
}


PROGRAM 2- This has a switch sort function but does not count
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
107
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>	
#include <cstdlib> 		
using namespace std;

class AbstractSort					
{
public:
	virtual void sort(int arr[], int size) = 0;
	int getComparisonCount()
	{
		return comparisonCount;
	}
		void resetComparisonCount()
	{
			comparisonCount= 0;
	}

protected:
			int compare(int x, int y);
private:
			int comparisonCount;
};

//	AbstractSort::compare
//		Returns  -1, 0, or 1 a la strcmp
//		This also keeps track of the number of comparisons performed

int	AbstractSort:: compare(int x, int y)	//compare func
		{
			comparisonCount++;
			return x -y;
		}
// derived class
		class MaxSort : public AbstractSort
		{
		public:
			void sort(int arr[], int size);
		};
		//MaxSort::sort  sort the given array with the given number of elements
		void MaxSort::sort (int arr[], int size)
		{
			resetComparisonCount();
			for (int k = size = size-1; k>=1; k--)
			{
			int indexOfLargest =0;
			for (int ix = 1; ix <= k; ix++)
			{
				if (compare (arr[ix], arr[indexOfLargest]) > 0)
				{
					indexOfLargest = ix;
				}
			}
		swap(arr[indexOfLargest], arr[k]);
			}
		}

int main()
{
    const int MAX_SIZE = 100;
    int arr[MAX_SIZE];
    int size;
    
    // Explain the program
    cout << "This program keeps track of the number of comparisons required to\n"; 
    cout << "to sort a randomly generated array.\n";
    cout << "How large do you want the array to be? ";
    
    // Get the size of the array
    cin >> size;    
    if (size > MAX_SIZE) 
    {
        cout << "The size of the array must be no greater than 100.";
        exit(1);
    }
    
    // Initialize random number generator
    srand(time(0));
    
    // Fill the array with random numbers
    for (int k = 0; k < size; k++)
      { 
		  arr[k] = rand() % 1000;
	  }
    // Output array to be sorted
    cout << "Array to be sorted is: \n";
    for (int k = 0; k < size; k++)
	{
        cout << arr[k] << "  ";
	}
    // Sort and output results
    MaxSort maxSort;
    maxSort.sort(arr, size);
    cout << "\nThe sorted array is: \n";
    for (int k = 0; k < size; k++)
	{
        cout << arr[k] << "  ";
	}
    cout << "\nNumber of comparisons performed is: " << maxSort.getComparisonCount() << endl;
	
 
	system("pause");
	return 0;
}

Last edited on
Topic archived. No new replies allowed.