Thread: Help understanding: using recursion and dynamic data sturcture stack

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    Help : using recursion and calculating time of function

    You are going to write a C program and use recursion as well as a dynamic data structure stack in this question.
    Here is my homework assigment.

    Factorial of a number is calculated as
    n ! = n x (n‐1) x (n‐2) x (n‐3) x ... x 1

    First, write a C program to implement the factorial operation using recursion. Name this first file as yourFirstName‐LastName‐recursive.c. Run your first program for n=1, 2, 3, ..., 12 and record runtime (in msec) for each execution. For calculating runtime, please make a search and add a runtime calculator code to your C program.

    Then, write another C program to calculate the factorial of a given number using stack this time. Name this first file as yourFirstName‐LastName‐with‐stack.c. Run your second program for n=1, 2, 3, ..., 12 nd record runtime (in msec) for each execution. For calculating runtime, please use the same runtime alculator code you have used above and add it to your C code.

    Help me understand what am i suppose to do please. I just started stacks but have no idea how I can calculate the run time, is it already in c?

    Thanks for the help as usual.
    Last edited by newbc; 04-29-2011 at 08:02 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    should I do something like this to measure time? the thing is it measures it in seconds they want int m seconds.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
    	time_t time1,time2;
    	char get_input [256];
    	double dif_sec;
    
    	time (&time1);
    	printf ("Please enter the name of your pet: ");
    	gets (get_input);
    
    	time (&time2);
    	dif_sec = difftime (time2,time1);
    	printf ("It took you %.2lf seconds to type the name.\n", dif_sec );
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    you can use sys/timeb.h's timeb struct. If sys/timeb.h is unavailable, you can use Windows.h's SYSTEMTIME. Just replace "struct timeb" with "SYSTEMTIME" and "ftime" with "GetSystemTime". Then replace ".time" with ".wMinute" and ".millitm" with ".wMilliseconds"

    Code:
    #include <stdio.h>
    #include <sys/timeb.h>
    
    int main() {
    	struct timeb time_start, time_current;
    	ftime(&time_start);
    
    	SomeFunction();
    
    	ftime(&time_current);
    	printf("This program took %d milliseconds to run", time_current.millitm - time_start.millitm);
    
    	return 0;
    }
    But watch out, if you're at, lets say 1:30:59.980 (1:30 PM, 59 seconds, and 980 milliseconds) and your execution takes 30 miliseconds, the difference would be a negative (in this case, -950).

    To counter this, an easy yet lazy way is to add 1000 for each second which passes.
    Example:

    Code:
    /* calculates the difference in seconds */
    
    int seconds_difference = time_current.time - time_start.time;
    
    /* if 0 seconds pass between time_start and time_current, there will be no change to time_current.millitm, otherwise 1000 gets added per second to counter time difference */
    
    int milliseconds_difference = (time_current.millitm + (1000 * seconds_difference)) - time_start.millitm;
    Therefore, if your program took 15 seconds and 800 milliseconds from 1:30:59.980, the math would be:
    Code:
    (15 * 1000 + 780) - 980
    15780 - 980
    14800
    Which is the correct amount of time (in milliseconds) from the last time to now.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On windows you can do this...
    Code:
    #include <windows.h>
    
    int main (void)
      { unsigned int tc;
    
         // get start timestamp
         tc = GetTickCount();
    
          // code to be timed
    
          // get time taken
          tc = GetTickCount() - tc;
    
          printf("This took %u milliseconds",tc);
    
         return 0; }




    [/CODE]

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    should I do something like this to measure time? the thing is it measures it in seconds they want int m seconds.
    How about dividing by 1000?
    This will only work if diff is > 1 though.
    I think there's no standard way.
    for POSIX, you can use gettimeofday.
    For windows, refer to CT post.
    Last edited by Bayint Naung; 04-29-2011 at 12:21 AM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    ok here is what I have so far and is not working I get zero for every factorial even 12! which I think should take some time. I have it set to zero since I have not figured out a way to time each factorial when going trough the loop.

    Code:
    #include <stdio.h>
    #include <sys/timeb.h>
    
    long factorial(long number);
    
    
    int main (void){
    
    
    
      struct timeb time_start, time_current;
      int milliseconds_difference;
    
      int i;
    
    
    	for(i=12;i<=12;i++){
    
    	
    ftime(&time_start);
    	 factorial(i);
    
    ftime(&time_current);
    
    
    int milliseconds_difference = (time_current.millitm + (1000 * seconds_difference)) - time_start.millitm;
    	 
    
    	 
    		printf("%2d = %ld\n", i, factorial(i));
    		
    	printf("It took %.d m. seconds to calculate %2d!\n", milliseconds_difference, i );
    
    	}
    
    
    
    
    return 0;
    
    }
    
    long factorial (long number)
    {
    
     if (number <=1){
    	return 1;
     }
    
     else {
    
    	return ( number * factorial( number -1));
    
     }
    
    }
    Last edited by newbc; 04-29-2011 at 01:54 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Here is another failed try.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    long factorial(long number);
    double diffclock(clock_t clock1,clock_t clock2);
    
    
    int main (void){
    
    
      
    clock_t clock3, clock4;
        
         
    
    
    
    
      
      
    
      int i;
    clock3 = clock();
    
    
    	for(i=0;i<=12;i++){
    
    	 
    
    	 factorial(i);
    
    	 
    
    
    
    	 
    
    	 
    		printf("%2d = %ld\n", i, factorial(i));
    		
    	printf("It took  m.seconds to calculate %2d!\n", i );
    
    	}
    
    
    
    clock4 = clock() ;
    
    printf("%f m.seconds\n", diffclock(clock3,clock4) );
    
    return 0;
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    long factorial (long number)
    {
    
     if (number <=1){
    	return 1;
     }
    
     else {
    
    	return ( number * factorial( number -1));
    
     }
    }
    
    
    double diffclock(clock_t clock1,clock_t clock2)
    Last edited by newbc; 04-29-2011 at 10:19 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    The problem here is your clock is simply far too slow to measure anything as fast as it takes to run a simple function. You're basically trying to time a bullet from a gun using a sundial.

    Unless you do something, it always looks like zero.

    Try this
    Code:
    for(i=0;i<=12;i++){
      int result;
      clock1 = clock();
      for ( j = 0 ; j < 1000000 ; j++ ) {
        result = factorial(i);
      }
      clock2 = clock();
      // clock2 - clock1 is the time for 1M calls
      // divide this by 1M to get the time for 1 call.
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    ok I tried with the 1m loop, but now i'm getting negative nums.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    long factorial(long number);
    double diffclock(clock_t clock1,clock_t clock2);
    
    
    int main (void){
    
    
      
    clock_t clock3, clock4;
        
         
    
    
    
    
      
      
    
      int i;
      int j;
    
    
    	for(i=0;i<=12;i++){
    
    	 clock3 = clock();
    
    		 for ( j = 0 ; j < 1000000 ; j++ ) {
       
    	 	 factorial(i);
    
    	 
    		 }
    		 clock4 = clock() ;
    
    	 
    
    	 
    		printf("%2d = %ld\n", i, factorial(i));
    		
    	printf("It took  %f m.seconds to calculate %2d!\n" , diffclock(clock3,clock4), i );
    
    	}
    
    
    
    
    
    
    
    return 0;
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    long factorial (long number)
    {
    
     if (number <=1){
    	return 1;
     }
    
     else {
    
    	return ( number * factorial( number -1));
    
     }
    }
    Last edited by newbc; 04-29-2011 at 10:19 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    That's because you're subtracting the last time (which is bigger) from the first time.

    You know, better variable names like 'before' and 'after' would make it obvious.
    As opposed to clock1234

    Also, indent your code properly. It's only 20 lines, but it's still a mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-30-2002, 09:03 PM
  2. Stack or Heap Private Data...?
    By GrNxxDaY in forum C++ Programming
    Replies: 4
    Last Post: 08-09-2002, 05:13 AM
  3. DATA and STACK Segments
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-20-2002, 04:11 PM
  4. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM
  5. Accessing data in the program stack
    By dajur in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 10:15 PM