Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Copy array in reverse order

In this example, we will copy array in reverse order

#include <iostream>
using namespace std;

// Function to print an array
void printArray(int arr[], int len) {
    for(int i = 0; i < len; i++) {
        cout << arr[i] << " "; // Print each element
    }
    cout << endl; // To move to the next line after printing the array
}

int main() {
    int original_arr[] = {1, 2, 5, 6, 8, 9};  // Initializing the original array
    int len = sizeof(original_arr) / sizeof(int);  // Calculating the length of the array

    int copy_arr[len];  // Declaring the copy array with the same length

    // Copying elements from original_arr to copy_arr in reverse order
    for(int i = 0; i < len; i++) {
        copy_arr[i] = original_arr[len - i - 1];  // Copy from the end of original_arr to the beginning of copy_arr
    }

    cout << "Print original array:" << endl;
    printArray(original_arr, len);  // Printing the original array

    cout << "Print reversed copy array:" << endl;
    printArray(copy_arr, len);  // Printing the reversed copy array

    return 0;
}

Output:

Print original array:
1 2 5 6 8 9 
Print reversed copy array:
9 8 6 5 2 1 


Explanation:

Step 1: Function to Print an Array

void printaArray(int arr[], int len) {
    for(int i = 0; i < len; i++) {
        cout << arr[i] << " "; // Print each element
    }
}
  • Function: printaArray(int arr[], int len) is a function that takes an array and its length as parameters and prints each element.
  • For Loop: The for loop iterates over the array and prints each element followed by a space.

Step 2: Main Function – Array Initialization

int main() {
    int original_arr[] = {1, 2, 5, 6, 8, 9};  // Initializing an array
    int len = sizeof(original_arr) / sizeof(int); // Calculating length of the array
  • Array Declaration: int original_arr[] = {1, 2, 5, 6, 8, 9}; defines an array of integers.
  • Array Length Calculation: sizeof(original_arr) / sizeof(int) calculates the length of the array by dividing the total size of the array (sizeof(original_arr)) by the size of one element (sizeof(int)).

Step 3: Copying Elements from One Array to Another

    int copy_arr[len], i; // Declaring the copy array with the same length
    for(i = 0; i < len; i++) {
        copy_arr[i] = original_arr[len - i - 1]; // Copy from the end of original_arr to the beginning of copy_arr
    }
  • Copy Array Declaration: int copy_arr[len] declares another array of the same length as original_arr.
  • Copying Elements: The for loop copies each element from original_arr to copy_arr using copy_arr[i] = original_arr[i];.

Step 4: Printing Both Arrays

    cout << "Print original array" << endl;
    printaArray(original_arr, len); // Calling the function to print original array

    cout << "Print Reverse copy array" << endl;
    printaArray(copy_arr, len); // Calling the function to print Reverse copied array
}
  • Printing Original Array: The function printaArray(original_arr, len) is called to print the elements of the original array.
  • Printing Reverse Copied Array: The function printaArray(copy_arr, len) is called to print the elements of the copied array.

    Leave a Reply

    Your email address will not be published.

    Need Help?