"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;
}

Your code is asking for trouble. You need to seed the random generator,
put this code inside the main where it will be called only once, :

srand( time(0) );

Your code is asking for trouble. You need to seed the random generator,
put this code inside the main where it will be called only once, :

srand( time(0) );

Tks. My mistake.

"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

I was unaware arrays had sides. What do you mean?

Prob: The random digits does seems to change & result incorrect

Random digits are supposed to change. That's why they are called random.
And what makes the result incorrect? I have no idea what's correct nor incorrect to go by.

What is wrong with the code from line 50 onwards? Logic not right?

I was unaware arrays had sides. What do you mean?


Random digits are supposed to change. That's why they are called random.
And what makes the result incorrect? I have no idea what's correct nor incorrect to go by.

I am refering to the index [0] to [9]:

[0]	[1]	[2]	[3]	[4]	[5]	[6]	[7]	[8]	[9]
56	78	3	72	99	47	50	0	30	56

If digits are lesser than or equal to 40, digits will move towards the left of index [0], [1], [2] ...etc

As of now, my output is not moving at all.

Anyone can advise me? I am not sure where goes wrong.
I had declared "srand(time(0))" in main function.

I am refering to the index [0] to [9]:

[0]	[1]	[2]	[3]	[4]	[5]	[6]	[7]	[8]	[9]
56	78	3	72	99	47	50	0	30	56

If digits are lesser than or equal to 40, digits will move towards the left of index [0], [1], [2] ...etc

As of now, my output is not moving at all.

I still have no idea what you're trying to do. A compete example from input to output is needed, including how to decide what goes where.

I still have no idea what you're trying to do. A compete example from input to output is needed, including how to decide what goes where.

This the example:

Random Nos									
83	78	6	80	84	69	23	71	67	64
Output (Sort from  < = 40  to  > 40)									
23	6	78	80	84	69	83	71	67	64

What is wrong with my swapArray?

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAX = 10;


void constructArray_1 (int*, int);
void printArray (int*, int);
void swap (int*, int*);
void swapArray (int*, int*);

int main ()
{
	
 	int a [MAX];
	int size;
	int *first = &a[0];
	int *last = &a[size - 1];	 
	

	srand (time (NULL));	
	
	constructArray_1 (a, MAX);
	printArray (a, MAX);
	swapArray(&a[0], &a[size - 1]);
	swap (first, last);
	printArray (a, MAX);
	
}

void constructArray_1 (int *a, int size)
{
	for (int i = 0; i < size; i++)
	{
		*a = rand () % 10;
		++a;
	}
}


void printArray (int *a, int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << *a << "\t";
		++a;
	}
	
	cout << endl;
}


void swap (int *first, int *last)
{
int tempvar1;


tempvar1 = *first;
*first = *last;
*last = tempvar1;

}



void swapArray (int *first, int *last)
{

int *a;
int size;

first = &a[0];
last = &a[size - 1];


if (first < last)
{
if (*first <= 50)
first++;
else if (*last >= 50)
last--;
else if ((*first > 50) && (*last <= 50))
{
swap (first, last);
first++;
last--;
}
swapArray(first, last);
}
}

Correction:

Line # 18

int size=MAX;

Line #28 - Remove that line

Line #37

*a = rand () % 100;

Line #71: Remove that line

Line #72

int size=MAX

Line 74: Remove that line
Line 75: Remove that line

commented: Bravo +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.