recursion code help

This is a discussion on recursion code help within the C Programming forums, part of the General Programming Boards category; hello, i have tried compiling and running this code but how do i input a numeric value like 30 or ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    29

    recursion code help

    hello,
    i have tried compiling and running this code but how do i input a numeric value like 30 or 35? this is recursive fibonacci and all i need to do is check the output after program has finished execution. any suggestions would be of tremendous help. i just need to know how to enter a value.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    __int64 fibRecursive(int n)
    {
    	if (n <= 0)
    		return 0;
    	if (n == 1)
    		return 1;
    	
    	return fibRecursive(n - 1) + fibRecursive(n - 2);
    }
    
    __int64 fibIteration(int n)
    {
    	__int64 ret =  0;
    	int i,j, iter;
    	i=0;
    	j=1;
    	if( n < 2)
    		return n;
    
    	for(iter=2; iter<=n; iter++)
    	{
    		ret=i+j;
    		i=j;
    		j=ret;
    	}
    	return ret;
    }
    
    int main(int argc, char* argv[])
    {
    	__int64 val;
    	int n;
    	time_t t1, t2;
    	if(argc != 3)
    	{
    		printf("Usage %s iteration or recursive number\n", argv[0]);
    		_exit(1);
    	}
    	n = atoi(argv[2]);
    	t1 = time(NULL);
    	if(strcmp(argv[1], "iteration") == 0)
    	{
    		val = fibIteration(n);
    	}
    	else if(strcmp(argv[1], "recursive") == 0)
    	{
    		val = fibRecursive(n);
    	}
    	t2= time(NULL);
    	printf("n=%d, Val = %I64u, time spent is =%d\n", n, val, t2 - t1);
    	return 0;
    }
    i want to enter a number, let the program run because all i require is to check how much time it took the program to complete the calculation.
    Last edited by ilikeapplepie; 03-07-2011 at 04:01 PM. Reason: extra details

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all, #include <stdint.h> and use int64_t That double underscore stuff is not a good idea in a program.

    Second, I find it a little hard to believe you wrote that code (pretty advanced stuff!) without realizing where the input value comes from... doing a little scoop and poop, maybe?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    You need to pass in the arguments on the command line. See if this helps: Cprogramming.com FAQ > Command line input.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    29

    this isn't my code

    i have very basic c language skills.
    my only task is to let the code finish executing and note the values because professor wants us to see the difference in time between recursion and iteration calculations.
    this isn't hw, just practice code and it was suggested to me that i shouldn't make changes to it prior calculation.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ilikeapplepie View Post
    i have very basic c language skills.
    my only task is to let the code finish executing and note the values because professor wants us to see the difference in time between recursion and iteration calculations.
    this isn't hw, just practice code and it was suggested to me that i shouldn't make changes to it prior calculation.
    Ok... we believe you ...

    If you actually ran the program you would see the prompt it gives...

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    how do i do command line input on visual c++ 2010? all the code i have ever written used the prompt window after compilation. thanks.

    it gives


    Usage C: the path filename iteration or recursive number
    press any key to continue
    Last edited by ilikeapplepie; 03-07-2011 at 04:11 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ilikeapplepie View Post
    how do i do command line input on visual c++ 2010? all the code i have ever written used the prompt window after compilation. thanks.

    it gives


    Usage C: the path filename iteration or recursive number
    press any key to continue
    Press and hold the key with the windows symbol on it.
    Now type the letter R
    Let go of the key with the windows symbol on it
    In the Run dialog type CMD
    Click OK.

    Now you can run your program ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 03:28 PM
  2. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21