Sorting and printing out array entries

This is a discussion on Sorting and printing out array entries within the C Programming forums, part of the General Programming Boards category; The problem wants me to create a main function that allows users to enter a list of numbers , then ...

1. 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};

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!

2. Compile with whatever option gives you the most warnings, and you should be able to find the bug.

3. 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. 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. Originally Posted by Jesius
just sort the array in descending order, then put it out .
But that's not what the assignment calls for....

6. Originally Posted by CommonTater
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.

7. 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.

8. Originally Posted by laserlight
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. Originally Posted by laserlight
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.

10. Originally Posted by claudiu
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. 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. Originally Posted by Hybodus
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.