• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

printing the number of function calls.




/*----------------------------------------------------------

First of all the following code sucks (as i am learning)

The code does performance analysis of two Fibonnaci algorithms

By printing the number of function calls.

I have defined call_count to count the number of inputs and I want to write the out put file as

       Output File for Algo #1
___________________________________________

First Fibbonnaci m Fibonnaci Numbers -  No. Of Calls to F

1. m = 1      - 1
2. m = 2      - 2
3. m = 3      - 5
4. m = 4      - 8
5. m = 5      - 11

etc.  

I can write 1 line of the table at a time i.e i should run the program by changing the values of m in

But if I put a for loop and try to generate in steps (i tried doing that but , then commented it)

1st 10 numbers then
1st 11 numbers etc. The call_out gives cumulative calls.

Can some body suggest me something ?? Hope I am clear enough

Now I am using MS Visual C++

------------------------------------------------ */


#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>


using namespace std;



 int call_count()

{

     //static int num_of_calls = k;   // the initial value of the static variable
                                    // only effective the first call.
     
     
      static int num_of_calls = 0;   // the initial value of the static variable
     
      //cout<<"-----"<<num_of_calls+1<<endl;
      return ++num_of_calls;         // increment the number of calls and return it.
}


/*
int F( int i)

{


cout<<"-----"<<call_count()<<endl;

//cout<<"calling"<<endl;

     if (i<1) return 0;
     if(i==1) return 1;
         
     return F (i-1)+ F(i-2);


     

}

*/


#define MAX 100
unsigned int F(unsigned int n)
{


/*
  ofstream fout;
   
// "FOUT" as in "FILE output"
   fout.open("c:/fibonacci.txt");
 

 fout << endl <<"total="<<  call_count() << endl ;

 fout.close();
*/



//cout<<"-----"<<call_count()<<endl;

 
 static unsigned  int knownF[MAX] = {0};
if (n == 0 || n == 1)
knownF[n] = n;
else if (knownF[n] == 0)
knownF[n] = F(n-1) + F(n-2);
return knownF[n];

}


int main()

{


     
/*for (int m=0;m<5;m++)
{
     cout<<endl<<"Now printing First"<<m<<endl;
*/


     unsigned long int fib_no = 0, index = 0;

     while (index < 30)  
          {  
             fib_no = F(index);
             cout<<endl<<index+1 <<": "<<fib_no<<endl;
                          index++;              
          }




return 0;
     
}
 


0
anshuma
Asked:
anshuma
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
Alf,

If you happen to visit this page, then please also check:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20564603.html

Mayank.
0
 
SalteCommented:
Mayank,

It certainly looks like we can pick up the h word again.

anshuma,

Your problem is easy enough, you're just going about to solve it the wrong way. I will give you a hint:

This isn't C, this is C++.

The problem is that you have a function static counter and function static variables have one annoying features, it's only the function in which they're defined that can access them.

Now this doesn't have to be a big problem, in fact you can make a function that takes arguments and depending on the argument can access that static variable in many ways, you can have one command to read it, one to write it and one to increment it etc. However, it is clumsy and more trouble than it is worth.

Now, since you are posting this to a C++ forum the obvious solution is to think C++ instead of C and then we have other static variables which doesn't suffer the limitations of the function static variables. Enough hint.

Rethink your program.

Alf
0
 
substandCommented:
you either need to have two call_count() functions, or just place the code for your call_count function in each fibionacci function.

currently, since you only have 1 call_count function, if the first fib func takes 3 calls and the second takes 4 calls, it will print "3" for the first function, and "7" for the second function...

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
anshumaEngineeringAuthor Commented:
Dear Mr. mayank and salte,

Yes I got a homework problem and it was to do a profile analysis
of two algos for first 15 fibonnaci numbers by counting the number
of calls.

I had done my homework by manually changing the value of m to
1,2,3,4,5 etc and got the answer. Then I could have run the
program again by commenting aonther function and uncommenting
other. This way I could have done my program by doing it 30 times.
And yet i could have done my bit of programming for the grade.

Then for learning i increased the number from 15 to 25 to 50 and ,similarly for learning, I was trying to put a loop and generate ,the call numbers in one slot and then writing them in a file.

My homework was already completed by me in an inelegant manner, but still having done that my instructor would have awarded me full points.

You guys feel the hwork despicable. But you forget one thing, that everybody knows cheating is harm to self. If i get good grades and yet cannot prove myself in exam. I will get a zero.  I remember one expert saying that in these toughs times only pros are hired. And to become a pro one has to work hard and not get his/her homework done.

Please don't think that everybody young here is just getting the homework done. There can be genuine ones who want to learn something.

The onlything unethical I did was I used two-two ids as I was running out of points. For that matter the community support can penalize me and block my ids. But I am a student and can't afford to buy points and nor do i have a credit card.

Thank you all.
0
 
SalteCommented:
Which two versions of fibonacci did you try?

As far as I know there are three ways to compute fibonacci:

1. Using the recursion rule directly. For large values of n this becomes prohibitively slow and lots of calls.

2. Solve the equation and compute the value directly using exponentials. This is fast way but is kinda like cheating I guess. It is also notoriously inaccurate for computers since it involves computing real non-fractional numbers and adding themtogether and the sum is supposed to be a natural number but in the world of computers and accuracy that might not be happening. Of course, this can be helped by using higher accuracy but that too has its limits.

3. Use the 'smart' recursion. Of fear of spoiling I won't really go into details, just saying that it involves a std::map<int,int> or some similar data structure and the idea is to avoid computing fib(n) for values that you have already computed.

Now the second doesn't use recursion at all and so the call count isn't really interesting for it, so I suspect the two methods you compare are 1 and 3, is that so? Or did you use a 4th method which I didn't put in my list?

Alf
0
 
anshumaEngineeringAuthor Commented:
/*

I think I have understood the problem. The second function does nothing but uses dynamic programming. so if put a for loop like this

for(int i=0;i<8;i++)
{
call_fib(i);
cout<<endl<<endl<<"**************************"<<endl<<endl;
}

it uses the numbers in memory and increases them in one step.

But I want the output in a way that it deletes everything in the memory and redo the program.

so if I manually call call_fib(7);
Then the number of steps is 17

Then if I manually call call_fib(6); after exiting the program.

Then the number of steps is 14

But if I put a for loop;
it just increments the number of steps by 1;
Because this way I am not freeing the memory.

I was previously focusing on the counter function and wasted lot of hours. Now I think its not the counter but the call_fib() function.

I tried deleting delete [] knownF;

But could not do anything . Moreover I think that way i am trying to delete something on stack.

----------------------------------------------------------

By the way Mr.Salte I have done my homework already.
But I am trying this just to learn the concepts, however I have wasted lot of hours doing this and that's why I am asking for help.

If you don't want to answer further, then please put a comment to stop and I will award points to you and say good bye.

*/



#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>


using namespace std;

int globe=0;

int call_count()

{

  //static int num_of_calls = globe;   // the initial value of the static variable
  //cout<<"globe: "<<globe;
//  return ++num_of_calls;         // increment the number of calls and return it.

//cout<<"The value of globe is " <<globe<<endl;
return ++globe;

}


int F1( int i)

{

//cout<<"calling"<<endl;

     if (i<1) return 0;
     if(i==1) return 1;

    cout<<"-----"<<call_count()<<endl;

     return F1 (i-1)+ F1(i-2);
     

}


#define MAX 100
unsigned int F(unsigned int n)
{


/*
  ofstream fout;
   
// "FOUT" as in "FILE output"
   fout.open("c:/fibonacci.txt");
 

 fout << endl <<"total="<<  call_count() << endl ;

 fout.close();
*/



cout<<"-----"<<call_count()<<endl;

 
static unsigned  int knownF[MAX] = {0};

if (n == 0 || n == 1)

knownF[n] = n;

else if (knownF[n] == 0)

knownF[n] = F(n-1) + F(n-2);

return knownF[n];

//delete [] knownF;

}



void call_fib(int m)

{



     unsigned long int fib_no = 0, index = 0;

     while (index <m)  
          {  
             
             fib_no = F(index);
             cout<<endl<<index+1 <<": "<<fib_no<<endl;
                          index++;              
          }


}  


int main()

{

     call_fib(7);
    globe=0;

cout<<endl<<"-----------------------"<<endl;

    call_fib(6);
    globe=0;



/*
for(int i=0;i<8;i++)
{
     //globe=1;
call_fib(i);
cout<<endl<<endl<<"**************************"<<endl<<endl;
}

*/

return 0;
     
}
 


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now