Sorting the array

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.
In my design,I should have the following functions:
(a) A function to construct the array
(b) A function to print out the array elements
(c) A function to swap two elements
(d) An iterative function (using ONE loop) to move the array elements so that all the lowercase letters are on the left hand side of the array and all the uppercase letters are on the right hand side of the array.
(e) Convert (d) to recursive function.
(f) Some other useful functions.
So far i have gotten iterative swap to move all lower case to left side and all upper case to right side. I am 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. Can anyone please help with the recursive version?

#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] << " ";
}
closed account (D80DSL3A)
First, some comments about your functions.
The directions say to write a swap function, but you're doing it directly in your function performing the partition, which is oddly named iterativeSwap instead of iterativePartition.
So, you'll need a swap function.

About the base case for recursion:
In your iterative version, the while loop goes until i==j.
This would also be your base case condition in recursivePartition
if(i==j) return;
The general case is just the same thing you're doing in the while loop.
Then, just call recursivePartition( myArray, i, j ) again.

EDIT: In my version I'm bumping both i and j each time, so I can get i > j (they may cross each other). My base case condition has to watch for this, so I have if(i>=j) return;
This would correspond to having while(i<j) in the iterative version.
This also allows me to bump i and j following each swap mySwap( myArray[i++], myArray[j--] ); since may as well get off the elements just swapped. The post increment (not pre increment) is essential there.

EDIT2: Not to get on you about your code in ConstructArray (it works), but it does afford an opportunity to suggest reducing the steps involved.
Your code:
1
2
3
4
5
6
7
8
9
10
randAlpha = rand() % 2 + 1;
randUppcase = rand() % 26 + 65;
randLowcase = rand() % 26 + 97;

if (randAlpha == 1)
randAlpha = randUppcase;
else 
randAlpha = randLowcase;

myArray [i] = randAlpha;

My code:
myArray[i] = 'A' + rand()%26 + ( rand()%2 )*( 'a' - 'A' );
Last edited on
Topic archived. No new replies allowed.