recursive problem

This is a discussion on recursive problem within the C++ Programming forums, part of the General Programming Boards category; please i want to write this program nonrecursivly and i don't know how can you help me? void f(int[], int, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    recursive problem

    please i want to write this program nonrecursivly and i don't know how
    can you help me?

    void f(int[], int, int);
    int main()
    { int a[]={2, 4, 6};
    f(a, 0 , 2);
    return 0; }

    void f(int array[], int low, int high)
    { printf("%d ", array[low]);
    if(low == high)
    return;
    else f(array, low + 1, high );
    }

    i know the output will be 2 4 6
    please set free our POWs

  2. #2
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    First of all why?

    Anwser:

    Maybe use a while loop

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    why?

    i want to know how
    because i've got amidterm tomorrow

    thanx for your help

    i will see how to use it with while
    but should i change what is given to the function or i keep them like they are i ment a, 0 and 2

    and what about the if else statment should i cancel it
    and put this insted

    printf("%d", array[low]);
    while(low!=high){
    for(int i=0; i<=2;i++){
    low++;
    printf("%d", array[low]);}
    }
    Last edited by kuwait; 12-14-2002 at 06:57 AM.
    please set free our POWs

  4. #4
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148

    Re: recursive problem

    Originally posted by kuwait
    please i want to write this program nonrecursivly and i don't know how
    can you help me?

    void f(int[], int, int);
    int main()
    { int a[]={2, 4, 6};
    f(a, 0 , 2);
    return 0; }

    void f(int array[], int low, int high)
    { printf("%d ", array[low]);
    if(low == high)
    return;
    else f(array, low + 1, high );
    }

    i know the output will be 2 4 6
    You said if low == high return so if you put it in a while loop k\like you did it will never end if low==high!
    so keep the if statement and combine it with your second thing to make a comprimise

    useing the for statemt will clean your code and make it easier to understand so use it
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    thanx i've got a way to do it

    while(low!=high+1)
    please set free our POWs

  6. #6
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    Sounds good. If you need any toher help feel free to ask
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Problem.
    By penance in forum C Programming
    Replies: 4
    Last Post: 07-07-2005, 11:16 AM
  2. recursive function problem
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 10-25-2002, 07:02 AM
  3. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM
  4. Problem with a recursive function
    By fofone in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 08:21 AM
  5. Recursive problem...
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2002, 04:20 PM

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