Recursive function for sorting array
Nov 16, 2012 at 3:28am UTC
Hi all, i have this mini program that generates randomly 15 to 25 sized array.
This array then randomly generate lower and upper case letters.
So far i have gotten iterative swap to move all lower case to left side and all upper case to right side. Im' required to do it recursively too. but am having a hard time figuring out the base case and general case. here is the code i have for the parts i got working.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAXSIZE = 25;
void ConstructArray (char [], int );
void printArray (const char [], int );
void iterativeSwap (char [], int );
int main ()
{
char myArray [MAXSIZE];
srand (time(NULL));
int randomSize = rand() % 11 + 15;
ConstructArray (myArray, randomSize);
\\printArray (myArray, randomSize);
\\iterativeSwap (myArray, randomSize);
}
void ConstructArray (char myArray [], int randSize)
{
char randUppcase, randLowcase;
int randAlpha;
for (int i = 0; i < randSize; i++)
{
randAlpha = rand() % 2 + 1;
randUppcase = rand() % 26 + 65;
randLowcase = rand() % 26 + 97;
if (randAlpha == 1)
randAlpha = randUppcase;
else
randAlpha = randLowcase;
myArray [i] = randAlpha;
}
printArray (myArray, randSize);
iterativeSwap (myArray, randSize);
}
void printArray (const char myArray [], int randSize)
{
cout << "Given the following array " << endl;
for (int i = 0; i < randSize; i++)
cout << myArray[i] << " " ;
cout << endl << endl;
}
void iterativeSwap (char myArray [], int randSize)
{
int i = 0;
int j = randSize - 1;
char temp;
while (i != j)
{
if (myArray[i] >= 'A' && myArray[i] <= 'Z' )
{
if (myArray[j] >= 'a' && myArray[j] <= 'z' )
{
temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
else
--j;
}
else
++i;
}
cout << "Iterative Swap" << endl;
for (int i = 0; i < randSize; i++)
cout << myArray[i] << " " ;
}
Last edited on Nov 16, 2012 at 4:01am UTC
Topic archived. No new replies allowed.