1

Edit: Solved

This is a program to rotate an array of size 'n' by 'd' towards the left. For example: 1 2 3 4 5 6 7 for d=3 goes to 4 5 6 7 1 2 3. My problem is regarding the final loop in int main() that is being used to print the array. That loop is not printing the array upto all it's 'n' members.

I've tried printing the array for a specific 'n' for example n=7. It works in that case. Hence, I don't think there is any logical error in the rotleft() function.

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

void rotleft(int arr[],int d,int n)
{
    int temp[d];
    for(int i=0;i<d;i++)
    {
        temp[i]=arr[i];
    }
    for(int i=0;i<n-d;i++)
    {
        arr[i]=arr[i+d];
    }
    for(int i=n-d;i<n;i++)
    {
        arr[i]=temp[i-n+d];
    }

}

int main()
{
    int n,d,arr[n];

    cin>>n>>d;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    rotleft(arr,d,n);
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    return 0;
}

When I replaced 'n' by 7 in the final loop I got the accurate result, but for general 'n', it shows 4 5 6 instead of 4 5 6 7 1 2 3 (refer to above example).

Exulansis
  • 23
  • 1
  • 7
  • Can you give a specific `n` example, the results you should have and what you actually have? – AAA May 31 '19 at 10:21
  • Let n=7 and d=3 and arr={1,2,3,4,5,6,7}. I am expecting 4 5 6 7 1 2 3, but am getting 4 5 6. – Exulansis May 31 '19 at 10:23
  • declare arr after getting n value. – Prasanth Ganesan May 31 '19 at 10:24
  • @Exulansis, okay, make the edit I suggested in my answer below. – AAA May 31 '19 at 10:29
  • You could improve the question: Provide a [mcve]. In particular, don't rely on anyone to input any numbers but hardcode them there, after all it's only goal is to demonstrate that an algorithm misbehaves, not the input. Also, provide the actual and expected output from the program. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt May 31 '19 at 10:33
  • **Recommended reading:** "[Why should I not #include ?](https://stackoverflow.com/q/31816095/560648)" – Lightness Races in Orbit May 31 '19 at 10:50
  • 1
    Variable length arrays are not part of C++. Use `std::vector` instead. – Eljay May 31 '19 at 11:30

3 Answers3

4

You are creating the array before n is initialized, so there might not be space for all members.

// n is used here
int n,d,arr[n];
// Initialized here
cin>>n>>d;

Instead of array, you could use for example std::vector. With it you don't have to know the size when declaring the variable.

VLL
  • 9,634
  • 1
  • 29
  • 54
0

C++ does not support array of dynamic size yet. The size of array needs to be known at compile time. So as suggested already, use vector instead of array if your size is dynamic.

You may achieve the same result by using already existing library function: std::rotate(). However, if you are trying to learn to implement rotate yourself then, its fine.

Rushikesh
  • 163
  • 8
-3

For dynamically allocated arrays you should use pointers and memory allocation on the heap. Otherwise the variables get assigned random values from memory.

http://www.cplusplus.com/doc/tutorial/dynamic/

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

void rotleft(int* arr,int d,int n)
{
    int temp[d];
    for(int i=0;i<d;i++)
    {
        temp[i]=arr[i];
    }
    for(int i=0;i<n-d;i++)
    {
        arr[i]=arr[i+d];
    }
    for(int i=n-d;i<n;i++)
    {
        arr[i]=temp[i-n+d];
    }

}

int main()
{
    int n,d;

    cin>>n>>d;
    int* arr = new int[n];

    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    rotleft(arr,d,n);
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }

    delete [] arr;
    return 0;
}
Ludo
  • 484
  • 2
  • 9
  • Nooooo don't use `new`. Use smart pointers. And your code has a memory leak. – Lightness Races in Orbit May 31 '19 at 10:49
  • for smart pointers OP should state what version of C++ is he using, thanks for mentioning memory leak though – Ludo May 31 '19 at 10:55
  • @Ludo I don't understand how static allocation to arr is a problem. Why does it work if it's incorrect? "Otherwise the variables get assigned random values from memory." How does that happen, can you explain with an example? Which variables will get those random values in my program? Nothing similar to that happened when I ran it several times. – Exulansis May 31 '19 at 10:58
  • @Lightness Races in Orbit ,Where is the memory leak? also I don't know anything about smart pointers yet though... – Exulansis May 31 '19 at 11:01
  • @Exulansis: Ludo edited the answer since my comment. – Lightness Races in Orbit May 31 '19 at 11:03
  • @Exulansis your free memory in PC is populated with random data, now whenever your application starts it takes some chunks from this random free memory and assigns it to your statically allocated variables (including arrays), thus you cannot predict what values are assinged at the start of application with random `n` at the start, you statically allocated random size of your `arr` array – Ludo May 31 '19 at 11:03
  • even in c++98 there is no reason not to use some sort of smartpointer – 463035818_is_not_an_ai May 31 '19 at 11:31
  • @formerlyknownas_463035818 std::auto_ptr is removed in C++17, others not present in C++98 – Ludo May 31 '19 at 11:38