Array outputting unintended triangle

My last sorting function recSwap is returning a triangle. I'm getting the sorting I want which is in the last line of output from the triangle but I've no luck so far in omitting the rest of the triangle.

My question is how do I omit the triangle and why is there the triangle output in the first place. My for loops seems to be functioning as intended so I'm not sure what's the cause here. Thanks.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;

const int MAX = 25;

void constructArray (char a [], int);
void printArray (const char a [], int);
void itSwap (char a [], int);
void recSwap (char a [], int);

int main ()
{
	char a [MAX];
	srand (time (NULL));
	int size = rand() % 11 + 15;
	
	constructArray (a, size);
	printArray (a, size);
	cout << endl;
	itSwap (a, size);
	cout << endl;
	constructArray (a, size);
	printArray (a, size);
	cout << endl;
	recSwap (a, size);
	
}

void constructArray (char a [], int size)
{
	char randUpper, randLower;
	int randLetter;
	
	for (int i = 0; i < size; i++)
	{
	randLetter = rand () % 2 + 1;
	randUpper = rand () % 26 + 65;
	randLower = rand () % 26 + 97;
	
	if (randLetter == 1)
		randLetter = randUpper;
	else
		randLetter = randLower;
	a [i] = randLetter;
	}
}

void printArray (const char a [], int size)
{
	cout << "Given the following array" << endl;
	for (int i = 0; i < size; i++)
	cout << a [i] << " ";
	cout << endl;
}
	
void itSwap (char a [], int size)
{
	int i = 0;
	int	j = size - 1;
	char temp;
	
	while (i != j)
	{
		if (a [i] >= 'A' && a [i] <= 'Z')
		{
			if (a [j] >= 'a' && a [j] <= 'z')
			{
				temp = a [i];
				a [i] = a [j];
				a [j] = temp;
			}
			else --j;
		}
		else ++i;
	}
	
	cout << "Iterative swap of array" << endl;
	for (int i = 0; i < size; i++)
	cout << a [i] << " ";
	cout << endl;
}

void recSwap (char a [], int size)
{
	int i = 0;
	int j = size - 1;
	char temp;
	
	for (int i = 0; i < size; i++)
	{
		if (a [i] < a [j])
			j = i;
	}
		temp = a [size - 1];
		a [size - 1] = a [j];
		a [j] = temp;
	
	if (size > 1) 
	{
		recSwap (a, --size);
	}
	cout << "Recursive swap of array" << endl;
	for (int i = 0; i < size; i++)
	cout << a [i] << " ";
	cout << endl;
}
Last edited on
Topic archived. No new replies allowed.