Urgent Help Need [Array]

I am trying to print a randomised array which is already working
My next step is to swap elements with statements in my while loop.

My only problem is that I need to print out the first-hand randomised array then print the array that went through the while loop. Means that I need 2 printed out. One before and after, can anyone help me out?

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const int MAX = 20;

void constructArray(int[], int);

void printArray(const int[], int);

void swapArray(int[], int, int&, int&);


int main()
{
	int a[MAX];
	int size;

	srand(time(NULL));


	for (int i = 1; i <= 1; i++)
	{
		size = rand() % 11 + 10;

		int left = a[0];
		int right = a[size - 1];

		cout << "Given the following array\n";
		cout << "  ";
		constructArray(a, size);
		printArray(a, size);
		cout << endl;
		

		cout << "Iterative swap of array\n";
		cout << "  ";

		while (left < right)
		{ 
			if (left < 25 && right <= 25)
				swapArray;
		left++, right--;

		if (left > 25)
			right--;
		
		if (right <= 25)
			left++;

		else
			left++, right--;
		}
	
	printArray(a, size);	
	}

}

void constructArray(int a[], int size)
{
	for (int i = 0; i < size; i++)
		a[i] = rand() % 51;
}

void printArray(const int a[], int size)
{
	for (int i = 1; i < size; i++)
		cout << a[i] << "  ";
	cout << endl;
}

void swapArray(int a[], int size, int& left, int& right)
{
	int temp = a[left];
	a[left] = a[right];
	a[right] = temp;

}
@line 27..28 you are taking values form the array, i think you mean to use the indices.
1
2
int left = 0;
int right = size-1;


@line 43; swapArray(a,size,left,right);

@line 44; stop using comma like this, its bad form.

@line 69; that should be for (int i = 0

as for the question of printing the array out before and after, you already do this @33 and @56

your problems lie between lines 46 and 53. what are you trying to achieve here?

you also use 25 quite a lot! did you mean size? there are only 20 items in the array, accessing index >=20 will crash.

if you are trying to reverse the array try this.
1
2
3
4
5
6
while (left < right) 
{ 
      swapArray(a,size,left,right);
      left++;
      right--;
}

Last edited on
Hey Jay, it's you again! Thanks for replying and helping out.

@line27-28, ah I am trying to start from the left of the array, which means
a{1,2,3,4,5}, I am using the a{1}.. Thus a[0]; Am I right this way?

For right, I am using the right most array, so I did a[size-1]; Am I right on this? Ahh I am confused actually.


@line43 Changed, thanks!

@line69 changed again!

I am actually trying to compare the numbers in the array with 25, means {1, 2, 3, 4, 5}.. So if 25 is larger than 1, I swap etc. The numbers in the array will be larger, just giving an example.


Thanks for the reverse array guide, I can try. But still help me out please :P
always glad to help a tryer :)

at 27..28 you are actually setting left,right to be the VALUES from the array. you want the indices, so replace with my suggestion.

still not sure what you mean, what would you expect the following data to end up like?
{12,50, 4, 8,19, 9,30,45}

Hey, sorry I went to sleep yesterday. Haha

I changed accordingly to your suggestions!

int left = 0;
int right = size-1;

Hmm, I'll try explain abit more, so sorry.

Base on the example {12, 50, 4, 8, 19, 9, 30, 45}

Left = {12, 50, 4, 8} = So Left starts from the left side of the array, 0;
Right = {19, 9, 30, 45} = So right started from the right side of the array, size - 1;

Am I right on these 2?

Ok, now what I am trying to achieve is..

{12, 50, 4, 8, 19, 9, 30, 45}

My while statement is...

If (Left > 25 && Right <= 25)
swapArray
Left--, Right++

It compare 12 on the left and 45 on the right. So for this part, no it doesn't fit the swap criteria. It will now move to my next if-else statements...

Else (Left++, Right--)
Now the array on the left is pointing to 50 while Right array is pointing to 30.

Etc..

Final Outcome!
Small numbers on the left side, bigger numbers on the right side while comparing.
Last edited on

ok, i see some of your confusion came from left,right. these are the indices into the array. left runs 0..n and right runs n..0. you then took the values from the array and assigned them to left,right, thus confusing the issue.

so, to be clear, your code doesnt need to actually take the values from the array, just reference them by their indices. Which brings it down to the following...

1
2
3
4
5
6
7
8
while (left < right)
{ 
      if (a[left] > 25 && a[right] <= 25)
         swapArray(a,size,left,right);

      left++;
      right--;
}


if you removed the if(), this loop would spin until the indices met or passed each other in the middle.

Adding the if() just says that while we are spinning through, any items that meet our criteria will get swapped.
Topic archived. No new replies allowed.