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?
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
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.
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.