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 139 140 141 142 143 144 145 146 147 148 149
|
#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);
//recursive swap for vowels
void vowelSwapR(char [], int, int, int);
int main()
{
//initialise array
char charList[MAX], charListR[MAX];
int arraySize, arraySizeR;
static int n = 0;
static int m;
//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 call to generate array for recursive swap
arraySizeR = constructArray(charListR);
//function call to print recursive array
cout << "Given the following array" << endl;
printArray(charListR, arraySizeR);
m = arraySizeR - 1;
vowelSwapR(charListR, arraySizeR, n, m);
cout << "Recursive swap of array" << endl;
printArray(charListR, arraySizeR);
}
//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 - 1; j > i; j--)
{
if (vowelCheck(charList[j]))
{
swap (charList[i], charList[j]);
}
}
}
}
}
//Iterative function
void vowelSwapI(char charList[], int arraySize)
{
cout << "Iterative swap of array" << endl;
swapElements(charList, arraySize);
printArray(charList, arraySize);
}
//recursive function
void vowelSwapR(char charListR[], int arraySizeR, int n, int m)
{
//base case
if (n == arraySizeR && m == 0)
return;
//special case
else
{
if ((!vowelCheck(charListR[n])) && vowelCheck(charListR[m]))
{
swap(charListR[n], charListR[m]);
vowelSwapR(charListR, arraySizeR, n++, m--);
}
}
}
|