Click to See Complete Forum and Search --> : How to stop returning the address in memory?


barbes
November 8th, 2002, 12:09 AM
Can't seem to access the required elements in the following code. Pretty simplistic program, but hey... gotta start somewhere right?



#include <iostream.h>

float* mirror(float[], int);

void main(void)
{
const int size = 5;
float numbers[size] = {1.0, 2.0, 3.0, 4.0, 5.0};

cout << "The numbers before mirror() are..." << endl;

for(int i = 0; i < size; i++)
{
cout << numbers[i] << " ";

}
cout << endl;


cout << "The numbers after mirror() are..." << endl;

for(i = 0; i < size; i++)
{
cout << mirror(numbers, size) << " ";

}


}

float* mirror(float p[], int arraySize)
{
int i;

float* tempArray = new float[arraySize];

for(i = 0; i < arraySize; i++)
tempArray[i] = p[(arraySize - 1) - 1];

return tempArray;

}

jfaust
November 8th, 2002, 12:19 AM
You can't send an array to cout.


/*...*/

cout << "The numbers after mirror() are..." << endl;

float* after = mirror(numbers, size);

for(i = 0; i < size; i++)
{
cout << after[i] << " ";
}

//don't forget this!
delete[]after;

/*...*/


Jeff

jfaust
November 8th, 2002, 12:21 AM
Also, you should use <iostream> instead of <iostream.h>.

Once you feel comfortable with your example, take a look at std::vector. It's a much nicer way of handling arrays.

Jeff

jfaust
November 8th, 2002, 12:23 AM
Also... (maybe I should read everything the first time around)


for(i = 0; i < arraySize; i++)
tempArray[i] = p[(arraySize - 1) - 1];


should be

for(i = 0; i < arraySize; ++i)
tempArray[i] = p[i];


You can also accomplish this much clearer by using std::copy.

Jeff

barbes
November 8th, 2002, 01:00 AM
Thanx for all your help Jeff

Just with that last post..the actual function should return the reverse order of the array...hence (mirror), however I should of called the function reverseOrder or sumthing...

Thanx again..

Yves M
November 8th, 2002, 07:41 AM
actually mirror is fine, but you put a 1 instead of an i :p


float* mirror(float p[], int arraySize)
{
int i;

float* tempArray = new float[arraySize];

for(i = 0; i < arraySize; i++)
tempArray[i] = p[(arraySize - 1) - i];

return tempArray;

}