Search
 
SCRIPT & CODE EXAMPLE
 

CPP

swap alternate elements in an array c++ problem

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to update the array
void UpdateArr(int arr[], int n)
{
 
    // Initialize the pointers
    int i = 0, j = n - 1;
 
    // While there are elements to swap
    while (i < j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
 
        // Update the pointers
        i += 2;
        j -= 2;
    }
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    UpdateArr(arr, n);
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: Converting Strings to Numbers in C/C++ 
Cpp :: how to run cpp using gcc vscode 
Cpp :: ifstream 
Cpp :: C++ Nested if 
Cpp :: remove element from c++ 
Cpp :: time complexity 
Cpp :: c++ check if number is even or odd 
Cpp :: c++ program to find gcd of 3 numbers 
Cpp :: dangling pointer 
Cpp :: convert single character string to char c++ 
Cpp :: C++ function inside main 
Cpp :: heap allocated array in c ++ 
Cpp :: Maximum element in a map c++ 
Cpp :: initialisation of a c++ variable 
Cpp :: replace a char in string c++ at a specific index 
Cpp :: c++ segmentation fault 
Cpp :: c++ square and multiply algorithm 
Cpp :: Consell de forces polítiques de Catalunya 
Cpp :: c++ calling variable constructor 
Cpp :: surf interpolation matlab 
Cpp :: new expression 
Cpp :: kadane algo 
Cpp :: pointer in cpp details 
Cpp :: . Shell sort in c++ 
Cpp :: permutation in c++ with backtracking 
Cpp :: check whether kth bit is 1 
Cpp :: c++ reverse bits 
Cpp :: unreal engine c++ bind action to function with parameter 
Cpp :: map::begin 
Cpp :: C++ (.NET CLI) 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =