GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-May-2009, 23:32
ultrAslan ultrAslan is offline
New Member
 
Join Date: Feb 2009
Location: Oh, USA
Posts: 16
ultrAslan has a little shameless behaviour in the past

Power Test on Resolve C++


Hi All,

I have an assignment due to tomorrow but I can't figure it out. Could anyone help me about that..


Method

Implementation 1 - Adding Power as a global operation

Examine the file Power_Test.cpp, which is a simple test driver for global_procedure Power specified below. (Note that, 0^0 = 1.)
CPP / C++ / C Code:
global_procedure Power (
        alters Natural_Number_1_C& n,
        preserves Integer p
    );
/*!
    requires
        p >= 0
    ensures
        n = #n ^ (p)
!*/


Write a body for the Power operation which uses the "obvious" algorithm -- repeated multiplication by the original n. This body goes into Power_Test.cpp.


Test the above implementation, revising as necessary until you're convinced that this implementation of Power works. For implementation 1, the grader will look at the source code and output of Power_Test.
Implementation 2 - Making Power an extension

Examine the file Power_1_Test.cpp, which is the same simple test driver as above but this time for Power being the extension in abstract_instance class Natural_Power, which is in the file AI/Natural/Power.h in your lab07 directory.


Write a body for the Power operation in the file CI/Natural/Power_1.h in your lab07 directory. DO NOT write the body in the test driver. Use the "obvious" algorithm (repeated multiplication). This should be very straightforward, it is almost a cut and paste from implementation 1!


Test Natural_Power_1::Power using Power_1_Test.cpp as the test driver. Revise the operation body for Power until you're convinced that it works. The grader will look at the source code of Power_1.h and the output for Power_1_Test to grade this part.
Implementation 3 - Making Power more efficient

Copy Power_1_Test.cpp to Power_2_Test.cpp, and make the new test driver use a new implementation of Power simply by replacing ALL occurrences of Power_1, in Power_2_Test.cpp, with Power_2.


Provide a new body for the Power operation in the file CI/Natural/Power_2.h in your lab07 directory. This time use the more efficient and non-obvious "fast powering" method discussed in class.


Test Natural_Power_2::Power using Power_2_Test.cpp as the test driver and revising the operation body as necessary until you're convinced that this implementation of Power also works. The grader will look at the source code of Power_2.h and the output for Power_2_Test to grade implementation 3.

That's the assignment.
  #2  
Old 30-May-2009, 05:51
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Power Test on Resolve C++


I don't know if this is what you're looking for, but it may help you get started:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

void pow(long& number, const int exp)
{
    int result = number;
    if(exp > 0)
    {
	for(int i = 1; i < exp; i++)
	{
	    result *= number;
	}
	number = result;
    }
    else if(exp == 0)
    {
	number = 1;
    }
}

int main()
{
    cout << "Enter an integer: ";
    long number;
    cin >> number;
    cout << "Enter an exponent: ";
    int exp;
    cin >> exp;
    cout << number << " ^ " << exp << " = ";
    pow(number, exp);
    cout << number << endl;

    return 0;
}


Output:

Code:
bash-3.2$ ./pow Enter an integer: 2 Enter an exponent: 28 2 ^ 28 = 268435456 bash-3.2$ ./pow Enter an integer: 2 Enter an exponent: 29 2 ^ 29 = 536870912 bash-3.2$ ./pow Enter an integer: 2 Enter an exponent: 30 2 ^ 30 = 1073741824 bash-3.2$ ./pow bash-3.2$ ./pow Enter an integer: 2 Enter an exponent: 0 2 ^ 0 = 1 bash-3.2$ ./pow Enter an integer: 3 Enter an exponent: 2 3 ^ 2 = 9 bash-3.2$ ./pow Enter an integer: 3 Enter an exponent: 3 3 ^ 3 = 27 bash-3.2$ ./pow Enter an integer: 3 Enter an exponent: 4 3 ^ 4 = 81 bash-3.2$ ./pow Enter an integer: -12 Enter an exponent: 2 -12 ^ 2 = 144 bash-3.2$ ./pow Enter an integer: -12 Enter an exponent: 3 -12 ^ 3 = -1728 bash-3.2$ ./pow Enter an integer: 39 Enter an exponent: 4 39 ^ 4 = 2313441

It doesn't gracefully handle storage overflow, so there is still much more to do to make it "worthy."


MxB
  #3  
Old 30-May-2009, 08:40
ultrAslan ultrAslan is offline
New Member
 
Join Date: Feb 2009
Location: Oh, USA
Posts: 16
ultrAslan has a little shameless behaviour in the past

Re: Power Test on Resolve C++


What about recursion? Could you give me an example of that too?
  #4  
Old 30-May-2009, 23:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Power Test on Resolve C++


Quote:
Originally Posted by ultrAslan
What about recursion? Could you give me an example of that too?
CPP / C++ / C Code:
//
// An example of a recursive "fast power" function that uses
// squaring.
// 
// Reference: [url]http://www.cse.iitd.ernet.in/~suban/CSL102/lecture/node21.html[/url]
// Exercise 3.6
//
// davekw7x
//
#include <iostream>

using namespace std; 

double fast_power(double x, int n);

int main()
{
    double x, y;
    int n;

    cout << fixed;
    cout << "Enter x and n (n must be greater than or equal to zero): ";
    while ((cin >> x >> n) && (n >= 0)) {
        y = fast_power(x, n);
        cout << x << " to the power " << n << " = " << y << endl << endl;
        cout << "Enter x and n (n must be greater than or equal to zero): ";
    }
    cout << "Goodbye for now." << endl;
    return 0;
}

inline double square(double x)
{
    return x * x;
}

inline bool odd(int n)
{
    return ((n % 2) == 1);
}

//
// Fast power by squaring
//
double fast_power(double x, int n)
{
    if (n == 0) {
        return 1;
    }
    return (odd(n) ? x*square(fast_power(x, n / 2)) : square(fast_power(x, n / 2)));

}

A run:

Code:
Enter x and n (n must be greater than or equal to zero): 1.1 2 1.100000 to the power 2 = 1.210000 Enter x and n (n must be greater than or equal to zero): 2 31 2.000000 to the power 31 = 2147483648.000000 Enter x and n (n must be greater than or equal to zero): 1.99999999 31 2.000000 to the power 31 = 2147483315.140062 Enter x and n (n must be greater than or equal to zero): 0 0 0.000000 to the power 0 = 1.000000 Enter x and n (n must be greater than or equal to zero): 0 1 0.000000 to the power 1 = 0.000000 Enter x and n (n must be greater than or equal to zero): 1 0 1.000000 to the power 0 = 1.000000 Enter x and n (n must be greater than or equal to zero): Hasta la vista, Baby! Goodbye for now.


Regards,

Dave
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trinary operator gagless12 C++ Forum 5 06-Jun-2008 08:42
Quick test whether a number is a power of two mikhail C Programming Language 5 14-Feb-2008 08:38
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
CSS Layout question oihjk Web Design Forum 3 28-May-2003 12:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.