# how do I reverse order of my array

• 09-25-2002
jgonzales
how do I reverse order of my array
How do I reverse the order of this array?

Jose

Code:

#include <iostream.h>
#include <stdlib.h>

const int myarraysize = 5;

void Fill_array(double myarray[], int j);
void Show_array(double myarray[], int j);
void Reverse_array(double myarray[], int j);

int main()
{
cout << "starting in main" << endl;

double uservalues[myarraysize] = {};

Fill_array(uservalues, myarraysize);
Show_array(uservalues, myarraysize);
Reverse_array(uservalues, myarraysize);
Show_array(uservalues, myarraysize);

cout << "back in main" << endl;

system("PAUSE");
return 0;
}

void Fill_array(double myarray[], int j)
{
cout << "I'm in Fill array" << endl;
int numberofentries = 0;
int counter = 0;

for (int i = 0; i < j; i++)
{
cout << "Enter a value to be stored in array" << endl;
cin >>  myarray[i];
counter = counter + 1;
}

numberofentries = counter;
cout << "Values you entered: " << numberofentries << endl;

}

void Show_array(double myarray[], int j)
{

cout << "Now I'm in Show array" << endl;
cout << "This is what you entered into the array" << endl;
for (int i = 0; i < j; i++)
{
cout << myarray[i] << endl;
}

}

void Reverse_array(double myarray[], int j)
{
cout << "Now I'm in Reverse array" << endl;
myarray[0] = myarray[4];
myarray[1] = myarray[3];
myarray[2] = myarray[2];
//myarray[3] = myarray[4];
//myarray[4] = myarray[3];

}

• 09-25-2002
Magos
Traverse the array from the beginning until you reach the middle. Swap the first element with the last, second element with the second last etc...
Code:

void SwapValues(double* Value1, double* value2)
{
double Temp = *Value1;
*Value1 = *Value2;
*Value2 = Temp;
}

void ReverseArray(double MyArray[], int SizeOfArray)
{
for(int i = 0; i < (int)(SizeOfArray / 2); i++)
{
SwapValues(&MyArray[i], &MyArray[SizeOfArray - i - 1]);
}
}

• 09-25-2002
use a temporary variable to swap values:

Code:

void reverseArray(int * array, int num)
{
int temp;
int i;
int number = num - 1;//assumes num is number of ints in array
for(i = 0; i < num/2; i++, number--)
{
temp = array[number];
array[number] = array[i];
arrary[i] = temp;
}
}

• 09-25-2002
Sang-drax
Allow me to introduce something wonderful:

S T L !!!

Code:

#include <algorithm>

void ReverseArray(double* array, int size)
{
std::reverse(array,array+size);
}

• 09-25-2002
if you have access to STL, go for it! If you want to know what STL is doing, study the other two posts.
• 09-25-2002
Captain Penguin
I believe this would work too (incomplete but you can fill in whats needed, which isn't much)-

Code:

for(y = (j-1); y >= 0; y--)
{
temparray[y] = myarray[x];
x++;
}
for(x=0; x < j; x++)
{
myarray[x] = temparray[x];
}

There, that works. Fairly simple to understand, too, IMHO.
• 09-25-2002
jgonzales
Thanks all that helped, I am newbie to C++ and am taking a college course this semester. This is my final working program.

God bless,
Jose

Code:

//Jose Gonzales, #c++04, 95

#include <iostream.h>
#include <stdlib.h>
const int myarraysize = 5;

void Fill_array(double myarray[], int j);
void Show_array(double myarray[], int j);
void Reverse_array(double myarray[], double myrevarray[], int j);
void Swap_array(double myarray[], double myrevarray[]);

int main()
{

double uservalues[myarraysize] = {};
double uservaluesreverse[myarraysize] = {};

cout << "\n";
cout << "Enter " << myarraysize <<" double values to be stored in array" << flush << endl;
Fill_array(uservalues, myarraysize);
cout << "\n";

{
cout << "This is what you entered into the array" << endl;
Show_array(uservalues, myarraysize);
cout << "\n";
Reverse_array(uservalues, uservaluesreverse, myarraysize);
cout << "Now here are your values in reverse order" << endl;
Show_array(uservaluesreverse, myarraysize);
cout << "\n";
Swap_array(uservalues, uservaluesreverse);
cout << "your values in reverse order all but 1st and last elements of array" << endl;
Show_array(uservalues, myarraysize);
}
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";

system("PAUSE");
return 0;
}

void Fill_array(double myarray[], int j)
{
int numberofentries = 0;
int counter = 0;

for (int i = 0; i < j; i++)
{
if (!cin)
{
cin.clear();
cout << "bad input, goodbye." << endl;
cout << "\n";
break;
}
else
{
cin >>  myarray[i];
counter = counter + 1;
}
}

numberofentries = counter;
cout << "total number of entries: " << numberofentries << endl;
}

void Show_array(double myarray[], int j)
{
for (int i = 0; i < j; i++)
{
cout << myarray[i] << endl;
}
}

void Reverse_array(double myarray[], double myrevarray[], int j)
{
for (int i=0; i<j; i++)
{
myrevarray[i] = myarray[j - i -1];
}
}

void Swap_array(double myarray[], double myrevarray[])
{
myarray[1] = myrevarray[1];
myarray[2] = myrevarray[2];
myarray[3] = myrevarray[3];
}