Answers for "reverse an array in cpp"

C++
1

reverse sort cpp

int main(){    
  	int arr[5] = {1,3,2,4,5};
	sort(arr, arr+5, greater<int>()); 
  	// arr == {5,4,3,2,1}
  	return 0;
}
Posted by: Guest on September-08-2020
2

reverse an array in c++

#include <iostream>
using namespace std;
int main()
{		// While loop
	const int SIZE = 9;
	int arr [SIZE];
	cout << "Enter numbers: \n";
	for (int i = 0; i < SIZE; i++)
		cin >> arr[i];
	for (int i = 0; i < SIZE; i++)
		cout << arr[i] << "\t";
	cout << endl;
	cout << "Reversed Array:\n";
	int temp, start = 0, end = SIZE-1;
	while (start < end)
	{
		temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}
	for (int i = 0; i < SIZE; i++)
		cout << arr[i] << "\t";
	cout << endl;	
  	return 0;
}
Posted by: Guest on January-12-2021
1

reverse() in c++

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
    vector<int>a = {11,22,33,44,99,55};
    reverse(a.begin(), a.end());
    auto it = a.begin();
    for(it= a.begin(); it!=a.end(); it++){
        cout << *it << ' ';    
    }
}
Posted by: Guest on March-05-2021
1

reverse an array in c++

#include <iostream>
using namespace std;
int main()
{
	const int SIZE = 9;
	int arr [SIZE];
	cout << "Enter numbers: \n";
	for (int i = 0; i < SIZE; i++)
		cin >> arr[i];
	for (int i = 0; i < SIZE; i++)
		cout << arr[i] << "\t";
	cout << endl;
	cout << "Reversed Array:\n";
	int end = SIZE - 1, temp;
	for (int i = 0; i < end; i++)
	{
		temp = arr[i];
		arr[i] = arr[end];
		arr[end] = temp;
		end--;
	}
	/*	Reverse using while loop
	int temp, start = 0, end = SIZE-1;
	while (start < end)
	{
		temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}*/
	for (int i = 0; i < SIZE; i++)
		cout << arr[i] << "\t";
	cout << endl;	
	}
Posted by: Guest on January-12-2021
2

reverse string array java

//java program to reverse array using for loop
public class ReverseArrayDemo 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[]{2, 4, 6, 8, 10};  
      System.out.println("Given array: ");  
      for(int a = 0; a < arrNumbers.length; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
      System.out.println("Reverse array: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}
Posted by: Guest on November-07-2020
0

reverse an array in cpp

#include<iostream>
using namespace std;
int main()
{   
     int n=3;
     int arr[]={1,2,3,4};
      while (n>=0)
    {
        cout<<arr[n];
        n--;
      }
         
    return 0;
}
Posted by: Guest on May-11-2021

Browse Popular Code Answers by Language