The following function converts a positive decimal number to base 8 and displays the result.

void displayOctal (int n)
                   {
                       if (n > 0)
                       {  if (n/8 >0)
                             displayOctal(n/8);
                          cout << n%8;
                       } //end if
                    } //end displayOctal

I have to trace the function with n = 100. Does this simply mean I have to replace 'n' with 800 and write it in pseudocode?

what is wrong with

cout << oct << n;

?


In this context trace is ambiguous. I would have thought it meant that you need to output what the function is doing on each iteration.

And finally n is not a decimal number if you insist on assigning it a base then binary would be most appropriate as you are (probably) working on a binary computer, however the best approach is to not think of it has having a base at all until you try to display it to the user, that is base is a property of how the value is displayed and manipulated not of the value itself.

what is wrong with

cout << oct << n;

?


In this context trace is ambiguous. I would have thought it meant that you need to output what the function is doing on each iteration.

And finally n is not a decimal number if you insist on assigning it a base then binary would be most appropriate as you are (probably) working on a binary computer, however the best approach is to not think of it has having a base at all until you try to display it to the user, that is base is a property of how the value is displayed and manipulated not of the value itself.

This is a question I was given, I didn't write the code myself. :)
The question (word by word) reads as follows:

Consider the following function that convert a positive decimal number to base 8 and displays the result.

void displayOctal (int n)
                   {
                       if (n > 0)
                       {  if (n/8 >0)
                             displayOctal(n/8);
                          cout << n%8;
                       } //end if
                    } //end displayOctal

Describe how the algorithm works. Trace the function with n = 100.

I'm just wondering what the question is actually asking me to do.

Yeah I think it definately means produce a list of how the function is called.

Basically each time displayOctal is called output a line saying its been called and what parameters its been called with so that you trace the operation of the function. <----- still a guess if this is course work check with your professor.

Tracing might also included outputing the functions return value, but this function doesn't have 1.

The trace should provide enough information so that you can follow the exact path through the code that execution has taken.

100 is the intial value to call displayOctal with.

If you output your trace to cerr you can split the proper function output and the trace output.

Yeah I think it definately means produce a list of how the function is called.

Basically each time displayOctal is called output a line saying its been called and what parameters its been called with so that you trace the operation of the function. <----- still a guess if this is course work check with your professor.

Tracing might also included outputing the functions return value, but this function doesn't have 1.

The trace should provide enough information so that you can follow the exact path through the code that execution has taken.

100 is the intial value to call displayOctal with.

If you output your trace to cerr you can split the proper function output and the trace output.

Could you show me an example of this process - the first iteration maybe? How would I output a line saying whats been called? Thanks for your help so far! EDIT: I know that the algorithm is recursive so the last iteration should be the first to display.

cout << "FunctionName: parameter1 = " << <parameter1> << endl;

as the first line of the function or as I said use cerr instead of cout.

cout << "FunctionName: parameter1 = " << <parameter1> << endl;

as the first line of the function or as I said use cerr instead of cout.

What would be in place of '<parameter 1>'?

n

n

void displayOctal (int n)
		cout << "FunctionName: parameter1 = " << n << endl;
                   {
		       n = 100;
                       if (n > 0)
                       {  if (n/8 >0)
                             displayOctal(n/8);
                          cout << n%8;
                       } //end if
                    } //end displayOctal

This is probably wrong.

Describe how the algorithm works. Trace the function with n = 100.

This doesn't even involve coding. It is a written description of the function with a written list of the steps performed when "displayOctal()" is called with an initial argument of 100. It is essentially a simple algebra problem.

This doesn't even involve coding. It is a written description of the function with a written list of the steps performed when "displayOctal()" is called with an initial argument of 100. It is essentially a simple algebra problem.

It's strange that this question in an exam paper is worth 8 marks - is there reason enough to do it both ways? I'd like to know how to find the answers to this question through coding too!

This is probably wrong.

Did it compile?

BTW see FBody's post because I think he is right. It didn't occur to me because after 20 years of programming I am now phsycologically trained to void using a pen and paper where ever possible.

Did it compile?

BTW see FBody's post because I think he is right. It didn't occur to me because after 20 years of programming I am now phsycologically trained to void using a pen and paper where ever possible.

I get 3 errors:
'cout' : unknown override specifier
syntax error : missing ';' before '<<'
'{' : missing function header (old-style formal list?)

If you could help me get this to work in code form it would be terrific as the question in the past paper is worth 8 marks which might ask for a bit more of an answer.

This is the full code I'm working with:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <math.h>
#include <string>
using std::string;
using namespace std;

void displayOctal (int n)
		cout << "FunctionName: parameter1 = " << n << endl;
                   {
		       n = 100;
                       if (n > 0)
                       {  if (n/8 >0)
                             displayOctal(n/8);
                          cout << n%8;
                       } //end if
                    } //end displayOctal

The cout code needs to be inside the braces for displayOctal

The cout code needs to be inside the braces for displayOctal

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <math.h>
#include <string>
using std::string;
using namespace std;

void displayOctal (int n)
                   {
cout << "FunctionName: parameter1 = " << n << endl;
		       n = 100;
                       if (n > 0)
                       {  if (n/8 >0)
                             displayOctal(n/8);
                          cout << n%8;
                       } //end if
                    } //end displayOctal

Like so? I'm compiling this using Visual C++ and I'm now getting 2 more uncommon errors:
'unresolved external symbol _main referenced in function ___tmainCRTStartup'
and
'1 unresolved externals'

Yes, but this will never run. There is no main()...

Even if it does compile, this is not a functional program.

Yes, but this will never run. There is no main()...

Even if it does compile, this is not a functional program.

Ahhh, I see now. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.