"I am trying to display an array of digits using rand.
My final result need to be:
1) Output all digits <=40 to the left side of array
2) Output digits > 40 to the right side of array
Prob: The random digits does seems to change & result incorrect
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAX = 10;
typedef void* VoidPtr;
void constructArrayVP (VoidPtr [], int);
void printArrayVP (VoidPtr [], int);
void swapArray (VoidPtr [], int);
void swap2Elements (VoidPtr&, VoidPtr&);
int main ()
{
VoidPtr vp [MAX];
constructArrayVP (vp, MAX);
printArrayVP (vp, MAX);
swapArray (vp, MAX);
printArrayVP (vp, MAX);
}
void constructArrayVP (VoidPtr vp [], int size)
{
int *item;
for (int i = 0; i < size; i++)
{
item = new int;
*item = rand () % 100;
vp [i] = item;
}
}
void printArrayVP (VoidPtr vp [], int size)
{
int item;
for (int i = 0; i < size; i++)
{
item = *(static_cast <int *> (vp [i]));
cout << item << "\t";
}
cout << endl;
}
//Does not work
void swapArray (VoidPtr vp [], int size)
{
int left = 0;
int right = size - 1;
do
{
if (left > 40)
{
if (right <= 40)
swap2Elements (vp [left], vp [right]);
else
right--;
}
else
left++;
}while (left > right);
}
void swap2Elements (VoidPtr& vp1, VoidPtr& vp2)
{
VoidPtr temp = vp1;
vp1 = vp2;
vp2 = temp;
}