Thread: Sorting and printing out array entries

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    Red face Sorting and printing out array entries

    The problem wants me to create a main function that allows users to enter a list of numbers, then create a function that finds the largest entry in magnitude then switch the largest entry that I found to the second last entry and finally print out the entire array

    I'm having some difficulties in getting this program to print out the final array.

    Code:
    #include <stdio.h>
    
    int selection_sort(int n, int a[]);
    
    int main ()
    {
      int number, a[10]={0};
      printf("Please enter 10 numbers: \n");
    
      int i;
    
      for (i=0; i<10; i++)
       {
        scanf("%d", &a[i]);
       }
    
      selection_sort(10, a[i]);
    
      for (i=0; i<10;i++)
     {
      printf("\n and the numbers in the list are: \n", a[]);
     }
    
    return 0;
    }
    
    int selection_sort (int n, int a[])
    {
     int cur, nxt, largest, i;
     for (cur = 0, nxt = 1; nxt < n; cur++, nxt++)
      {
        if (a[cur]>a[nxt])
       {
         largest = a[cur];
       }
    
        else if (a[nxt]>a[cur])
       {
        largest = a[nxt];
       }
    
        else
       {
        cur++;
        nxt++;
       }
      }
    
      printf("\nthe largest number in the list is:", largest);
     
      a[n-1]=largest;
      return a[n];
    }
    Please trace through the program and find the error, thanks in advance!
    Last edited by Hybodus; 11-28-2010 at 03:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing sorting pointer array
    By bazzano in forum C Programming
    Replies: 6
    Last Post: 08-24-2005, 02:01 PM
  2. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  3. Replies: 4
    Last Post: 05-13-2003, 04:54 PM
  4. Help with a data sorting algorithm
    By mlupo in forum C Programming
    Replies: 2
    Last Post: 11-01-2002, 02:38 PM

Tags for this Thread