By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

I need help getting started.............

P: n/a
I have an assignment do tonite and I can not get it started. We are
learning about recursion. This is my assignment, and this is what I have
written, could someone please tell me what I am doing
wrong.................THANK YOU.......
ASSIGNMENT:

Demonstrate to the instructor the successful completion of each program
output. Have this sheet ready for instructor to sign. Hand to instructor
to receive credit.

Write a program that will read in an integer number
It will recursively show the conversion between the decimal number and the
Octal number (base 8).
Example program output:
Please enter the decimal number : 43
The decimal number 43 is 53 in octal

You must use a recusive function to produce the output. Using the output
stream to set output to octal is not acceptable.
CODE I HAVE WRITTEN: PLEASE HELP.................

#include <iostream.h>

using namespace std;

main()

{
int num, oct, idiv, irmdr;
{
cout<<"Please enter the decimal number : ";
cin>>num;
cout<<"The decimal number "<<num;
cout<<" is "<<oct<<" in octal";
displayOct(num);
}

void displayOct(int pnum)
{
int idiv, irmdr;
irmdr=pnum%2;
idiv=pnum/8;
if (idiv>0) displayOct(idiv);
}
return 0;
}

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
bigcjr44 wrote:
I have an assignment do tonite and I can not get it started. We are
learning about recursion. This is my assignment, and this is what I have
written, could someone please tell me what I am doing
wrong.................THANK YOU.......
ASSIGNMENT:

Demonstrate to the instructor the successful completion of each program
output. Have this sheet ready for instructor to sign. Hand to instructor
to receive credit.

Write a program that will read in an integer number
It will recursively show the conversion between the decimal number and the
Octal number (base 8).
Example program output:
Please enter the decimal number : 43
The decimal number 43 is 53 in octal

You must use a recusive function to produce the output. Using the output
stream to set output to octal is not acceptable.
CODE I HAVE WRITTEN: PLEASE HELP.................

#include <iostream.h>

using namespace std;

main()

{
int num, oct, idiv, irmdr;
{ Extra brace.
cout<<"Please enter the decimal number : ";
cin>>num;
cout<<"The decimal number "<<num;
cout<<" is "<<oct<<" in octal"; The variable "oct" has not been initialized, so
it will print garbage. Bascially, you can't print
a result until you've calculated it.
displayOct(num); Does this function return the octal value or
does it place the result into its parameter?
} For completeness you should have:
return EXIT_SUCCESS;
at the end of main().


void displayOct(int pnum)
{
int idiv, irmdr;
irmdr=pnum%2;
idiv=pnum/8;
if (idiv>0) displayOct(idiv);
} Does this match the opening brace of the function?
return 0; Your function is declared as void, yet here
it returns a value. What's the deal?
}


I highly suggest you use pencil and paper to trace
this recursive code.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.