I'd appreciate any help I could get with this calculation of the median.

This is a discussion on I'd appreciate any help I could get with this calculation of the median. within the C Programming forums, part of the General Programming Boards category; I'm working on a function that performs both an average output and the median of string of numbers. To perform ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    20

    I'd appreciate any help I could get with this calculation of the median.

    I'm working on a function that performs both an average output and the median of string of numbers. To perform the median calculation first I must be able to sort the functions properly, but I am having problems with it so far. This is what I have. Thanks everyone.

    Code:
    #include <stdio.h>
    
     temp bubble_sort(int start, int end) 
    int main (){
    
    	char buffer[512]; /* see previous program */
    int entries, current;
    double input, average=0;
    printf("Please enter number of entries.");
    fgets(buffer, sizeof(buffer), stdin);
    sscanf(buffer, "%d", &entries);
    for (current=0; current<entries; ++current){
    
    	
    	printf(" Please enter number %d: ", current+1);
    
    	
    	fgets(buffer, sizeof(buffer), stdin);
    
    	sscanf(buffer, "%lf", &input);
    
    	average= ((average*current) + input)/(current+1); /* sufficient for
    small sets */
    /* printf("Current average: %g\n", average); */ /* debug */
    getchar();
    }
    
    printf("Average = %g\n", average);
    
    return 0;
    getchar();
    }
     temp bubble_sort(int i, int n, int y, int x) {
    
    	 for (int i =0; i < n; i++)
    	 {
    		
    		int index_of_min = x;
    		for (int y=i; y<i; y++)
    		{
    			if( array [index_of_min] >array [y] )
    			{
    				index_of_min = y;
    			}
    		}
    		int temp array =array[x];
    		array [x] = array [index_of_min];
    		array [index_of_min] = temp;
    		
    	 getchar();
    	 }
    
     }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What is temp?

    Please post code that compiles.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And why aren't you passing the array into your function to be sorted? And why are you passing in a loop counter? And to sort, you need to do more than "find the smallest number and put it in position 0", as generally speaking there are a lot more positions than that in your array.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Prelude has a great tutorial that should get you going with sorting.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Couldn't you just do something simple without a function, like:

    Code:
    #include <stdio.h>
    
    int main ()
    {
         float average, number, count = -1.0, total = 0.0;
    
         do {
              printf ("Enter number. Enter 0 to compute.\n\n");
              scanf ("%f", &number);
              total += number;
              count ++;
              printf ("\n");
         } while (number != 0);
    
         average = total / count;
    
         printf ("The average of the numbers entered is: %.2f\n", average);
    
         return 0;
    }
    Sorry if this is more simplistic that what you are looking for, I'm fairly new to C programming, but I ran this and it finds the median just fine.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Unfortunately, average and median are actually two different things.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    @Sanglant
    1. We have a policy against handing out code here, since it keeps people from actually learning. Read the forum guidelines.
    2. Your code does not find the median, it finds the mean. Look up mean, median and mode on Wikipedia.
    3. You completely ignored the OPs actual question which was about sorting.
    4. Using functions is a really good idea, and makes sense when calculating multiple statistics, like mean and median.

    We love having more people around to help out and answer questions for people, but please put more thought into your replies.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,764
    Please read about bubble sort; this assumes you wish to use bubble sort to do your sorting

    Bubble sort - Wikipedia, the free encyclopedia

    Tim S.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by anduril462 View Post
    @Sanglant
    [list="1"][*]We have a policy against handing out code here, since it keeps people from actually learning. Read the forum guidelines.
    That's false (read the forum guidelines). There is no policy against posting code (it is a programming forum), there's a policy discouraging people who ask others to do their homework for them. Providing examples or throwing out concrete ideas does not "keep people from learning".

    Catching mistakes (such as confusing median and average) in posted code is always laudable tho.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You're right, it's not stated explicitly, and I could have phrased that better. But posting code for somebody else's homework without any effort to teach or explain violates the spirit of the homework policy in my opinion. Somebody coming around handing out answers to homework is the flip side to asking for people to do it for you. Asked for or not, the OP will likely read the post with the solution and lose the opportunity to think through a solution on their own. That problem solving aspect is a huge part of what is taught in programming classes and it's often harder to think up your own, unique solution when you're staring at somebody else's working solution. I'm not against posting working code, but I have seen more than one person join this forum and run around handing out homework solutions, and I was mostly interested in discouraging Sanglant from doing that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Median Help!
    By tidbit in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2010, 10:55 PM
  2. mean, mode median calculation..
    By naspek in forum C Programming
    Replies: 2
    Last Post: 09-10-2009, 10:15 AM
  3. Median
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2007, 05:00 PM
  4. Median
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2006, 03:07 AM
  5. median
    By frank in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2001, 04:32 PM

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