CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2002
    Posts
    16

    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;
    
    }

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  3. #3
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  5. #5
    Join Date
    Aug 2002
    Posts
    16
    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..

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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
  •  





Click Here to Expand Forum to Full Width

Featured