have to write a program to rotate the elements of an array a specified number of places input by the user, with these specifications:
1. A positive number indicates a rotation to the right
2. A negative number indicates a rotation to the left
3. Use only pointer notation (not array notation) in the functions other than main.
4. Do not declare additional space to hold an array outside of main; in other words, rotate the array "in place"

Does anyone know how to go about adding into this partial code to make this work?

#define SIZE 10

/* function prototypes go here */
int main()
{
int a = {23, -3, 6, 19, 88, -5, 7, 34, -101, 88};
int places;

printf("Original array:\n");
showArray(a, SIZE);
printf("\n\nRotate how many places (+/- for right/left)? ");
scanf("%d", &places);
rotate(a, SIZE, places);
printf("\nRotated array:\n");
showArray(a, SIZE);

printf("\n\n");
system("pause");
return 0;
}

Recommended Answers

All 2 Replies

That seems like a good outline, but you have to attempt to write the key functions and then we can help you debug it.

I wrote an example program which demonstrates some techniques you can use:

#include <iostream>

using namespace std;

void showArray(const int array[], int size);
void walkArray(const int array[], int el);

int main()
{
    const short SIZE = 10;
    int array[SIZE] = {25, 30, 35, -40, -45, 65, -95, 85, -50, -15};
    int elnum = 0;

    cout << "Original array: ";
    showArray(array, SIZE);
    cout << endl;
    cout << "Type element number: ";
    cin >> elnum;
    if(elnum < SIZE)
    {
        cout << endl << "Element " << elnum << " is: ";
        walkArray(array, elnum);
        cout << endl;
    } else {
        cout << "ERROR: There are only " << SIZE << " elements in the array !" << endl;
    }
    return 0;
}
void showArray(const int array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << *(array+i) << " ";
    }
}
void walkArray(const int array[], int el)
{
    cout << *(array+(el-1)) << endl;
}

(I'm aware of the fact it doesn't do what you asked, but it's you who has to write the code, not me)

BTW: Your code looks very much like C ...
> Try to avoid using 'DEFINE' for constants as it's deprecated, use 'const' instead ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.