0

how do i avoid using pointer variables and pointer-based pass-by-reference in this program? as my instructor said there is no need to use pointers. This is a the tortoise and the hare simulator , you will use number generation to develop a simulation of this memorable event.

#include <iostream>
using std::cout;
using std::endl;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include <iomanip>
using std::setw;

const int RACE_END = 70;

// prototypes
void moveTortoise( int *const );
void moveHare( int *const );
void printCurrentPositions( const int *const, const int *const );

int main()
{
   int tortoise = 1;
   int hare = 1;
   int timer = 0;

   srand( time( 0 ) );

   cout << "ON YOUR MARK, GET SET\nBANG !!!!"
      << "\nAND THEY'RE OFF !!!!\n";

   // loop through the events
   while ( tortoise != RACE_END && hare != RACE_END )
   {
      moveTortoise( &tortoise );
      moveHare( &hare );
      printCurrentPositions( &tortoise, &hare );
      timer++;
   } // end loop

   if ( tortoise >= hare )
      cout << "\nTORTOISE WINS!!! YAY!!!\n";
   else
      cout << "\nHare wins. Yuch.\n";

   cout << "\nTIME ELAPSED = " << timer << " seconds" << "\n" << endl;

   system("pause");

   return 0; // indicates successful termination
} // end main

// progress for the tortoise
void moveTortoise( int * const turtlePtr )
{
   int x = 1 + rand() % 10; // random number 1-10

   if ( x >= 1 && x <= 5 ) // fast plod
      *turtlePtr += 3;
   else if ( x == 6 || x == 7 ) // slip
      *turtlePtr -= 6;
   else // slow plod
      ++( *turtlePtr );

   if ( *turtlePtr < 1 )
      *turtlePtr = 1;
   else if ( *turtlePtr > RACE_END )
      *turtlePtr = RACE_END;
} // end function moveTortoise

// progress for the hare
void moveHare( int * const rabbitPtr )
{
   int y = 1 + rand() % 10; // random number 1-10

   if ( y == 3 || y == 4 ) // big hop
      *rabbitPtr += 9;
   else if ( y == 5 ) // big slip
      *rabbitPtr -= 12;
   else if ( y >= 6 && y <= 8 ) // small hop
      ++( *rabbitPtr );
   else if ( y > 8 ) // small slip
      *rabbitPtr -= 2;

   if ( *rabbitPtr < 1 )
      *rabbitPtr = 1;
   else if ( *rabbitPtr > RACE_END )
      *rabbitPtr = RACE_END;
} // end function moveHare

// display new position
void printCurrentPositions( const int * const snapperPtr,
   const int * const bunnyPtr )
{
   if ( *bunnyPtr == *snapperPtr )
      cout << setw( *bunnyPtr ) << "OUCH!!!";
   else if ( *bunnyPtr < *snapperPtr )
      cout << setw( *bunnyPtr ) << 'H'
         << setw( *snapperPtr - *bunnyPtr ) << 'T';
   else
      cout << setw( *snapperPtr ) << 'T'
         << setw( *bunnyPtr - *snapperPtr ) << 'H';

   cout << '\n';
} // end function printCurrentPositions

2 Answers2

0

In C++ you can use references instead of pointers. For example, instead of

void foo(int *x) {
  *x = *x + 1;
}

int main() {
  int a = 0;
  foo(&a);
  return 0;
}

you can pass x by reference, like so:

void foo(int &x) {
  x = x + 1;
}
int main() {
  int a = 0;
  foo(a);
  return 0;
}

Passing a reference is sort of like passing a pointer, except you don't need to dereference the pointer every time you want to access the value it points to. You can google "C++ pass by reference" for more information, such as this tutorial: http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Alternatively, in your program, you could simply pass int arguments and return the new value:

int moveTortoise(int turtle) {
  ...
  turtle = turtle + 3;
  ...
  return turtle;
}

tortoise = moveTortoise(tortoise)
Emily
  • 5,869
  • 1
  • 22
  • 15
0

References& and pointers* are usefull when:
1. you deal with instances of complex classes that passing by reference is resource(CPU time & main memory) consuming operation;
2. when you want to change arguments what are passed(as any function in C++ can return only one value, opposit for ex. to python where multiply values can be returned, you can cope with that restriction by passing using & or *);
3. Other cases...

Built-in(atomic) types can be passed by value (which is you case) without decrease in efficiency.

spin_eight
  • 3,925
  • 10
  • 39
  • 61