You are on page 1of 2

Property of Lindenwood University.

C++ Functions Class

A Basic Rotate Function

This program prompts 3 numbers and the number of times the user would
like to rotate said numbers, and returns the result. It uses three
functions to make the prompt and the calculation.

#include <iostream>
using namespace std;

void print_triple(int, int, int);

void rotate(int&, int&, int&, int);

int prompt_and_get(int&, int&, int&);

int main()
{
int a, b, c, positions;

positions=prompt_and_get(a,b,c);

cout << "Before rotate, values are: ";


print_triple(a, b, c);

rotate(a, b, c, positions);

cout << "After rotate, values are: ";


print_triple(a, b, c);

return 0;
}

void print_triple(int x, int y, int z)


{
cout << x << " " << y << " " << z << endl;
}

void rotate(int &d, int &e, int &f, int position1)


{
int x=d, y=e, z=f;

if(position1 % 3 == 1)
{
e=x, f=y, d=z;
}
else if(position1 % 3 == 2)
{
d=y, e=z, f=x;
}
else
{
d=x, e=y, f=z;
}
}

int prompt_and_get(int &x, int &y, int &z)


{
int positions;

cout << "Enter the three values to rotate: ";


cin >> x >> y >> z;

cout << "How many positions do you want to rotate by? ";
cin >> positions;

return positions;
}

You might also like