Yes, noOfAdditions is a counter for counting the number of additions that were made solely for the purpose of calculating f(n). You can see where noOfAdditions is declared in Post #12.
Printable View
Yes, noOfAdditions is a counter for counting the number of additions that were made solely for the purpose of calculating f(n). You can see where noOfAdditions is declared in Post #12.
Hmmm i see, Well lets say i wanted to call noOfAdditions. Like to see what the number is, since it's within the function and it's not returning. is there some way to 'easily' display the counter?(since each function has the same counter variable of "noofadditions"
and thx for all your help man
btw whats the the "unsigned" usage......we've never gone over that in class.....does that just mean it doesn't need a prototype or something?
noOfAdditions is a global variable. So, it can be accessed from any function, including main(). An "unsigned" variable means that the variable cannot hold a negative (no sign allowed) value. There cannot be a negative number of additions... so it makes sense to declare the variable as an unsigned integer. The keyword unsigned has its roots in C. Read this for more info.
Personally, I think of memoization as a simple addition to a recursive program which should be used only when it isn't convenient to refactor to a dynamic programming approach.
The reason for doing either memoization or dynamic programming is to keep computation time from blowing up exponentially due to a large number of repeated calculations. Once you understand that the reason you store computation results is so that if you ever get precisely the same inputs you'll have the output immediately, the concept should become clear.
true, but i have to do all three for the assignment.
last questions guys i swear: for the assignment I have to measure like the "time" it took to do each of these equations. is there like a good time /stopwatch program to implement in these functions, to see how long it took to run each.
he has a stopwatch program on his unix server we can use, but I figured if I could find something within c++ or something easier I could use that, i also tried to use his "stopwatch" code, but it says it cant find <sys/times> nor the other include either
EDIT: nm i used a class stopwatch thing looks like this
that should work ok for my code I guess?Code:#include <ctime>
class stopwatch
{
public:
stopwatch() : start(std::clock()){} //start counting time
~stopwatch();
private:
std::clock_t start;
};
#include <iostream>
using namespace std;
stopwatch::~stopwatch()
{
clock_t total = clock()-start; //get elapsed time
cout<<"total of ticks for this activity: "<<total<<endl;
cout<<"in seconds: "<<double(total)/CLOCKS_PER_SEC<<endl;
}
the times will be slightly different each time im assuming?
You are supplied the stopwatch class in the course materials I posted a link to earlier.
ya I ended up using the clock program above ^^ because for some reason my computer wouldn't load sys/times.
it ended up working out in the end tho.
thx guys!