C
cheetahclaws
I am trying to create a function that reverses the halves of an
integer array. For example. if the array contains 1,2,3,4 --- after it
has gone to the function I constructed, the array will look like
3,4,1,2. In the case the array contains an odd amount of elements e.g.
1,2,3,4,5, it will look like 4,5,3,1,2.
I have created such an array as followed:
==================================
for(i=0;i<=((size/2)-1);i++)
{
if(size%2 != 0)
{
t = a;
a = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
}
else
{
t = a;
a = a[(size/2)+i];
a[(size/2)+i] = t;
}
}
==================================
NOW, the problem is I want to create a function that does the same
thing, but I want to use a Nested For loop to do it as such without
using IF statements to handle what to do when size is odd or not.
I came up with the following code:
==================================
void reverseHalves(int a[], int size)
{
int i, t, j=0;
for(i=0;i<=((size/2)-1);i++)
{
t = a;
a = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
for(j=(size-(size/2));j<=(size/2)-1;j++)
{
t = a[j];
a[j] = a[(size/2)+j-1];
a[(size/2)+j-1] = t;
}
}
==================================
The problem is the second nested-for-loop version I have come up with
doesn't work. It only works for odd numbers. When I enter an even
number, I always end up getting a -24214214 or something like that for
one of the numbers.
It alllllmost works. I have been at this for about three hours and
almost at the end of my rope. Can someone PLEASE FOR THE LOVE OF GOD
GIVE ME SOME HELP BEFORE I BLOW MY BRAINS OUT?!?!!
-Thanks for listen.
integer array. For example. if the array contains 1,2,3,4 --- after it
has gone to the function I constructed, the array will look like
3,4,1,2. In the case the array contains an odd amount of elements e.g.
1,2,3,4,5, it will look like 4,5,3,1,2.
I have created such an array as followed:
==================================
for(i=0;i<=((size/2)-1);i++)
{
if(size%2 != 0)
{
t = a;
a = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
}
else
{
t = a;
a = a[(size/2)+i];
a[(size/2)+i] = t;
}
}
==================================
NOW, the problem is I want to create a function that does the same
thing, but I want to use a Nested For loop to do it as such without
using IF statements to handle what to do when size is odd or not.
I came up with the following code:
==================================
void reverseHalves(int a[], int size)
{
int i, t, j=0;
for(i=0;i<=((size/2)-1);i++)
{
t = a;
a = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
for(j=(size-(size/2));j<=(size/2)-1;j++)
{
t = a[j];
a[j] = a[(size/2)+j-1];
a[(size/2)+j-1] = t;
}
}
==================================
The problem is the second nested-for-loop version I have come up with
doesn't work. It only works for odd numbers. When I enter an even
number, I always end up getting a -24214214 or something like that for
one of the numbers.
It alllllmost works. I have been at this for about three hours and
almost at the end of my rope. Can someone PLEASE FOR THE LOVE OF GOD
GIVE ME SOME HELP BEFORE I BLOW MY BRAINS OUT?!?!!
-Thanks for listen.