//  /*--------------------------------------------------------*\
//  |   Main Program: Simple tester for Natural Power
//  |*--------------------------------------------------------*|
//  |   Date:      Summer Quarter 2006
//  |   Author:    Bruce W. Weide (adapted from "anonymous")  
//  |   
//  |   Brief User's Manual:
//  |   Allows user either to exercise the Power operation
//  |   in an interactive menu-driven mode or in a batch
//  |   file mode (using input redirection).
//  |   
//  |   Modified:  8 April 2004
//  |   Modifier:  Wayne D. Heym
//  |   Modification:
//  |   Support for comments in test scripts
//  |   
//  |   Modified:  6 December 2006
//  |   Modifier:  Tim Long
//  |   Modification:
//  |   Request expected outgoing values for each test case
//  |   
//  \*--------------------------------------------------------*/

///-------------------------------------------------------------
/// Global Context ---------------------------------------------
///-------------------------------------------------------------

#include "RESOLVE_Foundation.h"
#include "CI/Natural/Number_1_C.h"

///-------------------------------------------------------------
/// Interface --------------------------------------------------
///-------------------------------------------------------------

global_procedure Power (
	alters Natural_Number_1_C& n,
	preserves Integer p
    );
    /*!
	requires
	    p >= 0
	ensures
	    n = #n ^ (p)
    !*/

//--------------------------------------------------------------

procedure_body Power (
	alters Natural_Number_1_C& n,
	preserves Integer p
    )
{
   //-------- for students to fill in --------
    
    
    if (p > 1)
    {
    	n.Multiply(n);
    	Power(n, p-1);
    }
    
    else if (p == 0)
    {
    	n.Clear();
    	n.Multiply_By_Radix(1);
    }
    
}

//--------------------------------------------------------------
//--------------------------------------------------------------

program_body main ()
{
    object Character_IStream input;
    object Character_OStream output;
    object Text response;
    object Boolean interactive_mode;
    object Integer p, p_exp_out;
    object Natural_Number_1_C n, n_exp_out;

    // Open input and output streams
    
    input.Open_External ("");
    output.Open_External ("");

    // Ask user about interactive mode
    
    output << "Run in interactive mode (y/n)? ";
    input >> response;
    interactive_mode = (response == "y");
    output << "\n";

    // Ask user for whether to test

    if (interactive_mode)
    {
	output << "Start testing (y/n)? ";
    }
    input >> response;

    // Execute interactive testing loop until finished

    while (response != "n")
    {
	if (response != "y")
	{
	    output << response << '\n';
	}
	else
	{
	    // Get values for n and p for this test case
	    
	    if (interactive_mode)
	    {
		output << "n = ";
	    }
	    input >> n;
	    
	    if (interactive_mode)
	    {
		output << "p = ";
	    }
	    input >> p;


	    // Get expected output for n and p for this test case
	
	    if (interactive_mode)
	    {
		output << "\nexpected output for n = ";
	    }
	    input >> n_exp_out;

	    if (interactive_mode)
	    {
		output << "expected output for p = ";
	    }
	    input >> p_exp_out;
	    
	    
	    // Report results of this test case
	    
	    output << "-------------------------------------------------\n"
		   << "                       |  n = " << n << '\n'
		   << "                       |  p = " << p << '\n'
		   << "-------------------------------------------------\n";
	    
	    Power (n, p);
	    
	    output << "Power (n, p);          |\n"
		   << "-------------------------------------------------\n"
		   << "                       |  n = " << n << '\n'
		   << "                       |  p = " << p << '\n'
		   << "-------------------------------------------------\n";

	    if (p != p_exp_out)
	    {
		output << "\nERROR DETECTED:\n"
		     << "    expected p = " 
		     << p_exp_out << '\n'
		     << "    observed p = "
		     << p << "\nEXECUTION TERMINATING\n";
		return 0;
	    }

	    if (n.Compare (n_exp_out) != 0)
	    {
		output << "ERROR DETECTED:\n"
		     << "    expected n = " 
		     << n_exp_out << '\n'
		     << "    observed n = "
		     << n <<  "\nEXECUTION TERMINATING\n";
		return 0;
	    }

	}
	// Determine whether to continue testing
	
	if (interactive_mode)
	{
	    output << "\nContinue testing (y/n)? ";
	}
	input >> response;
    }

    // Close input and output streams
    
    input.Close_External ();
    output.Close_External ();
}

I am having problem at

if (p > 1)
    {
    	n.Multiply(n);
    	Power(n, p-1);
    }

this multiplication. because for every value I get zero. Any idea??

Recommended Answers

All 10 Replies

Have no idea how to help you because I don't know what Multiply() and Power() do.

And that is one of the weirdest programs I have seen in a long long time. program_body instead of int ??? Only a university professor would dream of writing such confusing and unconventional crap.

definitely it's the crappiest program language...

definitely it's the crappiest program language...

c++ is not a crappy programming language -- the program you have to "enhance" is crappy. IMHO your teacher is an idiot. But that's neigther her nor there.

Your immediate problem is that you posted calls to functions that are non-standard in c++ language so you need to post those functions too. Otherwise no one can tell you the answer to your question.

c++ is not a crappy programming language -- the program you have to "enhance" is crappy. IMHO your teacher is an idiot. But that's neigther her nor there.

Your immediate problem is that you posted calls to functions that are non-standard in c++ language so you need to post those functions too. Otherwise no one can tell you the answer to your question.

I have no f* idea what they are they just showed them and want me to do it..

>definitely it's the crappiest program language...
On what arguments do you base this?

C++ is a more advanced language and it will allow some things other programming languages won't ever let you do, if you write a program in C++, and you are a good programmer, this will reflect in your work, but when you're a bad programmer, this will also reflect in your work :P

So, IMO this code is just crap ! (and now you read the above info again :P)

>I have no f* idea what they are they just showed them and want me to do it..
I've also no f* idea of your problem because you haven't posted the whole code as already mentioned by Ancient Dragon :)

If you don't know what those functions are then we won't be able to help you. Multiply() is a method of class Natural_Number_1_C. So you have to read the header file Number_1_C.h to find out what they do.

I have found this exotic Number_1_C... panopticon at Ohio state university site. It looks like an attempt to convert the C++ language to an incomprehensible slang for dedicated persons. As far as I could judge it's not so easy to understand lots of special conventions, mysterious macros and other tricks embeedded in this stuff...

How sad! :(

commented: Yes, this is unadulterated brain-rot which will cripple the students for life. +32

Probably too late... but when you make the call n.Multiply(n); you are violating the repeated parameter rule by using n twice in this operation. If p > 0, you should make a copy of n then multiply n by n_copy.

n.Copy_To(n_copy);
n.Multiply(n_copy);
Power(n, p-1);

It is officially clear that the code is best understandable by its author.
About C++:
It is (nearly) a perfect language with majority of imperfect programmers.
Most of high school teach C++ with giving students proper programming background. These student, mis-interpret themselves as C++ programmers and write crap code. And thus they spoil the name of language.
I still feel sorry "Why the hell they decided to teach them C++ first??"

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.