|
-
November 8th, 2002, 01:09 AM
#1
How to stop returning the address in memory?
Can't seem to access the required elements in the following code. Pretty simplistic program, but hey... gotta start somewhere right?
Code:
#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;
}
-
November 8th, 2002, 01:19 AM
#2
You can't send an array to cout.
Code:
/*...*/
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
-
November 8th, 2002, 01:21 AM
#3
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
-
November 8th, 2002, 01:23 AM
#4
Also... (maybe I should read everything the first time around)
Code:
for(i = 0; i < arraySize; i++)
tempArray[i] = p[(arraySize - 1) - 1];
should be
Code:
for(i = 0; i < arraySize; ++i)
tempArray[i] = p[i];
You can also accomplish this much clearer by using std::copy.
Jeff
-
November 8th, 2002, 02:00 AM
#5
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..
-
November 8th, 2002, 08:41 AM
#6
actually mirror is fine, but you put a 1 instead of an i 
Code:
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;
}
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|