Help! Check my code over.

This is a discussion on Help! Check my code over. within the C Programming forums, part of the General Programming Boards category; I have written a successful piece of code that takes a random assortment of numbers and puts them in numerical ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    Help! Check my code over.

    I have written a successful piece of code that takes a random assortment of numbers and puts them in numerical order and then finds the mean and median of the list. My professor wants us to make it as fast as possible and i think i have a few things that could be cleaned up, and i still don't fully understand C so any help would be grateful.

    Edit: Basically all i need is some help finding where i can cut down on processes to make the run time faster.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    void GetValues(int x[], int *pN);
    void SortValues(int x[], int n);
    double GetMean(int x[], int n);
    double GetMedian(int x[], int n);
    
    void main()
    {
    int a[100],b[100],pN,i;
    double mean,median;
    do
    {
    scanf("");
    GetValues(a,&pN);	 // get values and put them into "a" array
    if(pN!=0)
    {
    for(i=0;i<pN;i++)
    {
    	b[i]=a[i];	  // is there a better way of doing this?? i feel like it adds alot of unneccasry steps
    }
    SortValues(b,pN);	 //sort values using the "b" array
    mean=GetMean(b,pN);	  //get mean of "b" array
    median=GetMedian(b,pN);   //get median
    printf("\n     Original      Sorted\n");	  //print everything	
    for(i=0;i<pN;i++)
    {
    printf("%13d %11d\n",a[i],b[i]);
    }
    printf("\nMean:%11.2lf\n",mean);
    printf("Median:%9.2lf\n\n",median);
    }
    }
    while(pN!=0);
    }
    
    
    //GetValues
    void GetValues(int x[], int *pN)
    {
    	int size,i;
    	printf("Enter number of integer values: ");
    	scanf("%d",&size);
    	for(i=0;i<size;i++)
    	{
    		printf("Enter Value  %d: ",i+1);
    		scanf("%d",&x[i]);
    	}
    	*pN=size;
    	return;
    }
    
    
    //SortValue
    void SortValues(int x[], int n)
    
    {
      
    	int i,j,t;
      
    	for (i=0; i<n; i++)
     
    	{
       
    		for (j=0; j<(n-1); j++)
       
    		{
    		
    			if (x[j]>x[j+1])
         
    			{
           
    				t=x[j];
            
    				x[j]=x[j+1];
            
    				x[j+1]=t;    
    			}
      
    		}
     
    	}
    
    	return;
    }
    
    
    //GetMean
    double GetMean(int x[], int n)
    {
    	int i;
    	double sum=0;
    	for(i=0;i<n;i++)
    	{
    		sum=sum+x[i];
    	}
    	sum=sum/n;
    	return sum;
    }
    
    
    //GetMedian
    double GetMedian(int x[], int n)
    {
    	int j,k,k1;
    	double median;
    	j=n%2;
    	if(j==0)
    	{
    		k=n/2;
    		k1=k-1;
    		median=x[k]+x[k1];
    		median=median/2;
    	}
    	else
    	{
    		k=n/2;
    		median=x[k];
    	}
    	return median;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by omGeeK
    Code:
    for(i=0;i<pN;i++)
    {
    	b[i]=a[i];	  // is there a better way of doing this?? i feel like it adds alot of unneccasry steps
    }
    You can just do this instead:
    Code:
    memcpy(b, a, sizeof(a[0]) * pN);
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    If you want to improve performance you could use a faster sorting algorithm than the bubble sort. lol. It is well established that that one sucks. The running time of the bubble sort algorithm goes as O(n^2) - Not very efficient.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think Swarvy hit the main point here. Like all things in life, the best approaches take the most amount of work. Bubble sort is simple, intuitive and hence by no means efficient.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    alright, thanks guys did some house keeping lol

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As the numbers are being put in, make all your calculations, right there inside that loop. No need for other functions.

    As each number is entered, it also will have an index array that will be changed, to reflect it's sorted status. So you can output the array just as if it was sorted - but it's not sorted. Only the index array is adjusted.

    Now, without moving anything but one int in the index array, you have both the original order (printed in the normal way), or the sorted order, (printed through the index instead).

    When the last number is entered, you'll make one more calculation on your running mean and median values, and on the index array for the sorted output - right inside that one loop.

    And you're done.

    It's not that you're program will be doing a lot less work - there isn't much work here. It's that you'll have everything but the last digit, already worked up. When it's entered, you're practically done already.

    If you'd like to see an index used to "sort", an array, let me know.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check my c code.
    By rfm in forum C Programming
    Replies: 4
    Last Post: 09-24-2009, 06:47 AM
  2. How to read a txt file into an array
    By Hitsugaya_KK in forum C Programming
    Replies: 49
    Last Post: 08-22-2009, 03:22 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 08:35 AM
  4. Check my code Please
    By AdioKIP in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 08:52 PM

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