472,958 Members | 2,570 Online

# I need help with a function that swaps array halves.

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[i];
a[i] = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
}
else
{
t = a[i];
a[i] = 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[i];
a[i] = 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.

Sep 5 '07 #1
8 2301 <ch**********@yahoo.comwrote in message
>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[i];
a[i] = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
}
else
{
t = a[i];
a[i] = 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[i];
a[i] = 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?!?!!
This looks like homework, but you have shown a lot of effort. Look at this
code, although you'll have to fix one bug when the arrays are odd sized:

#include <iostream>

void reverseHalves(int a[], int size)
{
int half = size / 2;
bool odd = size % 2 == 1;

int j = half;
if ( odd )
j++;

for ( int i = 0; i < half; ++i )
{
std::swap( a[i], a[j++] );
}
}
int main()
{
int Array[] = { 1, 2, 3, 4, 5, 6 };
reverseHalves( Array, sizeof( Array ) / sizeof( Array ) );
for ( int i = 0; i < sizeof( Array ) / sizeof( Array ); ++i )
std::cout << Array[i] << " ";
std::cout << "\n";

int Array2[] = { 1, 2, 3, 4, 5, 6, 7 };
reverseHalves( Array2, sizeof( Array2 ) / sizeof( Array2 ) );
for ( int i = 0; i < sizeof( Array2 ) / sizeof( Array2 ); ++i )
std::cout << Array2[i] << " ";
std::cout << "\n";

}

Output:
4 5 6 1 2 3
5 6 7 4 1 2 3

The output is close to what you want except for odd, where the question
becomes... do you want
4567123
or
5671234

Fix the code to express what you want.
Sep 6 '07 #2
On Sep 5, 8:19 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
<cheetahcl...@yahoo.comwrote in message

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[i];
a[i] = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
}
else
{
t = a[i];
a[i] = 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[i];
a[i] = 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?!?!!

This looks like homework, but you have shown a lot of effort. Look at this
code, although you'll have to fix one bug when the arrays are odd sized:

#include <iostream>

void reverseHalves(int a[], int size)
{
int half = size / 2;
bool odd = size % 2 == 1;

int j = half;
if ( odd )
j++;

for ( int i = 0; i < half; ++i )
{
std::swap( a[i], a[j++] );
}

}

int main()
{
int Array[] = { 1, 2, 3, 4, 5, 6 };
reverseHalves( Array, sizeof( Array ) / sizeof( Array ) );
for ( int i = 0; i < sizeof( Array ) / sizeof( Array ); ++i )
std::cout << Array[i] << " ";
std::cout << "\n";

int Array2[] = { 1, 2, 3, 4, 5, 6, 7 };
reverseHalves( Array2, sizeof( Array2 ) / sizeof( Array2 ) );
for ( int i = 0; i < sizeof( Array2 ) / sizeof( Array2 ); ++i )
std::cout << Array2[i] << " ";
std::cout << "\n";

}

Output:
4 5 6 1 2 3
5 6 7 4 1 2 3

The output is close to what you want except for odd, where the question
becomes... do you want
4567123
or
5671234

Fix the code to express what you want.

Thanks for the help. I'm starting to put together a picture of
something here. I was just wondering, how does the swap function work?
If I wanted to exclude it from the For-Loop and use something like:

t=a[i];
a[i] = a[j];
a[j] = t;
j++;

where t is a temporary variable, would that be the same?

Sep 6 '07 #3
ch**********@yahoo.com says...
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.
Already posted on c.l.c++.m, but since you seem a bit stressed and this
will get to you a bit sooner:

#include <algorithm>

void swap_halves(int *a, size_t size) {

int pivot = size/2;

std::swap_ranges(a, a+pivot, a+pivot+size%2);
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 6 '07 #4
On 9 6 , 7 50 , cheetahcl...@yahoo.com wrote:
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[i];
a[i] = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
}
else
{
t = a[i];
a[i] = 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[i];
a[i] = 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.
I have a silly idea, what about to use a stack to make it? I think you
understand what I mean.

Sep 6 '07 #5
<ch**********@yahoo.comwrote in message
On Sep 5, 8:19 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
><cheetahcl...@yahoo.comwrote in message

>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[i];
a[i] = a[(size/2)+i+1];
a[(size/2)+i+1] = t;
}
else
{
t = a[i];
a[i] = 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[i];
a[i] = 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?!?!!

This looks like homework, but you have shown a lot of effort. Look at
this
code, although you'll have to fix one bug when the arrays are odd sized:

#include <iostream>

void reverseHalves(int a[], int size)
{
int half = size / 2;
bool odd = size % 2 == 1;

int j = half;
if ( odd )
j++;

for ( int i = 0; i < half; ++i )
{
std::swap( a[i], a[j++] );
}

}

int main()
{
int Array[] = { 1, 2, 3, 4, 5, 6 };
reverseHalves( Array, sizeof( Array ) / sizeof( Array ) );
for ( int i = 0; i < sizeof( Array ) / sizeof( Array ); ++i )
std::cout << Array[i] << " ";
std::cout << "\n";

int Array2[] = { 1, 2, 3, 4, 5, 6, 7 };
reverseHalves( Array2, sizeof( Array2 ) / sizeof( Array2 ) );
for ( int i = 0; i < sizeof( Array2 ) / sizeof( Array2 ); ++i )
std::cout << Array2[i] << " ";
std::cout << "\n";

}

Output:
4 5 6 1 2 3
5 6 7 4 1 2 3

The output is close to what you want except for odd, where the question
becomes... do you want
4567123
or
5671234

Fix the code to express what you want.

Thanks for the help. I'm starting to put together a picture of
something here. I was just wondering, how does the swap function work?
If I wanted to exclude it from the For-Loop and use something like:

t=a[i];
a[i] = a[j];
a[j] = t;
j++;

where t is a temporary variable, would that be the same?
Yes. Swapping two variables is a very common programming in task, where you
save one variable to a temp, assign the other to it, then save the temp to
the second. It is so common, in fact, that it's in the stl. Realistically,
it's probably supposed to be std::swap and I'm not even sure what header
it's supposed to be in. Whichever header, apparently <iostreamincludes
it.
Sep 6 '07 #6
On Sep 6, 3:03 am, Jerry Coffin <jcof...@taeus.comwrote:
cheetahcl...@yahoo.com says...
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.
Already posted on c.l.c++.m, but since you seem a bit stressed and this
will get to you a bit sooner:
#include <algorithm>
void swap_halves(int *a, size_t size) {

int pivot = size/2;

std::swap_ranges(a, a+pivot, a+pivot+size%2);
}
Interesting. Yet another standard function which I didn't know
about. Using std::vector (of course), something like:

std::swap_ranges( v.begin(),
v.begin() + v.size() / 2,
v.end - v.size() / 2 ) ;

should do the trick.

My first reaction, not knowing of this function, was to ask why
he didn't use the classical solution:

std::reverse( v.begin(), v.end() ) ;
std::reverse( v.begin(), v.begin() + v.size() / 2 ) ;
std::reverse( v.end() - v.size() / 2, v.end() ) ;

And of course, std::rotate can also be used, although it would
require an extra test:

std::rotate( v.begin(), v.begin() + v.size() / 2, v.end() ) ;
if ( v.size() % 2 != 0 ) {
std::rotate( v.begin(), v.begin() + 1, v.end() - v.size() /
2 ) ;
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 6 '07 #7
ja*********@gmail.com says...
On Sep 6, 3:03 am, Jerry Coffin <jcof...@taeus.comwrote:
[ ... ]
std::swap_ranges(a, a+pivot, a+pivot+size%2);
}

Interesting. Yet another standard function which I didn't know
Quite honestly, I didn't either until this came up. What I orginally
thought of (and was looking for) was whether there was a version of
remove that took a pair of iterators to specify what was to be removed.
It doesn't exist, but opening the reference for algorithm, I noticed
swap_ranges, which sounded quite promising...
Using std::vector (of course), something like:

std::swap_ranges( v.begin(),
v.begin() + v.size() / 2,
v.end - v.size() / 2 ) ;
You meant 'v.end()' of course.
should do the trick.
Yup. Roughly the same idea would work here, using a+size-size/2 instead
of a+size+size%2 [or, equivalently, a+size+(size&1)].
My first reaction, not knowing of this function, was to ask why
he didn't use the classical solution:
[ ... reversals ]
And of course, std::rotate can also be used, although it would
require an extra test:
Right -- but at least in this case, the extra test is only executed
once, instead of inside a loop.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 6 '07 #8

Jim Langston wrote in message...
>
Yes. Swapping two variables is a very common programming in task, where
you
save one variable to a temp, assign the other to it, then save the temp to
the second. It is so common, in fact, that it's in the stl.
Realistically,
it's probably supposed to be std::swap and I'm not even sure what header
it's supposed to be in. Whichever header, apparently <iostreamincludes
it.
#include <algorithm>

--
Bob R
POVrookie
Sep 7 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 20 by: Terry | last post by: I'm trying to have two divs side-by-side, each taking up 50% of the body. Sounds simple enough, right? http://novodom.net/div.html The code validates as XHTML 1.1 Strict. Looks fine in Firefox,... 2 by: edself | last post by: Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains... 3 by: ritchie | last post by: Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I... 48 by: Chad Z. Hower aka Kudzu | last post by: A few of you may recognize me from the recent posts I have made about Indy Those of you coming to .net from the Delphi world know truly how unique and... 26 by: _R | last post by: Given that VS2005 has made an effort to clean up the syntax of VC++ (in C++/CLI), is there any future plans to do away with function protos, ala C#/VB? What are they needed for these days? 6 by: daveyand | last post by: Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values 4 by: jeromytheoutlaw | last post by: /* Write a program that reads names and gpas from a text file. The file looks like: James 3.9 Margaret 3.5 Charles 1.2 Jennifer 4.0 Your program sorts the students ascending by gpa,... 20 by: sirsnorklingtayo | last post by: hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()... 11 by: Stevo | last post by: I have a problem with Firefox 2 where if I resize the height of a DIV (using JavaScript), but the rest of the page below isn't being updated to reflect the new positions that affected items should... 2 by: DJRhino | last post by: Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 0 by: Aliciasmith | last post by: In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app... 2 by: giovanniandrean | last post by: The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions... 3 by: NeoPa | last post by: Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on... 0 by: NeoPa | last post by: Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it... 0 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike... 2 by: GKJR | last post by: Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...