# Sorting and printing out array entries

• 11-28-2010
Hybodus
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!
• 11-28-2010
msh
Compile with whatever option gives you the most warnings, and you should be able to find the bug.
• 11-28-2010
CommonTater
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.
• 11-29-2010
Jesius
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 *"
• 11-29-2010
CommonTater
Quote:

Originally Posted by Jesius
just sort the array in descending order, then put it out .

But that's not what the assignment calls for....
• 11-29-2010
claudiu
Quote:

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.
• 11-29-2010
laserlight
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.
• 11-29-2010
CommonTater
Quote:

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...
• 11-29-2010
claudiu
Quote:

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. :)
• 11-29-2010
CommonTater
Quote:

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-29-2010
Hybodus
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.
• 11-29-2010
CommonTater
Quote:

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.

Don't feel bad... you're not the only one who misread it....
• 11-29-2010
claudiu
Quote:

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.

You are welcome. By the way, there is no passing by reference in C only value.