473,748 Members | 5,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rotate array without using temp/global/local storage

5 New Member
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 6558
horace1
1,510 Recognized Expert Top Contributor
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
kvbreddy
23 New Member
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
quiz123
5 New Member
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
2309
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
12217
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 - The rotation is by only one char to the right side
37
48742
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
4465
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 accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
8
1924
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 also set macros such as #define bitspersample tag so when in my original program it was written bitspersample now it acts like its tag, and i also moved things that were in the main function into specific functions. anyways, the program read the...
8
11046
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 makes one more copy of the original matrix, how is the efficiency? Can the copy be saved? Comments on it are welcome. Can I extend it to work on a matrix of any size (or even any type, not just integers) for the logic may be the same when it...
19
2485
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 any special classes i just can't seem to figure it out. i've got a pointer, void *storage
112
5460
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 that may print some messages. foo(...) { if (!silent)
8
7785
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 prolem im having is i can load a single local file (a png file) into a bitmapImage source it works perfectly but if i try and store a number of bitmapImages in an array and try and load a local file it always creates a program crash. The first...
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.