Thread: sorting

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    sorting

    I have been working on this code for awhile now. I wrote this function to sort but I don't know how to make it work. Can someone help me out.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
    
    
    void swap(int *, int *);
    
    
    void bubble(int a[], int n[]) /* sorting code */
    {
    	int i, j;
    
    	for (i = 0; i < n - 1; ++1)
    		for (j = n - 1; i < j; --j)
    			if (a [j - 1] > a [j])
    				swap (&a[j-1], &a[j]);
    }
    
    
    
    int main(void)
    {
      int nums[10];
      int i, j, sum = 0;
      float average;
      int numberCount = 0;
    
      printf("Start by entering up to 10 numbers.  To exit enter a zero.\n");
      printf("\n");
        
      for(i=0; i < 10; i++) 
      {
        printf("Enter a Number: "); /*get the numbers*/
        scanf("%i", &nums[i]);            
        if(nums[i] == 0) /* exit if zero */
          break;
        else        
          numberCount++;    /*count the numbers being entered*/
      }
    
        
      printf("\n\n");
    
      average = 0; /*calculate average*/
      for(j=0; j < numberCount; j++)
        sum+=nums[j];
        average = (sum/numberCount);
    
      printf("Here are the numbers you entered:\n"); /*print out the numbers enetered*/
      for(i=0; i < numberCount; i++)
      printf("%i ",nums[i]);
      printf("\n");
    
      printf("Here are the numbers you entered sorted from lowest to highest:\n"); /*trying to sort numbers from lowest to highest here*/
      for(i=0; i < numberCount; i++)
      printf("%i ",nums[i]);
      printf("\n");
      
      printf("The sum of your numbers is %d", sum); /* display sum*/
      printf("\n");
    
      printf("You have entered %d numbers", numberCount); /*display qty of numbers entered*/
      printf("\n");
    
      printf("The average of the numbers is %.1f", average); /*display average*/
      printf("\n");
    
      getchar();
        
      return 0;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    for (i = 0; i < n - 1; ++1)
    See the error?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Another thing, why you add twice stdio?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    thes stdio.h added twice is an accident. I still can't get it to work. I have no idea why not.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    void bubble(int a[], int n[]) /* sorting code */
    N is an array, now look here dude.
    Code:
    for (j = n - 1; i < j; --j)
    you use N like he was an single variable with value.
    Pass the size of the array to the function too.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Apart from stdio.h, you also included string.h twice.

    Are you aware that you never call the bubble function anywhere in your code???
    Plus, the size n is passed as an array, not a single value as it *should* be...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I can't figure it out. I have played with this for hours but just don't get it.

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    And what about your swap function?
    you forgot to complete it

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I have made some real big changes to this code but I am still having a problem in the sorting part of it. Please help.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
     
    
    int main(void)
    {
      char theNums[10][50];
      int nums[10];
      int i, j, sum = 0;
      int numberCount = 0;
      float average;
      int n, a;
    
      printf("Start by entering up to 10 numbers.  To exit enter a zero.\n");
      printf("\n");
    	
      for(i=0; i < 10; i++) 
      {
        printf("Enter a Number: "); /*get the numbers*/
        fgets(theNums[i], 50, stdin);
    		
        j = strlen(theNums[i])-1;
    		
        theNums[i][j] = '\0'; /* get rid of '\n' */
    		
        if(theNums[i][0] == '0') /* exit if zero */
          break;
      else if(!isdigit(theNums[i][0])) /*verify user data is a digit*/
      {
    	printf("%s is not a valid entry.\n", theNums[i]);
        nums[i] = 0;
      }
      else		
        nums[i] = atoi(theNums[i]);
    	numberCount ++;	/*count the numbers being entered*/
      }
    	
      printf("\n\n");
    
      average = 0; /*calculate average*/
      for(j=0; j < i; j++)
        sum+=nums[j];
        average = (sum/numberCount);
    
      printf("Here are the numbers you entered:\n"); /*print the numbers entered on the screen*/
      for(i=0; i < numberCount; i++)
      printf("%i ",nums[i]);
      printf("\n");
    
    
    
    /*  Problem is below somwhere - i can't compile because I have like 9 errors */
      
    printf("Here are the numbers you entered sorted from lowest to highest:\n"); /*sort and print*/
    
    	for (i=0; i<numberCount-1; i++) {
            for (j=i; j<numberCount-1-i; j++) 
    		if (a[j+1] < a[j]) {  /* compare the two neighbors */
    		tmp = a[j];         /* swap a[j] and a[j+1]      */
    		a[j] = a[j+1];
    		a[j+1] = tmp;
    		}
    	}
    
      for(i=0; i < numberCount; i++)
      printf("%i ",nums[i]);
      printf("\n");
    
    /*********Problem above***********************/
      
      
    
    printf("The sum of your numbers is %d", sum); /* display sum*/
      printf("\n");
    
      printf("You have entered %d numbers", numberCount); /*display qty of numbers entered*/
      printf("\n");
    
      printf("The average of the numbers is %.2f", average); /*display average*/
      printf("\n");
    
      getchar(); /*wait for keystroke*/
    	
      return 0;
    }

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Look the basics of sorting, you're using bubble sort, so look at this simple program, and try to understand.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 5 //size of our array
    
    void bubbleSort (int arr[]);
    void swap (int arr[], int nIndex);
    
    int main(void)
    {
        int nNumbers[SIZE]; //our array
        int i; //for looping
        //print hello, and get 5 numbers
        printf("Please, enter 5 numbers for sorting them.\n");
        for(i=0;i<SIZE;++i)
            scanf("%d",&nNumbers[i]);
        
        //print the array, and sort him.
        for(i=0;i<SIZE;++i)
            printf("array[%d] = %d\n",i,nNumbers[i]);
        
        bubbleSort(nNumbers); //sort the array
        printf("\n\n");
        
        //print the sorted array
        for(i=0;i<SIZE;++i)
            printf("array[%d] = %d\n",i,nNumbers[i]);
        
        //pause and exit
        system("PAUSE");
        return 0;
    }
    
    void bubbleSort (int arr[])
    {
        int i,j;
        
        for (i=0; i<SIZE; ++i)
            for(j=0; j < (SIZE-1); ++j)
                if(arr[j] > arr[j+1])
                    swap(arr,j);
    }
    
    void swap (int arr[], int nIndex)
    {
        int temp = arr[nIndex];
        //swap the elements
        arr[nIndex] = arr[nIndex+1];
        arr[nIndex+1] = temp;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM