Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed How to sort the array using the choice method in C?

+4 votes
asked Dec 3, 2020 by Natalia (1,080 points) 1 flag
closed Dec 9, 2020 by Admin

The code:

#include <stdio.h>
int main()
{
    float a[15]={2,6,7.5,4.1,5,2.3,6.2,3,1.5,4.9,0.3,12,3,14,1.4};
    int max=0;
    float buf=0;
    for(int i=0;i<15;i++){
        max=i;
        for(int j=i+1;j<15;j++){
            if(a[j]>a[max]){
              max=j;  
            }
              if(i!=max){
              buf=a[i];
              a[i]=a[max];
              a[max]=buf;   
            }
            
        }
    }
    for(int i=0;i<15;i++){
      printf("%.1f ", a[i]);  
    }
    return 0;
}

The result: 7.5 6.2 5.0 6.0 4.1 3.0 12.0 4.9 3.0 2.3 2.0 1.5 1.4 14.0 0.3.

As you can see, it's not what I want/

1 Answer

+1 vote
answered Dec 4, 2020 by Peter Minarik (84,720 points)
selected Dec 4, 2020 by Natalia
commented Dec 4, 2020 by Natalia (1,080 points)
Unfortunately, I put it twice. Thank you again)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...