My code for rotating arrays gives error for large array size

-2

Problem: I was trying to solve this problem from geeksforgeeks on array rotation. The code seems to be working fine for small array size but for large array size it displays some garbage values and then stops working.

The question is on this link: https://practice.geeksforgeeks.org/problems/rotate-and-delete/0

and my code is:

# include<iostream>

using namespace std;

void rotate_clockwise(int *arr, int size)
{
    int temp = *(arr + size -1);
    for(int i = 0;i < size;++i )
    {
        *(arr +(size -1) - i) = *(arr + (size-1) -(i+1));//*(arr +(size -1)) 
    }
    *arr = temp;
}

void initialize_array(int *arr,int size)
{

   for(int  i= 0; i < size; ++i)
   {
        cin>>*(arr +i);
   }


}

void print_array(int *arr,int size)
{
    for(int i = 0; i< size; i++)
    {
        cout<<*(arr + i)<<" ";
    }
    cout<<endl;
}

void remove_ith_fend(int i, int *arr, int *ptr_size)
{   
    int size = *ptr_size;

    int *last = (arr + size -1);

    i = i-1;

    if(i < size)
    {
        for(int j =0 ;j<size;j++)
        {
            *(last - i + j) = *(last -i + j+1);
        }
    }
    else
    {
        for(int j = 0;j<size;++j)
        {
            *(arr + j) = *(arr + j +1);
        }
    }
    --size;
    *ptr_size = size;
    }

int main()
{
    int size;

    cout<<"Enter size of array: ";

    cin>>size;
    int *ptr_size;
    ptr_size = &size;

    int arr[size];

    initialize_array(arr, size);

    print_array(arr, size);

    for(int i =1;size!=1;++i)
    {
        cout<<"rotating"<<endl;

        rotate_clockwise(arr,size);

        print_array(arr,size);

        remove_ith_fend(i, arr, ptr_size);

        cout<<"removing "<<i<<"th element from the end"<<endl;

        print_array(arr,size);
    }

    return 0;
} 

I was only trying this out so I don't take input for test cases which they are giving in the question. Sorry I have not commented the code but I was in the process of solving this and was going to do it later after getting the solution right.

Here is the error I was getting when I entered the array size: 54

Enter size of array: 54
4741824 0 -1 0 4744768 0 4503683 0 1 0 4745728 0 8 0 4751362 0 4744784 0 
7339432 0 7339436 0 1 0 0 0 -1 0 4741824 0 -1 0 4741824 0 10 0 4254288 
16777216 0 54 8390488 0 224 0 -1 0 4200452 0 0 0 0 0 8 0
rotating
0 4741824 0 -1 0 4744768 0 4503683 0 1 0 4745728 0 8 0 4751362 0 4744784 0 
7339432 0 7339436 0 1 0 0 0 -1 0 4741824 0 -1 0 4741824 0 10 0 4254288 
16777216 0 54 8390488 0 224 0 -1 0 4200452 0 0 0 0 0 8
removing 1th element from the end

It didn't asked for any input and just exited after printing this thing above. Also please point out some issues that I have with the way code is written as I am new to programming and like to learn the better way of writing.

I just debugged the code using visual studio and it showed this message

Unhandled exception at 0x0000000000401632 in second.exe: 0xC0000005: Access 
violation reading location 0x0000000000000000. occurred

name of the file is second.

also the correct output should look like this:

Enter size of array: 6
1 2 3 4 5 6
1 2 3 4 5 6
rotating
6 1 2 3 4 5
removing 1th element from the end
6 1 2 3 4
rotating
4 6 1 2 3
removing 2th element from the end
4 6 1 3
rotating
3 4 6 1
removing 3th element from the end
3 6 1
rotating
1 3 6
removing 4th element from the end
3 6
rotating
6 3
removing 5th element from the end
3

Did some more testing and found that it works for array size 8 and 10 but not for 7 and 9 and stops working for values greater than 10.

c++
arrays
asked on Stack Overflow Aug 22, 2018 by Yash Sharma • edited Aug 22, 2018 by halfer

1 Answer

2

This is a problem

void initialize_array(int *arr,int size)
{
   for(int  i ; i < size; ++i)
   {
        cin>>*(arr +i);
   }
}

should be

void initialize_array(int *arr,int size)
{
   for(int i = 0; i < size; ++i)
   {
        cin>>*(arr +i);
   }
}

Your compiler should have wanred you about the uninitialised variable. Pay attention to compiler warnings.

answered on Stack Overflow Aug 22, 2018 by john

User contributions licensed under CC BY-SA 3.0