-1

Can anyone please explain why the loop only execute once!!

The for loop executes once and never reaches end of the programm and if you've got some time to review my mistakes then please point out mistakes because i know this is not how it is done!!

using namespace std;
#include<iostream>

int* rotate(int* ar,int d,int n)
{
    int tmp[n];
    d = d%n;

    for (int i = 0; i < n; ++i)
    {
        tmp[i] = ar[(i+d)%n];
    }
    free(ar);
    return tmp;
}


int main(int argc, char const *argv[])
{
    int arr[] = {1,2,3,4,5};
    int *a;
    int n = sizeof(arr)/sizeof(arr[0]);
    a = rotate(arr,4,n);
    cout<<endl;

    for (int i = 0; i < n; ++i)
    {
         cout<<i<<endl;
         cout<<a[i]<<endl;
    }
    cout<<"End";
    return 0;
}
nish_13
  • 23
  • 1
  • 6

1 Answers1

2

Lets take it apart...

int* rotate(int* ar,int d,int n)
{
    int tmp[n];                     // 1
    d = d%n;
    for (int i = 0; i < n; ++i)
    {
        tmp[i] = ar[(i+d)%n];
    }
    free(ar);                       // 2
    return tmp;                     // 3
}

int tmp[n]; is not standard C++. If you do not want to use std::vector the proper replacement would be a dynamically allocated array.

You call free with a pointer that was not allocated via malloc, which invokes undefined behavior. In C++ you shouldn't be using free and malloc at all, but rather new and delete. And also new and delete only for such exercise. Otherwise use smart pointers.

You return a pointer to a local variable. The pointer is dangling and using it in main invokes undefined behavior.

You can't simply replace the static array arr in main with something else in the function. A function called rotate is not expected to create a new array or delete the one that was passed (also because like in your case it is just not possible).

There are different ways to fix your code. I choose to make rotate rotate the array "in-place". However, as you can see, the implementation actually uses an additional array of same size. I leave it to you to figure out how to change it to use less additional memory:

#include <iostream>
using std::cout;
using std::endl;

void rotate(int* ar,int d,int n)
{
    int* tmp = new int[n];
    d = d%n;
    for (int i = 0; i < n; ++i)
    {
        tmp[i] = ar[(i+d)%n];
    }
    for (int i = 0; i < n; ++i)
    {
        ar[i] = tmp[i];
    }                       
}


int main(int argc, char const *argv[])
{
    int arr[] = {1,2,3,4,5};
    int n = sizeof(arr)/sizeof(arr[0]);
    rotate(arr,4,n);
    cout<<endl;

    for (int i = 0; i < n; ++i)
    {
         cout<<i<<endl;
         cout<<arr[i]<<endl;
    }
    cout<<"End";
    return 0;
}

This is how you can do the same using std::rotate:

#include <array>
#include <iostream>
#include <algorithm>

int main(int argc, char const *argv[])
{
    int arr[] = {1,2,3,4,5};
    int n = sizeof(arr)/sizeof(arr[0]);
    std::rotate(std::begin(arr),std::begin(arr)+4,std::end(arr));

    for (int i = 0; i < n; ++i)
    {
         std::cout << i << std::endl;
         std::cout << arr[i] << std::endl;
    }
    std::cout<<"End";
}

... it even uses pointers ;)

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thanks for all this explanation....and yes a lot better code than this can be written...but my c++ knowledge is not to that point!! – nish_13 Nov 19 '20 at 06:54
  • @nish_13 don't you agree that the code that uses `std::rotate` is much simpler than the one with the handwritten algorithm? – 463035818_is_not_an_ai Nov 19 '20 at 10:18
  • yes i totally agree that it is simpler as well as efficient (the code you suggested). but i was just kind of exploring more on this ( the code i wrote )!! And thanks for juggling with me like this : ) others don't even bother to answer – nish_13 Nov 19 '20 at 17:16