dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Reversing array in C++

Threaded View

  1. #1
    Join Date
    Sep 2018
    Posts
    4

    Question Reversing array in C++

    Hi, I am new on this forum and in this field of programming as well.
    I was facing problems while coding so I searched for forums where I could ask questions related to programming and found this one.

    Please check this piece of code and lemme know what is the fault in it? My purpose is to reverse the array given by the user.

    Code:
    #include <iostream>
    using namespace std;
    void reverseArray(int *arr, int size)
    {
    	int *Copy_arr = new int[size];
    	int temp;
    	for (int i = 0; i < size; i++)
    	{
    		Copy_arr[i] = arr[i];
    	}
    
    	for (int i = 0; i < size; i++)
    	{
    		temp = arr[i];
    		arr[i] = arr[(size - 1) - i];
    		arr[(size - 1) - i] = temp;
    		
    		
    	}
    	
    
    	cout << "Array before function call was: ";
    	for (int i = 0; i < size; i++)
    	{
    		cout << Copy_arr[i] << " ";
    	}
    	cout << endl << "Array after function call is: ";
    	for (int i = 0; i < size; i++)
    	{
    		cout << arr[i] << " ";
    	}
    	cout << endl;
    
    	
    }
    void main()
    {
    	int size;
    	cout << "Enter size of array\n";
    	cin >> size;
    	int *p = new int[size];
    	cout << "Enter the elements of array\n";
    	for (int i = 0; i < size; i++)
    	{
    		cin >> p[i];
    	}
    	reverseArray(p, size);
    	
    
    }
    Last edited by 2kaud; September 14th, 2018 at 10:48 AM. Reason: Added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)