C++ Array of Characters rearrangement issue

All help will be appreciated. I need to find out why the code is not working as intended for purpose of revision.

Challenge:

I need to generate 2 sets of array. They are to be in capital letters characters and between 20-30 elements. I am supposed to find all non vowels starting from LEFT to right and vowels from RIGHT to left. I then rearrange the characters until all vowels are at the left and non vowels at the right.

It is broken down to a function that rearrange the first vowel encountered with the first non vowel encountered.

the first set are to be done with iterative, and 2nd set with recursive.

The problem :

Both iterative and recursive function are working fine. The issue lies with my function that finds the first vowel encountered with the first non vowel encountered and then swap them. Some issue with the code here is causing the end product(where all vowels is at left and non vowels at right side) NOT to be arranged properly SOMETIME. I.e sometime the arrangement is perfect while sometime it is not(like E A O I U B U K S). There's always one vowel after a non vowel.

Code

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

void constructArray(char[],int&);
bool checkVowel(char);
void printArray(const char[], int);
void swapElements(char[], int);
void swapAllElements(char[],int);
void recursiveSwap(char[],int);

const int MAX = 30;

int main()
{
	char a[MAX];
	char b[MAX];
	int sizeOfArray = 0; 
	bool isVowel;
	srand(time(NULL));
	
	constructArray(a,sizeOfArray);
	cout << "Given the following array" << endl;
	printArray(a,sizeOfArray);
	cout << "\n" << endl;
	cout << "Iterative swap of array" << endl;
	swapAllElements(a,sizeOfArray);
	printArray(a,sizeOfArray);
	
	constructArray(b,sizeOfArray);
	cout << "\n" << endl;
	cout << "Given the following array" << endl;
	printArray(b,sizeOfArray);
	cout << endl;
	cout << "\nRecursive Swap of array" << endl;
	recursiveSwap(b,sizeOfArray);
	printArray(b,sizeOfArray);
	
}

void constructArray(char a[],int& num)
{
	num = rand() % 11 + 20;
	
	for (int i=0;i<num;i++)
		a[i] = static_cast<char>(rand() % 26 + 65);
		
}

bool checkVowel(char a)
{
	if (a=='A' || a=='E' || a=='I' || a=='O' || a=='U')
		return true;
	else
		return false;
}

void printArray(const char a[], int max)
{	 
	for(int i=0;i<max;i++)
		cout << a[i] << " ";
}

void swapElements(char a[], int max)
{
	int posVowel, posNonVowel;
	int temp = 0;
	posVowel=0;
	posNonVowel=0;
	bool found = false;
	bool found2 = false;
				
	for(int i=max;i>=0;i--)					//Vowel
	{	
		if(!found)
		{
			found = checkVowel(a[i]);
			++posVowel;
		}
		else;
	}
	
	for(int i=0;i<max;i++)					//Non Vowel
	{	
		if(!found2)
		{
			found2 = !(checkVowel(a[i]));
			++posNonVowel;
		}
		else;
	}
	
	posVowel -= 1; // because of null char 
	posNonVowel -= 1; //because array start from 0
	
	temp = a[max-posVowel];
	a[max-posVowel] = a[posNonVowel];
	a[posNonVowel] = temp;
}

void swapAllElements(char a[],int max)
{
	for(int i=0;i<max;i++)
		swapElements(a,max);
}

void recursiveSwap(char b[],int max)
{
	if(max==0)             //I tried to use while(max>0) here, but the 
		return;           //program hanged while calling this function,
  	else                       // anyone care to explain why?
	{
		swapElements(b,max);
		recursiveSwap(b,max-1);
	}
}	


-------------

Last edited on
Topic archived. No new replies allowed.