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);

}

Your algorithm is a little goofy. I think a more intuitive algorithm would match the way one might do it manually:

  1. Find the first non-vowel and mark it y, this is the end of the vowel half
  2. Find the next vowel and mark it x
  3. Swap the characters at y and x
  4. Increment y
  5. Repeat at 2 until x reaches the end of the list

This is easily translated to C++, and only requires a way to determine whether the specified character is a vowel:

void vowel_partition(char a[], int n)
{
    int x = 0;
    
    // Find the first non-vowel
    while (x < n && is_vowel(a[x]))
        ++x;
    
    // Swap in any remaining vowels
    for (int y = x; x < n; x++) {
        if (is_vowel(a[x])) {
            std::swap(a[x], a[y]);
            ++y;
        }
    }
}

Hi, thanks for your help so far. I have modified my code to this.

#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[], int);

//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 ch)
{
        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 charList[], int arraySize)
{

    //start from left to right and find the first non vowel
    for (int i = 0; i < arraySize; i++)
    {
        if (!vowelCheck(charList[i]))
        {

        //start from right to left to find the first vowel
        for (int j = arraySize; j > 0; j--)
        {
            if (vowelCheck(charList[j]))
            {
                swap (charList[i], charList[j]);
            }
        }


        }
    }



}

void vowelSwapI(char charList[], int arraySize)
{
    cout << "Iterative swap of array" << endl;
    swapElements(charList, arraySize);
    printArray(charList, arraySize);
}

But it does not totally work yet. The last vowel found in the array gets sent to the last element of the array, instead of being sent to the left like the other vowels.

But it does not totally work yet.

Your inner loop should start at arraySize - 1 to avoid an out of bounds array access, and end at i rather than 0. Currently by going all of the way to 0, you're undoing work done previously and messing up the result.

Your inner loop should start at arraySize - 1 to avoid an out of bounds array access, and end at i rather than 0. Currently by going all of the way to 0, you're undoing work done previously and messing up the result.

ahh I see my mistake. Thank you :)

hello once more. I've been trying to turn the iterative function above into a recursive function, but it's not working

//recursive function
void vowelSwapR(char charListR[], int arraySizeR, int &n, int &m)
{
    n = n + 1;
    m = m - 1;
    //base case
    if (n >= m)
     return;
    //special case
    else if (vowelCheck(charListR[n]) == false)
            {
                if (vowelCheck(charListR[m]) == true)
                {
                   cout << " in else if";
                   swap(charListR[n], charListR[m]);
                   n++;
                   m--;
                   vowelSwapR(charListR, arraySizeR, n, m);
                }
            }
    else
        {
            cout << " in else";
            n++;
            m--;
            vowelSwapR(charListR, arraySizeR, n, m);
        }

}

Consider four cases given an array, a start index, and an end index:

  1. The base case where the start index passes the end index: terminate recursion.
  2. There's a vowel at the start index: recurse and increment the start index.
  3. There's not a vowel at the end index: recurse and decrement the end index.
  4. There's a vowel at the end index: swap the characters at the two indices and recurse while both incrementing the start index and decrementing the end index.
This question has already been answered. Start a new discussion instead.