473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

rotate array without using temp/global/local storage

Hi folks,
I have a problem. my function call is
void Rotate_n(int array[], int arraySize,int n);
//precondition: arraysize>=1 && n>=0 && n<=arraySize
//post condition: Values in the arrary are rotated forward(left to right)
by n positions.
eg: if the original array is {1,2,3,4,5}
then, rotate_n(array,5,1); will make it{5,1,2,3,4}
my problem is, how do i manipulate the array without using temp??
Can anyone give me a pointer as to how i can achieve the same?
my line of thoughts is, to manipulate the "index" of the array as opposed to the contents/values stored in the array.


thx
quiz123(fairly new to C++)
Feb 12 '07 #1
3 6514
horace1
1,510 Expert 1GB
if you are using C++ you could write your own class which could overload operator[] to map the supplied index to the actual index. Would there be a requirement to add/delete elements - if so this would add a layer of complexity.

The easy alternative is to shuffle the array elements to get the new origin to iindex 0 - however, you would need to use a temporary variable durring the exchanging of elements.
Feb 12 '07 #2
hi,
Here is the solution... for ur problem
It is perfectly based on ur requirement


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. void rotate_n(int *array, int size, int n)
  4. {
  5.    int ii, jj;
  6.  
  7.    for (ii = 1; ii <= n; ii++)
  8.    {
  9.      for (jj = size-1; jj > 0; jj--)
  10.      {
  11.        /* swapping the array[jj] and array[jj-1] elements without
  12.           using temporary variable */
  13.         array[jj-1] = array[jj-1] + array[jj];
  14.         array[jj] = array[jj-1] - array[jj];
  15.         array[jj-1] = array[jj-1] - array[jj];
  16.      }
  17.    }
  18.  
  19.    return;
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25.     int x[5] = {1,2,3,4,5}, n, ii;
  26.  
  27.     printf("Enter n value : \n");
  28.     scanf("%d", &n);
  29.  
  30.     rotate_n(x, 5, n);
  31.  
  32.     printf("After swapping\n");
  33.     for (ii = 0; ii < 5; ii++)
  34.     {
  35.       printf("%5d", x[ii]);
  36.     }
  37.     printf("\n");
  38.  
  39.     return 0;
  40. }
Feb 12 '07 #3
thanks for your prompt replies. It finally made sense.
Feb 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
8
by: FIFO | last post by:
Hi, Can you suggest me a (time) efficient way to "rotate" a array of char? In particular: - The array of char have a fixed (26) length - The array of char will not be modified in the code...
37
by: Carol Depore | last post by:
How do I determine the maximum array size? For example, int a works, but a does not (run time error). Thank you.
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
8
by: Michel Rouzic | last post by:
I had a program that worked perfectly, and that read .wav files. I changed something so the tags of the wave file, instead of being each in a different variable, are all in an array, called tag. i...
8
by: lovecreatesbeauty | last post by:
I write a function to rotate a matrix by 90 degrees clockwise, this function works on a matrix of specific size, for example, it rotates a 4*4 matrix of integers in the following code. The function...
19
by: Mark | last post by:
i need to make a class that can store anything (integers,objects,etc) it needs to maintain a list of pointers to these objects the list needs to be resized to accomodate more elements i can't use...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
8
by: martinsmith160 | last post by:
Hi everyone I am trying to create a simple wpf program that allows a user to select an image from a combo box and then where they click on th screen draw that image at the mouse co-ordinates. The...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.