Thread: Sorting and printing out array entries

  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.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Compile with whatever option gives you the most warnings, and you should be able to find the bug.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummmm... read the assignment... you don't need to sort the array... you just need to scan for the highest value and put it in the second to last position.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    just sort the array in descending order, then put it out .

    Code:
    selection_sort(10, a[i]);
    it is wrong when you call selection_sort like this. the sec arg of this function is "int *"

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jesius View Post
    just sort the array in descending order, then put it out .
    But that's not what the assignment calls for....

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by CommonTater View Post
    But that's not what the assignment calls for....
    Indeed.

    There is no need to sort an entire array for just a pair of values that you need swapped. Not only that, but the specification of the problem does not mention sorting, so it would be interpreted as a wrong answer to change the rest of the array.

    HINT: You need to keep track of the max and the second_max as you are reading in numbers and then just swap the two.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by claudiu
    HINT: You need to keep track of the max and the second_max as you are reading in numbers and then just swap the two.
    My interpretation is that "second last entry" really means second last based on index, not second largest.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    My interpretation is that "second last entry" really means second last based on index, not second largest.
    Yeah, that's how I took it too...

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by laserlight View Post
    My interpretation is that "second last entry" really means second last based on index, not second largest.
    Hmm... I think you guys are right. It seems I have read the specification too quickly, but in any case that doesn't change the complexity of the program it just makes it MORE WEIRD.
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    Hmm... I think you guys are right. It seems I have read the specification too quickly, but in any case that doesn't change the complexity of the program it just makes it MORE WEIRD.
    Looking at some of these homework assignments I'd agree with the "more weird" comments. Some of the stuff that's come through here this fall is just plain crazy... stuff you'd never find in real life.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    Thanks

    Thanks a lot you guys, I'm new to C and passing-by-reference is a big challenge for me as well as pointers...arrays and alot of things. And sorry for the confusion there but the problem is quite nebulous in its wording and purpose and I should've done a better job in explaining the question.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Hybodus View Post
    Thanks a lot you guys, I'm new to C and passing-by-reference is a big challenge for me as well as pointers...arrays and alot of things. And sorry for the confusion there but the problem is quite nebulous in its wording and purpose and I should've done a better job in explaining the question.
    Don't feel bad... you're not the only one who misread it....

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Hybodus View Post
    Thanks a lot you guys, I'm new to C and passing-by-reference is a big challenge for me as well as pointers...arrays and alot of things. And sorry for the confusion there but the problem is quite nebulous in its wording and purpose and I should've done a better job in explaining the question.
    You are welcome. By the way, there is no passing by reference in C only value.
    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.

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