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

B

bigcjr44

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

Thomas Matthews

bigcjr44 said:
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
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top