Circulate an Array

problem statement
Write a C program to read an array of size n and also read an integer m. Perform left circular rotation of the array elements
 m times. Print the array elements after each rotation.
      Prototypes
       ******************************
       int read_count();
       void read_array(int a[], int size);
       void print_array(int a[], int size);
       void rotation(int a[], int size, int m);  // m is the number of rotations

   Example:
         Input :
            Enter the value of n : 4
            Enter the array elementes : 1 2 3 4
            Enter no. of rotations : 3

         Output:
                 2 3 4 1
                 3 4 1 2
                 4 1 2 3


#include<stdio.h>
#include<stdlib.h>
int read_count();
void read_array(int a[], int size);
void print_array(int a[], int size);
void rotation(int a[], int size, int m);
int main()
{
  int size,*a,m;
  size=read_count();
  a=(int*)malloc(size*sizeof(int));
  read_array(a,size);
  printf("enter how many times circu;ating have to be done \n");
  scanf("%d",&m);
  rotation(a,size,m);
  return 0;
}
int read_count()
{
  int size;
  printf("ENTER THE SIZE OF THE ARRAY \n");
  scanf("%d",&size);
  return size;
}
void read_array(int a[],int size)
{
  int i;
  printf("enter the %d entrys in the  array \n",size);
  for(i=0;i<size;i++)
    {
      scanf("%d",&a[i]);
    }

  return ;
}
void rotation(int a[], int size, int m)
{
  int i,temp,j;
  for(i=0;i<m;i++)
    {
      temp=a[0];
      for(j=0;j<size-1;j++)
{
 a[j]=a[j+1];
}
      a[size-1]=temp;
      print_array(a,size);
    }
  return ;
}
void print_array(int a[],int size)
{
  int i;
  for(i=0;i<size;i++)
    {
      printf("%d ",a[i]);
    }
  printf("\n");
  return ;
}



No comments:

Post a Comment