C
chirag
hi i am providing my code to shift right and shift left as stated in the
comments. it does compile but it does not work for the pointers and does
not shift right. please help.
#include <iostream>
#include <cassert>
#include "ListA.h"
#include "ListA.cpp"
using std::cin;
using std::cout;
using std::endl;
void rotateRef(int &x, int &y, int &z, int direction, bool &success)
// If direction equals 0, the values of the variables are rotated to
// the right (i.e. y is set equal to x, z is set equal to y, and x is
// set equal to z). If direction equals 1, the values of the
// variables are rotated to the left. Success is set to true if a
// correct direction is entered, and to false otherwise.
{
int temp;
if (direction == 0)
{
temp=y;
y=x;
x=z;
z=temp;
}
else if (direction == 1)
{
temp=x;
x=y;
y=z;
z=temp;
}
}
void testRotateRef()
// Function to test rotateRef. Reads three values and a direction from
// the keyboard, passes them to rotateRef, and then prints the result.
// Prints a message if an error is reported by rotateRef.
{
int direction;
int x;
int y;
int z;
bool success;
cout<< "Enter three values followed by one space:"<< endl;
cin>>x;
cin>>y;
cin>>z;
cout<< "Enter 1 to totate right or 0 to rotate left"<<endl;
cin>>direction;
if (direction = 0)
{
rotateRef(x,y,z,direction,success);
cout <<x<<y<<z<<".\n";
}
else if (direction = 1)
{
rotateRef(x,y,z,direction,success);
cout <<x<<y<<z<<".\n";
}
}
void rotatePtr(int *xPtr, int *yPtr, int *zPtr, int direction, bool
&success)
// If direction equals 0, the values of the variables are rotated to the
// right. If direction equals 1, the values of the variables are
// rotated to the left. Success is set to true if a correct direction is
// entered, and to false otherwise.
{
int temp;
if (direction == 0)
{
success = true;
temp=*yPtr;
*yPtr=*xPtr;
*xPtr=*zPtr;
*zPtr=temp;
}
else if (direction == 1)
{
success = true;
temp=*xPtr;
*xPtr=*yPtr;
*yPtr=*zPtr;
*zPtr=temp;
}
else
{
(success = false);
}
}
void testRotatePtr()
// Function to test rotatePtr. Reads three values and a direction from
// the keyboard, passes them to rotateRef, and then prints the result.
// Prints a message if an error is reported by rotatePtr.
{
int direction;
int *xPtr;
int *yPtr;
int *zPtr;
bool success;
cout<< "Enter three values followed by one space:"<< endl;
cin>>*xPtr;
cin>>*yPtr;
cin>>*zPtr;
cout<< "Enter 1 to totate right or 0 to rotate left"<<endl;
cin>>direction;
if (direction = 0)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else if (direction = 1)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else
{
cout<<"Error"<<endl;
}
}
comments. it does compile but it does not work for the pointers and does
not shift right. please help.
#include <iostream>
#include <cassert>
#include "ListA.h"
#include "ListA.cpp"
using std::cin;
using std::cout;
using std::endl;
void rotateRef(int &x, int &y, int &z, int direction, bool &success)
// If direction equals 0, the values of the variables are rotated to
// the right (i.e. y is set equal to x, z is set equal to y, and x is
// set equal to z). If direction equals 1, the values of the
// variables are rotated to the left. Success is set to true if a
// correct direction is entered, and to false otherwise.
{
int temp;
if (direction == 0)
{
temp=y;
y=x;
x=z;
z=temp;
}
else if (direction == 1)
{
temp=x;
x=y;
y=z;
z=temp;
}
}
void testRotateRef()
// Function to test rotateRef. Reads three values and a direction from
// the keyboard, passes them to rotateRef, and then prints the result.
// Prints a message if an error is reported by rotateRef.
{
int direction;
int x;
int y;
int z;
bool success;
cout<< "Enter three values followed by one space:"<< endl;
cin>>x;
cin>>y;
cin>>z;
cout<< "Enter 1 to totate right or 0 to rotate left"<<endl;
cin>>direction;
if (direction = 0)
{
rotateRef(x,y,z,direction,success);
cout <<x<<y<<z<<".\n";
}
else if (direction = 1)
{
rotateRef(x,y,z,direction,success);
cout <<x<<y<<z<<".\n";
}
}
void rotatePtr(int *xPtr, int *yPtr, int *zPtr, int direction, bool
&success)
// If direction equals 0, the values of the variables are rotated to the
// right. If direction equals 1, the values of the variables are
// rotated to the left. Success is set to true if a correct direction is
// entered, and to false otherwise.
{
int temp;
if (direction == 0)
{
success = true;
temp=*yPtr;
*yPtr=*xPtr;
*xPtr=*zPtr;
*zPtr=temp;
}
else if (direction == 1)
{
success = true;
temp=*xPtr;
*xPtr=*yPtr;
*yPtr=*zPtr;
*zPtr=temp;
}
else
{
(success = false);
}
}
void testRotatePtr()
// Function to test rotatePtr. Reads three values and a direction from
// the keyboard, passes them to rotateRef, and then prints the result.
// Prints a message if an error is reported by rotatePtr.
{
int direction;
int *xPtr;
int *yPtr;
int *zPtr;
bool success;
cout<< "Enter three values followed by one space:"<< endl;
cin>>*xPtr;
cin>>*yPtr;
cin>>*zPtr;
cout<< "Enter 1 to totate right or 0 to rotate left"<<endl;
cin>>direction;
if (direction = 0)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else if (direction = 1)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else
{
cout<<"Error"<<endl;
}
}