Sorting values of an array?

Hello World, I am having difficulty getting this final section of code to display correctly. My hopes is that my sort() function will sort the 20 randomly generated numbers in order from smallest to largest.
Unfortunately at this point is just re-displays the same order of the original line of random numbers produced by the array.

I am new, so I understand this may have been discussed before, but I couldn't find any similar articles to my dilemma.
Thank you for your time and patience in advance,
pdx.iter




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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

#include<iostream> // used for input output
#include<stdlib.h> // used for the random number generator
#include<time.h> // used for seeding the random number generator

using namespace std;

class ArrayStuff

{

#define MAX 20 // constant

	// declare the public variables and functions
public:
		int someArray[MAX];	// this is the array
		void seedRandom();	// this seeds the random number generator
		ArrayStuff();		// this is the constructor
		void printArray();	// function prints the array to the output panel
		void printReverseArray(); // function prints reverse array to the output panel
		int minimum();
		int maximum();
		int sum();
		void swap (int, int);
		int minPosition (int);
		void sort();

};

void seedRandom(unsigned int uSeed = 0)
	{
		if (uSeed == 0)
			uSeed=(unsigned)time(NULL);
		srand(uSeed);

	}

ArrayStuff::ArrayStuff()  // constructor
	{
	someArray[MAX]; 
	for ( int i = 0; i < MAX; i ++ )
			someArray[i] = rand()%99 + 1; // returns random integers 1-99
	}

void ArrayStuff::printArray() // prints the array to the panel
	{
	for( int i = 0; i < MAX; i++ )
			cout << someArray[i] << " ";
	cout << endl;
	}	// end printArray

void ArrayStuff::printReverseArray()// prints the array to the panel
	{
	for( int i = MAX -1; i >= 0; i-- )
			cout << someArray[i] << " ";
	cout << endl;
	}// end printReverseArray()

int ArrayStuff::minimum() // determines the smallest number in  array
{
	int smallest = someArray[0];
	for ( int i = 1; i < MAX; i++)
		if (someArray [i] < smallest)
			smallest = someArray[i];
	return smallest;


} // end minimum()

int ArrayStuff::maximum() // determines the largest number in the array
{
	int largest = someArray[0];
	for ( int i = 1; i < MAX; i++)
		if (someArray [i] > largest)
			largest = someArray[i];
	return largest;


}// end maximum()

int ArrayStuff::sum()	// int ArrayStuff::sum()
{
int total = someArray[0];
for (int i = 1; i< MAX; i++)
	total += someArray[i];
return total;
}
// end sum()

void ArrayStuff::swap (int, int)	// void arrayStuff::swap( int x, int y )
{
	int temp = someArray[1];
	someArray[1] = someArray [5];
	someArray[5]  = temp;
}	// end swap()

int ArrayStuff::minPosition (int)	// int arraystuff::minPosition( int start )
{
	int min = 0;
		for (int i = 0 + 1; i < MAX; i++)
			if ( someArray [i] < someArray [min] )
				min = i;

	return min;
}
// end minPosition

void ArrayStuff::sort()	// void arrayStuff::sort()
{	
	int j;
	for ( int i = 0; i < MAX - 1; i++)
	{
		j = minPosition (i);

	swap (i, j);
	}
}
//end sort()

void main()
{
	seedRandom();			// call the seedRandom function
	ArrayStuff testArray;	// create an instance of arrayStuff
	testArray.printArray();	// call the printArray function
	testArray.printReverseArray(); // call the printReverseArray
	cout << "The smallest element: " << testArray.minimum() << endl;
	cout << "The largest element: " << testArray.maximum() << endl;
	cout << "The sum of the elements: " << testArray.sum() << endl;
	testArray.printArray();
	testArray.swap(1, 5);
	testArray.printArray();
	int foo = testArray.minPosition(0);
	cout << "The index of the smallest value in the array: " << foo << endl;
	testArray.sort();
	testArray.printArray();

} // end main
thanks Paul, i came across that page earlier too, but I'm still having trouble relating to what I have for my sort function, which is :
1
2
3
4
5
6
7
8
9
10
11
void ArrayStuff::sort()	// void arrayStuff::sort()
{	
	int j;
	for ( int i = 0; i < MAX - 1; i++)
	{
		j = minPosition (i);

	swap (i, j);
	}
}
//end sort() 


Would you say i'm missing something in there?
Here's a simple selection sort. you could pass j and iMin into swap if you wanted (j and iMin are the locations of the array that needed to be swapped).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void ArrayStuff::sort()
{
	int i,j, temp;
	int iMin;
 
	for (j = 0; j < MAX; j++) {

    iMin = j;
    
    for ( i = j+1; i < MAX; i++) {
         
        if (someArray[i] < someArray[iMin]) {
            iMin = i;
        }
    }
 
    if ( iMin != j ) {
        temp = someArray[j];
		someArray[j] = someArray[iMin];
		someArray[iMin] = temp;
		}
	}
}
You aren't using variables in swap() or minPosistion(). Look at the comments.

Do you know what I'm saying? minPosistion() isn't working right, and even if it was, all swap would do is switch position 1 with position 5.
Last edited on
thanks zrrz, the simple selection sort makes sense. I read about that in the text, and thought that's what I should bee using, but wasn't sure. I have this intro course I'm taking online, and sometimes I'm not sure what they instructor is asking, or expecting from us. He likes to provide some beginning code to get you started, but he sometimes even messes with that. It's all quite confusing sometimes.

lowestOne are you saying I haven't coded the minPosition() right?
As far as the swap() function, I think the instructor just wants to show us how to swap different element values.
You did a fairly good job and my example is not meant to show you up, but rather give an example of how cleaner code is easier to read and a simpler way to handle class code.

I've left out the sort algo as I'm not too sure where you were going with it, but Google bubble sort and see if you can come up with a method that works.

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
#include	<iostream> 		// used for input output
#include	<stdlib.h> 		// used for the random number generator
#include	<ctime> 		// used for seeding the random number generator
#include	<cmath>

using namespace std;

	const int MAX = 20;
	
class	Array {
    private:
	// By default class elements are private, but this just makes it clearer
	int Element [MAX], MinValue, MaxValue;
	int MinPos, MaxPos;
	int Sum;
		
    public:
    	Array ();
    	void ShowArr (bool = false);

    	int      Max () { return MaxValue; };
    	int      Min () { return MinValue; };
    	int MinIndex () { return MinPos; };
    	int MaxIndex () { return MaxPos; };
    	int    Total () { return Sum; };
	};

// Default constructor	
Array::Array () {
	int Seed = time (NULL), RNumb;
	MinValue = MAX; MaxValue = Sum = 0;
	
	srand (Seed);
	
	// RNumb because I know what object code looks like when doing calcs from an array
	for (int Pntr = 0; Pntr < MAX; ) {
		RNumb = rand () % 99 + 1;
		Sum += RNumb;		
		Element [Pntr++] = RNumb;
		
		if ( MaxValue < RNumb) {
			MaxValue = RNumb;
			MaxPos = Pntr;
			}
			
		if ( MinValue > RNumb ) {
			MinValue = RNumb;
			MinPos = Pntr;
			}
		}
	}

// Display array elements in either ascedning or decending order
void Array::ShowArr ( bool Reverse ) {
	int Skew, Pntr, Index;
	
	Reverse == true ? Skew = MAX - 1 : Skew = 0;
	
	for ( Pntr = 0; Pntr < MAX; Pntr++ ) {
		Index = fabs (Pntr - Skew);
		cout << "[" << Index + 1 << "]  " << Element [Index] << "\n";
		}
	}
	
int main ( ) {	
	Array MyNumbs;
	
	MyNumbs.ShowArr ( false );
	cout << "\nMin Value = " << MyNumbs.Min () << " @  " << MyNumbs.MinIndex () << "\n";
	cout << "Max Value = " << MyNumbs.Max () << " @  " << MyNumbs.MaxIndex () << "\n\n";
	MyNumbs.ShowArr ( true );
	cout << "\nTotal = " << MyNumbs.Total () << "\n\n";	
	
	return 0;
	}
Thanks coder, I've only been doing this for about 7 weeks now, so I'm still pretty unsure how spacing works. I agree that yours is much easier to read, as you can see better where each function starts and it's code is indented out. I've read a little about bubble sorts, but it's all rather confusing still for me.
Thanks again
Bubble sorts are really basic and time consuming. For small stuff like this, not bad, but large arrays not practical at all

1
2
3
4
5
6
7
8
9
  for(int x = 0;  x < MAX;  x++) {
    for(int y = 0; y < MAX - 1; y++) {
      if( array[y] > array[y + 1]) {
	int temp = array[y + 1];
	array[y + 1] = array[y];
	array[y] = temp;
	}
      }
    }


You've probably already spotted how most of the inside loop is not unlike ArrayStuff::swap (int, int).

In the days of DOS, when one could write directly to video memory I would write a random sequence of colors to screen and then use this method to sort the colors and you could actually visualize the bubble sort of affect.

Topic archived. No new replies allowed.