c++ pointers

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;
}
}
 
A

Andre Kostur

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.

[large snip]

Take a look at the prototypes for your two rotate functions. Your int
version takes reference-to-int as parameters. Your pointer version only
takes the pointers by value. You want to pass the pointers by reference as
well...
 
V

Victor Bazarov

chirag said:
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.
[...]
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;

You declared three pointers. What do they point _to_? Nothing.
They are *uninitialised*.
bool success;




cout<< "Enter three values followed by one space:"<< endl;
cin>>*xPtr;

First thing you do is dereference an *uninitialised* pointer. What
does that do? Undefined. Nasal demons. A nasty letter to your mom.
Formatting of your hard drive. Make your pick. Anything after that
does not matter.

Don't declare/define pointers here. Declare/define your regular values
just like you do in testRotateRef. Just pass your ints by taking their
address into the 'rotatePtr':

rotatePtr(&x, &y, &z, ...

V
 
J

Jay Nabonne

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.


if (direction = 0)

if (direction == 0)
{
rotateRef(x,y,z,direction,success);

cout <<x<<y<<z<<".\n";
}

else if (direction = 1)

else if (direction == 1)
{
rotateRef(x,y,z,direction,success);

cout <<x<<y<<z<<".\n";
}
}

if (direction = 0)

if (direction == 0)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else if (direction = 1)

else if (direction == 1)
{
rotatePtr(xPtr,yPtr,zPtr,direction,success);
cout <<xPtr<<yPtr<<zPtr<<".\n";
}
else
{
cout<<"Error"<<endl;
}
}

HTH

- Jay
 
D

davidrubin

chirag wrote:

Just a few additional comments.
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.

First, if a function changes a value, pass a pointer, not a reference
(see Stroustroup). This is a clear indication to the caller that values
can be modified. Second, since your function can fail, you should
return the status as a return value, and the type should be 'int', not
'bool'. You should return 0 on success, and a non-zero value otherwise.
The actual non-zero values are not important to clients of 'rotateRef',
but when you *test* the function, you can verify numerous error legs. A
status of 'false' does not indicate *why* the function failed, and
therefore makes testing different error legs a guessing game. Thirdly,
the specification of 'direction' is a little to strict. What might be
better is

Rotate values to the left by one place if the specified
'direction' is less than zero, or to the right if the
specified 'direction' is greater than zero. If
0 = 'direction' the specified arguments 'x', 'y', and 'z'
are not affected.

Then, since (if) the function has no notion of failure, you can
dispense with the return type completely. (Typically it goes without
saying that invalid (or null) pointers cause undefined behavior.)
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.

Interactive tests for these kinds of (low-level) functions are tedious
if not useless. You want to structure your test driver so that 1) it is
automated, 2) it can verify the results, and 3) it can be used as a
regression test to ensure that changes to the implementation meet the
contract. You want something like this:

// some helpful macros
#define LOOP2_ASSERT(I,J,C) // print I and J, non-fatal assert C
#define L_ __LINE__

// and then
const struct {
int d_line; // source line number
int d_x; // initial x value
int d_y; // initial y value
int d_z; // initial z value
int d_direction; // direction of rotation
int d_expX; // expected x value
int d_expY; // expected y value
int d_expZ; // expected z value
int d_result; // expected result
} DATA[] = {
//Line X Y Z Dir Exp X Exp Y Exp Z Res
//---- --- --- --- --- ----- ----- ----- ---
{ L_, 1, 2, 3, 0, 3, 1, 2, 0, },
{ L_, 4, 5, 6, 1, 5, 6, 4, 0, },
// etc
};
enum { DATA_SIZE = sizeof DATA / sizeof *DATA };

for (int i = 0; i < DATA_SIZE; ++i) {
const int LINE = DATA.d_line;
const int DIRECTION = DATA.d_direction;
const int EXP_X = DATA.d_expX;
const int EXP_Y = DATA.d_expY;
const int EXP_Z = DATA.d_expZ;
const int RESULT = DATA.d_result;

int x = DATA.d_x;
int y = DATA.d_y;
int z = DATA.d_z;

int result = rotateRef(x, y, z, direction);
LOOP2_ASSERT(i, LINE, EXP_X == x);
LOOP2_ASSERT(i, LINE, EXP_Y == y);
LOOP2_ASSERT(i, LINE, EXP_Z == z);
LOOP2_ASSERT(i, LINE, RESULT == result);
}
void rotatePtr(int *xPtr, int *yPtr, int *zPtr, int direction, bool
&success)

At this point, you probably want to consider a template function:

template <typename T>
int rotateArgs(T *a, T *b, T *c, int direction);

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top