I have created a program to generate a set of characters in an array. The objective is to move all vowels generated by the array to the left, for example:
given the following array:
A B C D E F G H I J K L M O P Q R S T U V W X Y Z
to this
A E I O U B C D F G H J K L M P Q R S T V W X Y Z
However, my code doesn't perform the desired swap. What is wrong with my code?
here is my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//global constant of max size of array
const int MAX = 30;
//function prototype to construct array
int constructArray(char []);
//function prototype to check vowel
bool vowelCheck(char [], int);
//function prototype to print elements
void printArray(char [], int);
//function prototype to swap elements
void swapElements(char&, char&);
//iterative swap for vowels
void vowelSwapI(char [], int);
int main()
{
//initialise array
char charList[MAX];
int arraySize;
//function call to generate array and return size
arraySize = constructArray(charList);
//function call to print array
cout << "Given the following array" << endl;
printArray(charList, arraySize);
//function call for iterative swap of vowels
vowelSwapI(charList, arraySize);
}
//function to construct array
int constructArray(char charList[])
{
srand(time(NULL));
int arraySize = rand() % 11 + 20;
for (int i = 0; i < arraySize; i++)
charList [i] = rand () % 26 + 65;
return arraySize;
}
//function to check if character is vowel
bool vowelCheck(char charList[], int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
char ch;
ch = charList [i];
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': return true;
break;
default: return false;
}
}
}
//function to print array
void printArray(char charList[], int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
cout << charList[i] <<" ";
}
cout << endl;
}
void swapElements(char& ch1, char& ch2)
{
char temp;
temp = ch1;
ch1 = ch2;
ch2 = temp;
}
void vowelSwapI(char charList[], int arraySize)
{
for (int i = arraySize; i > 0 ; i--)
{
if (vowelCheck(charList, arraySize))
{
swapElements(charList [i], charList [i-1]);
}
}
cout << "Iterative swap of array" << endl;
printArray(charList, arraySize);
}