array rotation

#include<iostream>
#include<cstdlib>
using namespace std;

void rotateRight(char a[],int size)
{
for(int i =size;i<;i++)
a[i]=a[i-1];

}

void display(char a[],int size)
{
for(int i =0;i<size;i++)
cout<<a[i];

cout<<endl;
}
int main()
{
const int funsize = 41;
char funtext[funsize] = "ARRAY IS NICE TO BE USED & FUN TO PLAY! ";

display( funtext, funsize-1 );
rotateRight( funtext, funsize-1 );
display( funtext, funsize-1 );
}
any problem with my function?
You are trying to reverse the array ? if it is then you can't serialise the elements at a time in one array in rotateright
Try this,


1
2
3
4
5
6
7
8
9
10
11
12
13

void rotateRight(char arr[], int size)
{
 char temp;
 int i;
for(i=0;i<=size;i++,size--)
  {
    temp = arr[i];
    arr[i] = arr[size];
    arr[size] = temp;
  }
}
You are probably trying to do find the algorithm yourself for an assignment or something, but the C++ standard library provides a simple solution to what you are looking for:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>

int main()
{
	int arr[5] = { 1, 2, 3, 4, 5 };
	
	std::reverse(arr, arr + 5); 
        //array now contains { 5, 4, 3, 2, 1 }
}
Topic archived. No new replies allowed.