split array

This is a discussion on split array within the C Programming forums, part of the General Programming Boards category; hi! i want to splt the array into sections of 2 values, recursively. the objective its to do the average. ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    split array

    hi! i want to splt the array into sections of 2 values, recursively. the objective its to do the average.
    i have this input and output, that its correct:
    > media_recursiva a 1 a 2 a 4 a 5 a 2 a 3 a 5 a 6
    1.000
    1.500
    3.000
    3.500

    ("a 1" means add the value 1)

    but mine its wrong, it gives to me this:
    1.000
    1.000
    2.500
    3.000

    Code:
    float media(int arr[], int count)
    {
        while(count != 0){
            if(count==1)
                return arr[0];
            else if(count ==2){
                return ((arr[0] + arr[1])/2);
            }
            else{
                int *fHalf = arr;//first half of array
                int *sHalf = arr + (count/2);//second half
                return (media(fHalf, (count/2)) + media(sHalf, (count/2)))/2;
            }
        }        
    }
    thanks
    Last edited by rec0o; 03-16-2013 at 04:01 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why the split of first half and second half? Problem is, it makes your code far less intuitive to modify or debug, and I don't see any gain from doing it - just the opposite.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by rec0o View Post
    Code:
    float media(int arr[], int count)
    {
        while(count != 0){
            if(count==1)
                return arr[0];
            else if(count ==2){
                return ((arr[0] + arr[1])/2);
            }
            else{
                int *fHalf = arr;//first half of array
                int *sHalf = arr + (count/2);//second half
                return (media(fHalf, (count/2)) + media(sHalf, (count/2)))/2;
            }
        }        
    }
    thanks
    The while is wrong.
    If count == 1 or count == 2 that is your terminating condition, and you return a value. Otherwise you call media() twice with the first half and second half of the array. You don't loop within the function.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    i removed the while but it stills wrong. maybe its because im giving a int array[] as parameter and its rounding down

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rec0o View Post
    i removed the while but it stills wrong. maybe its because im giving a int array[] as parameter and its rounding down
    Clearly time for a double - or float. you'll never get 3.5 from an int!

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    hi! i want to splt the array into sections of 2 values, recursively. the objective its to do the average.
    You'd end up calculating the following

    (1 + (2 + (3 + (4 + 5)))) / 2 / 2 / 2 / 2

    Is there really no concern that this is entirely wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Split a Byte Array
    By TGM76 in forum C Programming
    Replies: 4
    Last Post: 09-03-2011, 11:08 PM
  2. Split a textfile in an array
    By peterderijp in forum C Programming
    Replies: 15
    Last Post: 10-11-2010, 01:59 PM
  3. Howto? split and use a char array
    By djnicovski in forum C++ Programming
    Replies: 18
    Last Post: 07-23-2010, 11:18 PM
  4. Split up an Array
    By shanem in forum C Programming
    Replies: 6
    Last Post: 01-22-2009, 11:05 AM
  5. Sorting an array alphabetically - split
    By wurzull in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 11:17 PM

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