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++)
3 6514
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.
hi,
Here is the solution... for ur problem
It is perfectly based on ur requirement - #include<stdio.h>
-
-
void rotate_n(int *array, int size, int n)
-
{
-
int ii, jj;
-
-
for (ii = 1; ii <= n; ii++)
-
{
-
for (jj = size-1; jj > 0; jj--)
-
{
-
/* swapping the array[jj] and array[jj-1] elements without
-
using temporary variable */
-
array[jj-1] = array[jj-1] + array[jj];
-
array[jj] = array[jj-1] - array[jj];
-
array[jj-1] = array[jj-1] - array[jj];
-
}
-
}
-
-
return;
-
}
-
-
-
int main()
-
{
-
int x[5] = {1,2,3,4,5}, n, ii;
-
-
printf("Enter n value : \n");
-
scanf("%d", &n);
-
-
rotate_n(x, 5, n);
-
-
printf("After swapping\n");
-
for (ii = 0; ii < 5; ii++)
-
{
-
printf("%5d", x[ii]);
-
}
-
printf("\n");
-
-
return 0;
-
}
thanks for your prompt replies. It finally made sense.
Sign in to post your reply or Sign up for a free account.
Similar topics
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()
{
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |