1
#include <bits/stdc++.h> 
using namespace std; 

/*Prototype for utility functions */
void printArray(int arr[], int size); 
void swap(int arr[], int fi, int si, int d); 

void leftRotate(int arr[], int d, int n) 
{ 
    /* Return If number of elements to be rotated 
    is zero or equal to array size */
    if(d == 0 || d == n) 
        return; 
        
    /*If number of elements to be rotated 
    is exactly half of array size */
    if(n - d == d) 
    { 
        swap(arr, 0, n - d, d); 
        return; 
    } 
        
    /* If A is shorter*/        
    if(d < n - d) 
    { 
        swap(arr, 0, n - d, d); 
        leftRotate(arr, d, n - d);   
    } 
    else /* If B is shorter*/       
    { 
        swap(arr, 0, d, n - d); 
        leftRotate(arr + n - d, 2 * d - n, d); /*This is tricky*/
    } 
} 

/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size) 
{ 
    int i; 
    for(i = 0; i < size; i++) 
        cout << arr[i] << " "; 
    cout << endl; 
} 

/*This function swaps d elements starting at index fi 
with d elements starting at index si */
void swap(int arr[], int fi, int si, int d) 
{ 
    int i, temp; 
    for(i = 0; i < d; i++) 
    { 
        temp = arr[fi + i]; 
        arr[fi + i] = arr[si + i]; 
        arr[si + i] = temp; 
    } 
} 

// Driver Code 
int main() 
{ 
    int arr[] = {1, 2, 3, 4, 5, 6, 7}; 
    leftRotate(arr, 2, 7); 
    printArray(arr, 7); 
    return 0; 
} 

// This code is contributed by Rath Bhupendra 

I found this code on the geek for geeks website. The code is used to rotate the elements of an array. It is mentioned as block swap algorithm in the website, my questions are:

Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+n-d)?

How can we add integers to an array?

I tried adding an integer to an array in an online compiler and it didn't work. But the above code works perfectly giving the desired output 34567.

The link to the website is https://www.geeksforgeeks.org/block-swap-algorithm-for-array-rotation/.

anastaciu
  • 20,013
  • 7
  • 23
  • 43
gm lohith
  • 33
  • 4
  • No, you cannot add any elements to arrays in C++. They have fixed size. The code you showed does not add any elements to any arrays. – Fureeish Aug 30 '20 at 10:05
  • 2
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Aug 30 '20 at 10:17

2 Answers2

2

Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+n-d)?

How can we add integers to an array?

The answer is you can't, and that's not what's happening here.

int arr[] argument decays to a pointer to the first element of the array. It's the same as having int* arr so what you are doing in arr + n - d is simple pointer arithmetic.

The pointer will be moved n - d positions relative to the position it's at before the expression is evaluated.

Supposing the result of n - d is 4, and arr is pointing to the beginning of the array passed as an argument, that is to &arr[0] (in array notation) or arr + 0 (in pointer notation), which is where it's pointing to in its inicial state, you'll have arr + 4 or &arr[4], after the evaluation, the expression provides access to the address of index 4 (the 5th element of the array). To access the value within that address you'd use *(arr + 4) or arr[4].

On a side note I wouldn't advise the use of geeksforgeeks.com to learn C++, or any other language, for that matter, this should be done by reading a good book.

anastaciu
  • 20,013
  • 7
  • 23
  • 43
1

A function parameter having an array type is adjusted by the compiler to pointer to the array element type. That is these two function declarations are equivalent and declare the same one function.

void leftRotate(int arr[], int d, int n);

and

void leftRotate(int *arr, int d, int n);

You even may write for example

void leftRotate(int arr[100], int d, int n);
void leftRotate(int arr[10], int d, int n);
void leftRotate(int arr[1], int d, int n);

Again these declarations declare the function

void leftRotate(int *arr, int d, int n);

So within the function this expression

arr + n - d

uses the pointer arithmetic applied to the pointer arr.

For example the expression arr + 0 is equivalent to arr and points to the first element of the array. The expression arr + n points to the n-th element of the array.

Here is a demonstrative program where there is used the pointer arithmetic to output elements of an array in a loop.

#include <iostream>

int main() 
{
    int a[] = { 1, 2, 3, 4, 5 };
    
    for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
    {
        std::cout << *( a + i ) << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

The program output is

1 2 3 4 5

In the expression *( a + i ) the array designator a is implicitly converted to pointer to its first element.

Here is one more demonstrative program that shows that a function parameter having an array type is adjusted by the compiler to pointer to the array element type.

#include <iostream>
#include <iomanip>
#include <type_traits>

const size_t N = 100;

void f( int a[N] )
{
    std::cout << "\nin function\n";
    std::cout << "sizeof( a ) = " << sizeof( a ) << '\n';
    std::cout << "a is a pointer " << std::boolalpha <<std:: is_same<decltype( a ), int *>::value << '\n';
}

int main() 
{
    int a[N];
    
    std::cout << "In main\n";
    std::cout << "sizeof( a ) = " << sizeof( a ) << '\n';
    std::cout << "a is an array " << std::boolalpha <<std:: is_same<decltype( a ), int [N]>::value << '\n';

    f( a );
    
    return 0;
}

The program output is

In main
sizeof( a ) = 400
a is an array true

in function
sizeof( a ) = 8
a is a pointer true
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268