Thread: C program help

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    24

    C program help

    Hello guys, I still not good at recursion, now I had to do this for homework.
    Example: array [1,2,-1,2] , the sequence's average order 1 is 1, order 2 is (1+2)/2=1.5, order 3 is (1+2-1)/3=2/3, order 4 is (1+2-1+2)/4=1
    Question: build a function with recursion, the function should be like this double max_find(int a[], int n, double* avg), a = array, n = array_length, avg = I don't know what the hell is that...(the reason why I am asking for solution)

    the function returns the max average of the sequence, in the example above it should return 1.5 (cuz it's maximum)

    my try was this:
    Code:
    #include <stdio.h>
    
    double max_find(int a[], int n, double* avg)
    {
        double we;
        if(n < 1)
            return a[0];
        
        avg = a[0];
        we = (avg + max_find(a+1, n-1, avg))/2;
        if(avg > we)
            return avg;
        else
            return we;
    }
    
    int main(void)
    {
        int arr[4] = {1,2,-1,2};
        double result;
    
        result = max_find(a, 4, avg);
        printf("%lf\n", result);
    
        return 0;
    }

    - u can't use loop (for/while)
    - u can't use global variables
    - u can't change the values inside the array, even for abit of time


    I don't know what I did, cuz of that (double* avg), but any1 help xD
    Thank You
    Last edited by ConanCoder; 03-29-2014 at 01:56 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This site has a homework policy, here. It amounts to "do your own homework".

    Finding code that someone else has written, and begging in a forum for an explanation of how it works in case your teacher asks for an explanation, does not qualify as doing your own homework.

    Not that it really matters in this case. That code is not valid C, so will not even compile, let alone produce the results required of your homework.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    24
    Quote Originally Posted by grumpy View Post
    This site has a homework policy, here. It amounts to "do your own homework".

    Finding code that someone else has written, and begging in a forum for an explanation of how it works in case your teacher asks for an explanation, does not qualify as doing your own homework.

    Not that it really matters in this case. That code is not valid C, so will not even compile, let alone produce the results required of your homework.
    very well,
    1st I didn't know that policy, so now I know.
    2nd I didn't take that code from anyone I coded it.
    3rd that's god to know it's an error code (won't compile)
    4th I just asked for help, in my homework question, no big deal tho.
    5th won't ever ask about homeworks, so chill out ROFL !

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    My guess of the main function.
    Note: The use of literal "4" in the code is NOT correct thing to do.

    I did the smallest code change that looked correct; I have NOT compiled the code.

    Tim S.


    Code:
    int main(void)
    {
        int arr[4] = {1,2,-1,2};
        double result;
        double avg = arr[0];
     
        result = max_find(a, 4, &avg);
        printf("%lf\n", result);
     
        return 0;
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    24
    Thanks for the help mate, what I found hard was only that ((double* avg)) thing, I was like "what the heck should I do with it" Lol

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ConanCoder View Post
    very well,
    1st I didn't know that policy, so now I know.
    2nd I didn't take that code from anyone I coded it.
    3rd that's god to know it's an error code (won't compile)
    4th I just asked for help, in my homework question, no big deal tho.
    5th won't ever ask about homeworks, so chill out ROFL !
    6th ... when I replied to this thread, it had a subject line asking for help understanding code you had found. The subject line has subsequently changed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    24
    Quote Originally Posted by grumpy View Post
    6th ... when I replied to this thread, it had a subject line asking for help understanding code you had found. The subject line has subsequently changed.
    7th... I'm sorry to tell you u r wrong mate .. and I try my best be4 posting here... anyway I solved my problem alone so that's okay <3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM