Recusion problem

function in my .cpp file...
void clockType::hoursAwayFromNoon() const
{
cout << "It is " << 12 - hr << " hours before noon." << endl;
cout << "It is " << 12 + hr << " hours past noon." << endl;
}



in main file:


#include <cstdlib>
#include
#include "clocktype.h"
#include "extclocktype.h"

using namespace std;

int main(int argc, char *argv[])
{
clockType myClock;
clockType yourClock;


cout << "Enter time for Clock 1: ";
cin >> myClock;
cout << "You entered " << myClock << " as the time for Clock 1" << endl << endl;
cout << "Enter time for Clock 2: ";
cin >> yourClock;
cout << "You entered " << yourClock << " as the time for Clock 2" << endl << endl;

if (myClock == yourClock)
cout << "clock1 is equal to clock2" << endl;
else
cout << "clock1 does not equal clock2" << endl;

cout << endl;
cout << endl;


cout << "Hour value: ";
int x;
cin >> x;
int ind = 1;

myClock.hoursAwayFromNoon();


yourClock.hoursAwayFromNoon();


cout << endl;

system("PAUSE");
return EXIT_SUCCESS;
}

{
void printHoursAwayFromNoon(int num, int num)
{
if (myClock - x)




Add a recursive function hoursAwayFromNoon() to the ClockType class as a member function to your Week 6 program. The function will return the hours either before or after 12 clock at noon for a given clockType instance. For example, if a clockType instance has the hour value of 5, the function returns a message

Comments

  • You've been given bad instructions, from the look of things.

    Whilst it is possible to write the hoursfromnoon() function recursively, there is little point in doing so. To write a recursive function declare a default "depth" parameter with a default value of zero. Then move the clock one hour towards noon and call again, adding one to the depth parameter. When the clock hits noon, print out the message. I'll leave you to work out how to fiddle with it to get hours before or hours after noon.

    It's a bad exercise because recursion is used when it makes the program simpler to write, not more complex. The usual case is for applying some operation to all the nodes of a tree.

  • well, that's my issue. I figured this was a horrible exercise because it is making things more complicated. I believe so at least. That's the problem, I'm not positive how to make it call one function towards noon then call the other one past noon. That's why Iposted this, so maybe some could throw me some sample code or something.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories